Pygame Rect未出现在显示器上

时间:2018-08-17 09:23:48

标签: 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 __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(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."""

1 个答案:

答案 0 :(得分:1)

检查将物体绘制到屏幕表面的顺序。

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

请务必先致电screen.fill(ai_settings.bg_color),然后 绘制其他内容。


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

  • 您使用Sprite类,但是您的Sprites没有image属性,这使得使用Sprite类毫无意义
  • 您已经为精灵使用了Group类,但是使用for循环和draw_bullet函数手动绘制了它们。只需给您的子弹一个image,然后致电bullets.draw(screen)
  • 您检查update_bullets中的项目符号是否在屏幕之外。 bullet类可以通过简单地使用如下代码本身来处理它:

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

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