为什么我的程序停止响应并崩溃?
Why does my program stop responding and crashing?
我只是想学习一些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
没有按照您的意愿行事 它不是游戏的输入 window。删除它并将 attack
方法拆分为计算新生命值的方法和一个新方法 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
to fill screen
by a single color (clear the screen). Then draw the text. Finally use 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()
我只是想学习一些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
没有按照您的意愿行事 它不是游戏的输入 window。删除它并将 attack
方法拆分为计算新生命值的方法和一个新方法 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
to fill screen
by a single color (clear the screen). Then draw the text. Finally use 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()