在python中将类移至其自己的文件

时间:2018-11-25 22:34:06

标签: python pygame

我有2个文件,其中一个是主文件,另一个是播放器类,我收到了这些错误:

Traceback (most recent call last):
File "C:/Users/travi/PycharmProjects/game/main.py", line 15, in <module>
player.player1 = player.player(300, 410, 32, 32)
File "C:\Users\travi\PycharmProjects\game\player.py", line 18, in __init__
self.playerImage
AttributeError: 'player' object has no attribute 'playerImage'

这里有源代码

#file 1############################################
import pygame
import player

pygame.init()

size = WIDTH, HEIGHT = (640, 480)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Game 2")

FPS = 60
fpsclock = pygame.time.Clock()
animation_speed = 5

player.player1 = player.player(300, 410, 32, 32)
run = True
while run:
pygame.time.delay(50)

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False

keys = pygame.key.get_pressed()

if player.player1.active:
    player.player1.frame += 1
if player.player1.frame >= 2:
    player.player1.frame = 0

player.player1.playerImage = player.player1.cells[player.player1.frame + player.player1.dir]


if keys[pygame.K_LEFT] and player.player1.x > player.player1.vel:
    player.player1.x -= player.player1.vel
    player.player1.dir = 3
    player.player1.active = True
elif keys[pygame.K_RIGHT] and player.player1.x < 640 - player.player1.width - player.player1.vel:
    player.player1.x += player.player1.vel
    player.player1.dir = 6
    player.player1.active = True
elif keys[pygame.K_UP] and player.player1.y > player.player1.vel:
    player.player1.y -= player.player1.vel
    player.player1.dir = 9
    player.player1.active = True
elif keys[pygame.K_DOWN] and player.player1.y < 480 - player.player1.height - player.player1.vel:
    player.player1.y += player.player1.vel
    player.player1.dir = 0
    player.player1.active = True
else:
    player.player1.active = False


screen.fill((0, 0, 0))

screen.blit(player.player1.playerImage, (player.player1.x, player.player1.y))

pygame.display.flip()
fpsclock.tick(FPS)

pygame.quit()
##文件2
import pygame

class player():
  def __init__(self, x, y, width, height):
    self.cells = []
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.vel = 5
    self.left = False
    self.right = False
    self.dir = 0
    self.walkcount = 0
    self.active = False
    self.frame = 0
    self.spritesheetplayer = 
    pygame.image.load("playerspritesheet.png").convert()
    self.playerImage

def initialize(self):
    for n in range(12):
        width, height = (32, 32)
        rect = pygame.Rect(n * width, 0, width, height)
        image = pygame.Surface(rect.size).convert()
        image.blit(self.spritesheetplayer, (0, 0), rect)
        alpha = image.get_at((0, 0))
        image.set_colorkey(alpha)
        self.cells.append(image)
    self.playerImage = self.cells[0]

这些应该轮询事件并移动和设置角色动画的代码最初都在一个文件中,一旦我将其中的一些移动到另一个文件中,这些错误就开始发生,如何修复该代码,使其有效

1 个答案:

答案 0 :(得分:1)

您必须为playerImage设置初始值,例如,在您的 init 函数self.playerImage = None