Pygame Rect 未显示在显示屏上



我知道这肯定已经回答了很多次,但不幸的是,这个话题很难搜索,我真的很失败,因为Python没有抛出错误。

我正在遵循Python速成课程教程(Eric Matthes(,并正在按照太空入侵者的路线编写游戏。以下模块旨在控制项目符号:

import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""A class to manage bullets fired from the ship"""
def __init__(self, ai_settings, screen, ship):
"""Create bullet at the ships current positio"""
super().__init__()
self.screen = screen
# Create a bullet rect at (0, 0) and then set the correct position.
self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, 
ai_settings.bullet_height)
self.rect.centerx = ship.rect.centerx
self.rect.top = ship.rect.top
# Store the bullet's position as a decimal value.
self.y = float(self.rect.y)
self.color = ai_settings.bullet_color
self.speed_factor = ai_settings.bullet_speed_factor
def update(self):
"""Move the bullet up the screen."""
# Update the decimal position of the bullet.
self.y -= self.speed_factor
# Update the rect position.
self.rect.y = self.y
def draw_bullet(self):
"""Draw the bullet on the screen."""
pygame.draw.rect(self.screen, self.color, self.rect)

实际的游戏屏幕正在打印子弹计数,所以我知道子弹在屏幕上,并且随着向屏幕边缘移动而再次消失,但它们没有显示。

游戏本身如下所示:

import sys
import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
# Initialize game and create a 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(ai_settings, screen)
# Make a group to store the bullets in.
bullets = Group()
# Start main loop for the game
while True:
#Watch for keyboard and mouse events.
gf.check_events(ai_settings, screen, ship, bullets)
ship.update()
gf.update_bullets(bullets) 

gf.update_screen(ai_settings, screen, ship, bullets)
# Redraw Screen during each pass through.
screen.fill(ai_settings.bg_color)
ship.blitme()
# Make most recently drawn visible
pygame.display.flip()
run_game()

设置在那里,没有,项目符号与屏幕的颜色不同;-(

谁能帮我找到我的思维错误? 我的印象是,pygame.draw.rect函数应该使它们与gf.update_bullets(项目符号(调用一起显示。

非常感谢和最诚挚的问候, 西蒙

添加了其他文件: game_functions.py

import sys
import pygame
from bullet import Bullet
def check_keydown_events(event, ai_settings, screen, ship, bullets):
"""Respond to keypresses"""
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
ship.moving_right = True
elif event.key == pygame.K_LEFT:
ship.moving_left = True
elif event.key == pygame.K_SPACE:
fire_bullet(ai_settings, screen, ship, bullets)
def fire_bullet(ai_settings, screen, ship, bullets):
# Create a new bullet and add it to the bullets group.
if len(bullets) < ai_settings.bullets_allowed:
new_bullet = Bullet(ai_settings, screen, ship)
bullets.add(new_bullet)
def check_keyup_events(event, ship):
"""Respond to keypresses"""
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
def check_events(ai_settings, screen, ship, bullets):
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
check_keydown_events(event, ai_settings, screen, ship, bullets)
elif event.type == pygame.KEYUP:
check_keyup_events(event, ship)
def update_screen(ai_settings, screen, ship, bullets):
# Redraw all bullets behind the ship and aliens
for bullet in bullets.sprites():
bullet.draw_bullet()
def update_bullets(bullets):
"""Update position of bullets and get rid of old bullets"""
# Update bullet position
bullets.update()
# Get rid of bullets that have disappeared.
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
print(len(bullets))

"""Update images on the screen and flip to the new screen."""

检查将内容绘制到屏幕表面的顺序。

你打电话给gf.update_screen(ai_settings, screen, ship, bullets),之后,你实际上用screen.fill(ai_settings.bg_color)擦除了整个屏幕。

确保先打电话给screen.fill(ai_settings.bg_color)然后再画出你的其他东西。


您的代码还有其他一些问题,例如

  • 你使用精灵类,但你的精灵没有image属性,这使得使用精灵毫无意义
  • 您已经将 Group 类用于精灵,但使用for循环和draw_bullet函数手动绘制它们。只需给你的子弹一个image,然后打电话给bullets.draw(screen)
  • 您检查子弹是否在屏幕外update_bullets.bullet 类通过简单地使用如下所示的内容自行处理它:

    if not self.screen.rect.contains(self.rect): self.kill

  • 整个game_functions.py文件使代码难以阅读

最新更新