【发布时间】:2020-06-25 20:48:07
【问题描述】:
我一直在关注 Python 速成课程项目来创建一个太空入侵者游戏。我已经能够编码到游戏窗口将打开的地步,但是我没有在屏幕的中间底部使用我的图像获取背景,而是遇到了黑屏。我是编码新手,并且已经看到这对其他人来说也是一个类似的问题。我正在使用 Python 3.8 也成功上传了正确的 pip 安装 (https://ehmatthes.github.io/pcc_2e/updates/python3_8/#pygame-on-python-38)。
这是外星人入侵的代码:
import sys
import pygame
class AlienInvasion:
"""Overall Class to manage game assests and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.screen = pygame.display.set_mode((1100, 700))
pygame.display.set_caption("Alien Invasion")
def run_game(self):
"""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()
# Make the most recently drawn screen Visible
pygame.display.flip()
if __name__ == "__main__":
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
def __init__(self):
pygame.display.set_caption("Alien Invasion")
# Set the Background Color
self.bg_color = (230, 230, 230)
def run_game(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#redraw the screen during each pass through the loop
self.screen.fill(self.bg_color)
#make the most recently drawn screen visible.
pygame.display.flip()
import pygame
from settings import Settings
class AlienInvasion:
"""Overall Class to manage game assets and behavior"""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
def run_game(self):
# Redraw the screen during each pass through the loop.
self.screen.fill(self.settings.bg_color)
#make most recently drawn screen visible.
pygame.display.flip()
from settings import Settings
from ship import Ship
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
def run_game(self):
# Redraw the screen during each pass through the loop.
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Make most recently drawn screen visible
pygame.display.flip()
def run_game(self):
"""Start the main loop for the game."""
while True:
self._check_events()
# Redraw the screen during each pass through the loop
break
def _check_events(self):
"""Respond to keypress and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def run_game(self):
"""Start the main loop for the game"""
while True:
self._check_events()
self._update_screen()
def _check_events(self):
--snip--
def _update_sceen(self):
"""Update images on the screen, and flip to the new screen."""
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
pygame.display.flip()
设置
class Settings:
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's static settings."""
# Screen settings.
self.screen_width = 1100
self.screen_height = 700
self.bg_color = (230, 230, 230)
宇宙飞船:
import pygame
class Ship:
"""A class to manage the ship."""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position."""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
# Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
# Start each new ship at the bottom center of the screen
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
"""Draw the ship at current location"""
self.screen.blit(self.image, self.rect)
我目前正在使用 Eric Matthes 的 Python 速成课程。
【问题讨论】:
-
你能分享一些关于你的环境的信息吗?什么操作系统、软件包及其版本等。