我只是想学习一些我即将进行的项目所需的 PyGame,所以我想知道为什么这段示例代码崩溃以及如何修复它。
我尝试在网上查找,但没有找到有效的解决方案。
import pygame
# Initialise pygame
pygame.init()
clock = pygame.time.Clock()
# pygame.font.init()
myFont = pygame.font.SysFont('Comic Sans MS', 30)
# Colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Screen Size
size = [400, 300]
screen = pygame.display.set_mode(size)
# Initialise Variables
done = False
class Unit:
def __init__(self, name, hp, attack):
self.name = name
self.hp = hp
self.attack = attack
class Infantry(Unit):
def __init__(self, name, hp, attack):
super().__init__(name, hp, attack)
def attack_turn(self):
self.hp -= self.attack
text_surface = myFont.render(self.name + " has " + str(self.hp) + " health points left!", True, (255, 0, 0))
screen.blit(text_surface, (0, 0))
# print(self.name + " has " + str(self.hp) + " health points left!")
x = input("Press ENTER to continue!")
player = Infantry("Panzer", 110, 13) # The attack set here is for the enemy.
enemy = Infantry("T-14", 100, 30) # The attack set here is for the player.
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
while player.hp > 0 and enemy.hp > 0:
player.attack_turn()
enemy.attack_turn()
done = True
if player.hp > enemy.hp:
# print(player.name + " wins!")
text_surface = myFont.render(player.name + " wins!", False, (255, 0, 0))
screen.blit(text_surface, (100, 100))
else:
# print(enemy.name + " wins!")
text_surface = myFont.render(enemy.name + " wins!", False, (255, 0, 0))
screen.blit(text_surface, (100, 100))
我希望代码至少能够运行,这样我就可以在那里工作,但它甚至没有这样做。
最佳答案
不要在主循环内执行另一个循环。内部循环阻止了事件处理,摆脱它。在主循环结束时检查游戏是否已完成就足够了:
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
player.attack_turn()
enemy.attack_turn()
if player.hp <= 0 or enemy.hp <= 0:
done = True
pygame.display.flip()
input
没有执行您想要的操作,它不是游戏窗口的输入。删除它,并将 attack
方法拆分为计算新生命值的 on 方法和一个新方法 draw
,该方法“blit”文本:
class Infantry(Unit):
def __init__(self, name, hp, attack):
super().__init__(name, hp, attack)
def attack_turn(self):
self.hp -= self.attack
### x = input("Press ENTER to continue!") <----- delete
def draw(self):
text_surface = myFont.render(self.name + " has " + str(self.hp) + " health points left!", True, (255, 0, 0))
screen.blit(text_surface, (0, 0))
使用pygame.KEYUP
事件来检查pygame.K_RETURN
键是否被按下。
在主循环结束时进一步进行所有绘图。使用pygame.Surface.fill
用单一颜色填充屏幕
(清除屏幕)。然后绘制文字。最后使用pygame.display.flip()
更新完整显示:
例如
done = False
count = 0
while not done:
attack = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
attack = True
if attack:
if count % 2 == 0:
player.attack_turn()
else:
enemy.attack_turn()
count += 1
if player.hp <= 0 or enemy.hp <= 0:
done = True
screen.fill(BLACK)
if count % 2 == 0:
player.draw()
else:
enemy.draw()
pygame.display.flip()
关于python-3.x - 为什么我的程序停止响应并崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54718211/