为什么我的程序停止响应并崩溃?

时间:2019-02-15 23:09:13

标签: python-3.x crash pygame

我只是想学习即将到来的项目所需的一些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))

我希望代码至少能够运行,这样我就可以在那里工作了,但它甚至没有这样做。

1 个答案:

答案 0 :(得分:0)

不要在主循环内执行另一个循环。内部循环阻止事件处理,将其删除。检查游戏是否在主循环结束时完成就足够了:

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方法和将文本“变成黑框”的ne方法draw上:

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用一种颜色填充screen(清除屏幕)。然后绘制文本。最后使用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()