使用python和pygame的类

时间:2014-02-24 11:01:07

标签: python class pygame

我刚开始的游戏有两个脚本,一个是主游戏,另一个是在第一个脚本运行时显示3个图像,所以当我运行第一个脚本(mainGame.py)时,它应该运行使用类和函数第二个脚本(startUp.py)然后startUP将显示带有游戏标题和屏幕创建的图像,然后我会让它继续游戏菜单。但当然它不起作用,这是我的主要游戏代码:

import pygame, random, math, sys, os, time
import startUp
from pygame.locals import *
pygame.init()

while True:

    #quit button 
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    startUp.Begin.Run()

第二个脚本的代码(startUp.py):

import pygame, time
from pygame.locals import *
pygame.init()

class Begin(self):

    def __init__(self):
        self.screenA = "createdbyscreen.png"
        self.screenB = "powerdbyscreen.png"
        self.screenC = "gamelogoscreen.png"
        self.blackscreen = "blackSreen.png"

        self.screen = pygame.display.set_mode((1440, 720), 0, 32)

        self.startscreenA = pygame.image.load(screenA).convert()
        self.startscreenB = pygame.image.load(screenB).convert()
        self.startscreenC = pygame.image.load(screenC).convert()
        self.blackscreen = pygame.image.load(blackscreen).convert()

    def Run(self):
        while True:
           #starting imagers
            self.screen.blit(blackscreen, (0, 0))
            self.timeLeft = pygame.time.get_ticks()
            print(self.timeLeft)
            if self.timeLeft < 3500:
                self.self.screen.blit(self.startscreenA, (480, 120))
            if self.timeLeft > 4000 and self.timeLeft < 7500:
                self.screen.blit(self.startscreenB, (480, 120))
            if self.timeLeft > 8000 and self.timeLeft < 11150:
                self.screen.blit(self.startscreenC, (480, 120))

            pygame.display.update()

然后如果它有帮助那么这里是错误消息:

第一个脚本:

Traceback (most recent call last):
  File "C:\Users\claude\Desktop\game\dnagame.py", line 2, in <module>
    import startUp
  File "C:\Users\claude\Desktop\game\startUp.py", line 5, in <module>
    class Begin(self):
NameError: name 'self' is not defined

和第二个:

Traceback (most recent call last):
  File "C:/Users/claude/Desktop/game/startUp.py", line 5, in <module>
    class Begin(self):
NameError: name 'self' is not defined
谢谢你的时间和帮助!

2 个答案:

答案 0 :(得分:2)

class Begin(self):

您应该继承object

class Begin(object):

您还需要创建一个实例

begin = startUp.Begin()
begin.Run()

答案 1 :(得分:0)

一个类必须继承自object而不是self

class Begin(object):

self是用于实例方法的第一个参数的约定名称,它是对实例本身的引用。