我正在尝试在python速成课程书中完成第一个游戏项目

时间:2018-01-12 02:52:51

标签: python

当我运行代码时,我的while循环似乎没有任何效果。我无法更改屏幕颜色或添加图像。我怀疑我正在制作某种类型的简单格式错误,因为我对Python很新。谢谢!

import sys
import pygame
from settings import Settings
from ship import Ship


def run_game():
    # Initialize pygame, settings, and screen object.
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption('Alien Invasion')

    # Make a ship
    ship = Ship(screen)

    # Start the main loop for the game.
    while True:

        # Watch for keyboard and mouse events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        # Redraw the screen during each pass through the loop.
        screen.fill(ai_settings.bg_color)
        ship.blitme()

        # Make the most recently drawn screen visible.
        pygame.display.flip()


run_game()

1 个答案:

答案 0 :(得分:0)

您需要实际渲染游戏并将其翻转到显示屏上。所以你的代码看起来像这样`

import sys
import pygame
from settings import Settings
from ship import Ship


def run_game():
    # Initialize pygame, settings, and screen object.
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption('Alien Invasion')
    ship = Ship(screen)

    # Start the main loop for the game.
    while True:

    # Watch for keyboard and mouse events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

    # Redraw the screen during each pass through the loop.
        screen.fill(ai_settings.bg_color)
        ship.blitme()
        pygame.display.update()
        pygame.display.flip()


        for event in pygame.event.get():
            if event.key == K_ESCAPE:
                pygame.quit()  
run_game()

`