您好,我将不胜感激您提供有关为什么子弹没有发射的帮助,我按照书中的所有步骤操作,但它不起作用。
Bullet.py文件是新编写的,settings.py中的“#bullet settings”也是新编写的,并且alien_invasion.py中添加了一些部分。和在alien_invasion.py中的“self.bullets.sprites()中的子弹:bullet.draw_bullet”。没有画子弹吗?
外星人入侵.py
import sys
import pygame
from settings import Settings
from ship import Ship
from bullet import Bullet
class AlienInvasion:
def __init__(self):
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
self.settings.screen_width = self.screen.get_rect().width
self.settings.screen_height = self.screen.get_rect().height
#self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
self.bg_color = (0, 0, 50)
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
def _check_keyup_events(self, event):
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
def _fire_bullet(self):
""" Create a new bullet and add it to the bullets group."""
new_bullet = Bullet(self)
self.bullets.add(new_bullet)
def _check_keydown_events(self, event):
if event.key == pygame.K_RIGHT:
self.ship.rect2.x += 10
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.key == pygame.K_q:
sys.exit()
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_SPACE:
self._fire_bullet()
def _update_screen(self):
"""update images on the screen, and flip to the new screen"""
def _check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
self._check_keydown_events(event)
if event.type == pygame.KEYUP:
self._check_keyup_events(event)
for bullet in self.bullets.sprites():
bullet.draw_bullet()
def run_game(self):
while True:
self._check_events()
self.ship.update()
self.bullets.update()
self._update_screen()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
pygame.display.flip()
self.screen.fill(self.bg_color)
self.ship.blitme()
self.ship.blitme2()
if __name__ == '__main__':
ai = AlienInvasion()
ai.run_game()
设置.py
class Settings:
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
#Ship settings
self.ship_speed = 15
#Bullet settings
self.bullet_speed = 1.0
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = (60, 60, 60)
子弹.py
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""A class to manage blullets fired from the ship"""
def __init__(self, ai_game):
"""Create a bullet object at the ships current position."""
super().__init__()
self.screen = ai_game.screen
self.settings = ai_game.settings
self.color = self.settings.bullet_color
# Create a bullet rect at (0, 0) and then the correct position.
self.rect = pygame.Rect(0, 0, self.settings.bullet_width, self.settings.bullet_height)
self.rect.midtop = ai_game.ship.rect.midtop
# Store the bullets position as a decimal value.
self.y = float(self.rect.y)
def update(self):
""" Move the bullet up the screen."""
# Update the decimal position of the bullet.
self.y -= self.settings.bullet_speed
#Update the rect position.
self.rect.y = self.y
def draw_bullet(self):
""" Draw the bullet to the screen"""
pygame.draw.rect(self.screen, self.color, self.rect)
ship.py
import pygame
class Ship:
def __init__(self, ai_game):
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
self.settings = ai_game.settings
self.image = pygame.image.load('C:/Users/Documents/Python Learning/python crash course lessons/Alien Invasion/ship.bmp')
self.image2 = pygame.image.load('C:/Users/Documents/Python Learning/python crash course lessons/Alien Invasion/ship1.bmp')
self.rect = self.image.get_rect()
self.rect2 = self.image2.get_rect()
self.rect.midbottom = self.screen_rect.midbottom
self.moving_right = False
self.moving_left = False
self.x = float(self.rect.x)
self.x2 = float(self.rect2.x)
def update(self):
"""Update the ships position based on the movement flagg."""
if self.moving_right == True:
self.rect2.x += 5
self.rect.x +=2
if self.moving_right and self.rect2.right < self.screen_rect.right:
self.x += self.settings.ship_speed
if self.moving_left == True:
self.rect2.x -= 5
self.rect.x -= 2
if self.moving_left and self.rect2.left > 0:
self.x -= self.settings.ship_speed
#Update rect object from self.x
self.rect2.x = self.x
#self.rect.x = self.x
def blitme(self):
self.screen.blit(self.image, self.rect)
def blitme2(self):
self.screen.blit(self.image2, self.rect2.move(0, 670))