我为一个大学项目用 python 编写了一个小游戏。它处于早期阶段,但我在运行时遇到问题。当我运行 .py 文件时,会出现一个黑色窗口。当我按“X”按钮关闭窗口时,只有我的背景显示一秒钟,然后窗口关闭。简而言之,只有当我关闭窗口时,我的游戏才会运行。import pygameimport matplotlib.pyplot as pltimport matplotlib.colors as colorsimport numpyBLACK = (0, 0, 0)WHITE = (255, 255, 255)BLUE = (50, 50, 255)RED = (255, 0, 0)GREEN = (0, 255, 0)player_speed = 10class Player(pygame.sprite.Sprite): change_x = 0 change_y = 0 def __init__(self, x, y): super().__init__() self.image = pygame.Surface([10, 10]) self.image.fill(WHITE) self.rect = self.image.get_rect() self.rect.y = y self.rect.x = x self.change_x = 0 self.change_y = 0 #self.walls = None def changespeed(self, x, y): self.change_x += x self.change_y += y def move(self, walls): self.rect.x += self.change_x block_hit_list = pygame.sprite.spritecollide(self, walls, False) for block in block_hit_list: if self.change_x > 0: self.rect.right = block.rect.left else: self.rect.left = block.rect.right self.rect.y += self.change_y block_hit_list = pygame.sprite.spritecollide(self, walls, False) for block in block_hit_list: if self.change_y > 0: self.rect.bottom = block.rect.top else: self.rect.top = block.rect.bottomclass Wall(pygame.sprite.Sprite): def __init__(self, x, y, width, height): super().__init__() self.image = pygame.Surface([width, height]) self.image.set_alpha(0) self.image.fill([255, 255, 255]) self.rect = self.image.get_rect() self.rect.y = y self.rect.x = xclass Level(object): wall_list = None enemy_sprites = None def __init__(self): self.wall_list = pygame.sprite.Group() self.enemy_sprites = pygame.sprite.Group()
2 回答
MMTTMM
TA贡献1869条经验 获得超4个赞
谢谢你们。我很抱歉那些不需要的大代码,但我真的不知道出了什么问题。
我编写我的代码,而不是我的“while” 但是:在我将命令放入 while 循环并使代码正确运行后,“玩家”移动非常缓慢并且“点对点”移动。之前“玩家”的举动非常正常和流畅。
添加回答
举报
0/150
提交
取消