嘿伙计们,我不知道我的 python/pygame 游戏出了什么问题,当尝试使用 IDLE 运行它时,它一直给我这个错误
Traceback (most recent call last):
File "C:/code/gmaes/game/adad.py", line 102, in <module>
Game().main(screen)
File "C:/code/gmaes/game/adad.py", line 95, in main
self.tilemap.update(dt/1000.,self)
File "C:/code/gmaes/game\tmx.py", line 689, in update
layer.update(dt, *args)
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 399, in update
for s in self.sprites(): s.update(*args)
File "C:/code/gmaes/game/adad.py", line 36, in update
if self.resting and key[pygame.K_SPCAE]:
NameError: global name 'self' is not defined
我不知道为什么我会收到这个错误,而且我以前从未遇到过这个问题,我希望你们能提供帮助。这是我的完整代码,看看并留下你的想法
Python 游戏的主运行文件!python/pygame
import pygame
import tmx
class Player(pygame.sprite.Sprite):
def __init__(self,location,frames,*groups):
super(Player,self).__init__(*groups)
self.frame = 0
self.frames = frames
self.image = self.frames[self.frame]
self.rect = pygame.rect.Rect(location,self.image.get_size())
self.resting = False
self.dy = 0
self.is_dead = False
self.direction = 1
def update(slef,dt,game):
last = slef.rect.copy
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.rect -= 300 * dt
self.direction = -1
self.frame = self.frame
self.frame += 1
self.image = self.frames[frame]
if self.frame > 2:
self.frame = 0
if key[pygame.K_RIGHT]:
self.rect -= 300 * dt
self.direction = 1
self.frame = self.frame
slef.frame += 1
self.image = self.frames[frame]
if slef.frame > 2:
self.frame = 0
if self.resting and key[pygame.K_SPACE]:
self.dy = -500
self.dy = min(400,self.dy + 40)
self.rect.y += self.dy * dt
new = self.rect
self.resting = False
for cell in game.tilemap.layers['triggers'].collide(new,'blockers'):
blockers = cell['blockers']
if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
new.right = cell.left
if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
new.left = cell.right
if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
self.resting = True
new.bottom = cell.top
self.dy = 0
if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
new.top = cell.bottom
self.dy = 0
game.tilemap.set_focus(new.x,new.y)
class sprite_system(object):
def slice_sheet(self,sprite_sheet,sprite_start_x,sprite_start_y,sprite_size_x,sprite_size_y,sheet_frames):
frame = []
sheet_start = 1
frame_pos = 0
sprite_start_x_base = sprite_size_x
while sheet_start <= sheet_frames:
sheet = sprite_sheet
sheet.set_clip(pygame.Rect(sprite_start_x,sprite_start_y,sprite_size_x,sprite_size_y))
sprite_clip = sheet.subsurface(sheet.get_clip())
frame.append(sprite_clip)
sprite_start_x += sprite_start_x_base
sheet_start += 1
return frame
class Game(object):
def main(self,screen):
sprite = pygame.image.load('test.png')
frames = sprite_system().slice_sheet(sprite,0,0,64,64,4)
clock = pygame.time.Clock()
self.tilemap = tmx.load('lvl1.tmx',screen.get_size())
self.sprites = tmx.SpriteLayer()
start_cell = self.tilemap.layers['triggers'].find('player')[0]
self.player = Player((start_cell.px,start_cell.py),frames,self.sprites)
self.tilemap.layers.append(self.sprites)
while 1:
dt = clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return
self.tilemap.update(dt/1000.,self)
self.tilemap.draw(screen)
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((854,480))
Game().main(screen)
哦,这是失败的代码片段
if self.resting and key[pygame.K_SPACE]:
self.dy = -500
self.dy = min(400,self.dy + 40)
self.rect.y += self.dy * dt
new = self.rect
self.resting = False