我正在尝试对此代码进行整合,该代码会自动使用我的Sprite代码生成一个迷宫,但是当我尝试添加自己的Sprite代码段时,程序始终会出错。
IV尝试删除和重新定义变量,并使用自己的代码段删除程序变量
{{1}}
如果有人可以帮助我找出这里的实际问题,那真是太棒了,因为当我浏览代码时,我看不到列出的当前变量有任何问题,因为我的代码段与自动生成的迷宫没有任何共同之处程序。
答案 0 :(得分:2)
The issue is the assignment of the function object
load_background to
背景`
background = load_background
您必须调用函数load_background
并将返回值分配给background
:
background = load_background()
我建议将默认参数值添加到函数load_background
的参数中:
def load_background(filename=None):
name = filename if filename else "background.jpg"
background = pygame.image.load(name)
background = pygame.transform.rotate(background, -90)
background = pygame.transform.scale(background, (800,600))
return background
如果您想选择放弃背景,请设置
background = None
并在绘制或访问背景之前检查背景是否存在:
if background:
screen.fill(pygame.Color('grey'))
screen.blit(background, (0, 0))
class Player(pygame.sprite.Sprite):
# [...]
def update(self, events, dt):
# [...]
if self.background:
new_rect.clamp_ip(self.background.get_rect())
new_pos = new_rect.center
hit_box = self.background.subsurface(new_rect)
for x in range(new_rect.width):
for y in range(new_rect.height):
if sum(hit_box.get_at((x, y))) < 500:
return
避免在主循环内出现其他循环。该应用程序应在单个循环中运行。在主循环中,应该有一个事件循环。
变量play
是state,它指示迷宫是否已建立或游戏是否处于运行状态:
# initilization
# [...]
while not done:
# event loop
for event in pygame.event.get():
# [...]
if play:
# play
# [...]
else:
# create maze
# [...]
play
为空时,必须设置状态stack
:
if next_cell != False:
current_cell.neighbors = []
stack.append(current_cell)
removeWalls(current_cell,next_cell)
current_cell.current = False
current_cell = next_cell
elif len(stack) > 0:
current_cell.current = False
current_cell = stack.pop()
else:
play = True
main
函数的外观如下:
def main():
global current_cell
player = None
initialized = False
current_maze = None
dt = 0
screen_rect = screen.get_rect()
clock = pygame.time.Clock()
sprites = pygame.sprite.Group()
if not initialized:
#current_maze = 0
background = load_background()
background = None
player = load_player(background)
sprites.add(player)
initialized = True
play = False
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
for y in range(rows):
for x in range(cols):
grid[y][x].draw()
if play:
# [...]
sprites.update(None, dt)
# [...]
sprites.draw(screen)
pygame.display.flip()
dt = clock.tick(60)
else:
current_cell.visited = True
current_cell.current = True
next_cell = current_cell.checkNeighbors()
if next_cell != False:
current_cell.neighbors = []
stack.append(current_cell)
removeWalls(current_cell,next_cell)
current_cell.current = False
current_cell = next_cell
elif len(stack) > 0:
current_cell.current = False
current_cell = stack.pop()
else:
play = True
pygame.display.flip()
最后,由于self.pos
从未更改且self.rect
从未设置,因此播放器不会移动。
在self.rect
中设置Player.update
:
def update(self, events, dt):
pressed = pygame.key.get_pressed()
move = pygame.Vector2((0, 0))
if pressed[pygame.K_w]: move += (0, -1)
if pressed[pygame.K_a]: move += (-1, 0)
if pressed[pygame.K_s]: move += (0, 1)
if pressed[pygame.K_d]: move += (1, 0)
self.pos = self.pos + move*(dt/5)
self.rect.center = self.pos