Python Pygame Rect未显示在屏幕上
我知道这个问题肯定已经被回答了很多次,但不幸的是,这个主题很难搜索,我真的很困惑,因为Python没有抛出错误 我正在学习Python速成课程教程(Eric Matthes),并且正在编写一个关于太空入侵者的游戏。以下模块用于控制项目符号:Python Pygame Rect未显示在屏幕上,python,pygame,Python,Pygame,我知道这个问题肯定已经被回答了很多次,但不幸的是,这个主题很难搜索,我真的很困惑,因为Python没有抛出错误 我正在学习Python速成课程教程(Eric Matthes),并且正在编写一个关于太空入侵者的游戏。以下模块用于控制项目符号: import pygame from pygame.sprite import Sprite class Bullet(Sprite): """A class to manage bullets fired from the ship""" def __
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_子弹(子弹)调用一起显示
非常感谢并致以最良好的问候,
西蒙
添加的其他文件:
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."""
导入系统
导入pygame
从bullet导入bullet
def check_keydown_事件(事件、ai_设置、屏幕、发货、项目符号):
“”“响应按键”“”
如果event.key==pygame.K_RIGHT:
#把船移到右边。
ship.moving_right=真
elif event.key==pygame.K_左:
ship.moving_left=真
elif event.key==pygame.K_空间:
发射子弹(ai设置、屏幕、飞船、子弹)
def fire_子弹(ai_设置、屏幕、装运、子弹):
#创建新项目符号并将其添加到项目符号组。
如果允许镜头(项目符号)gf.update\u screen(ai\u设置、screen、ship、bullets)
,然后使用screen.fill(ai\u设置.bg\u颜色)
擦除整个屏幕
确保先调用screen.fill(ai\u settings.bg\u color)
,然后绘制其他内容
您的代码还存在一些其他问题,例如
- 您使用了Sprite类,但是您的Sprite没有
属性,这使得使用Sprite变得毫无意义image
- 您已经为精灵使用了Group类,但是使用
循环和for
功能手动绘制精灵。只需给你的子弹一张draw\u bullet
,然后调用图像
子弹。画(屏幕)
- 您可以在
中检查项目符号是否在屏幕外。bullet类只需使用如下内容即可自行处理:更新\u bullets
如果不是self.screen.rect.contains(self.rect): 自我毁灭
- 您的整个game_functions.py文件使您的代码难以读取