赞
踩
Chapter 12 code
alien.invasion.py
--------------------
import pygame
from pygame.sprite import Grouprun_game()
settings.py
-----------------------------
class Settings():
"""store all settings for alien invasion"""
def __init__(self):
"""initiate game"""
#screen setting
self.screen_width = 900
self.screen_height = 600
self.bg_color = (230, 230, 230)
#ship speed
self.ship_speed_factor = 1.5
self.bullet_speed_factor = 1
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = 60, 60, 60
self.bullets_allowed = 3
game_functions.py
-----------------------
import sys
import pygame
from bullet import Bullet
def check_keydown_events(event, ai_settings, screen, ship, bullets):
#response to key down
if event.key == pygame.K_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 check_keyup_events(event, ship):
#response to key up
if event.key == pygame.K_RIGHT:
ship.moving_right = False
elif event.key == pygame.K_LEFT:
ship.moving_left = False
def check_events(ai_settings, screen, ship, bullets):
#reponse to keyboard and mouse
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif 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):
#Reset bg color
screen.fill(ai_settings.bg_color)
#re-draw all bullets after ship and aliens
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
#display the lastest screen
pygame.display.flip()
def update_bullets(bullets):
bullets.update()
#delete missing bullets
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
def fire_bullet(ai_settings, screen, ship, bullets):
"""if the number of bullets is less then the max, shoot a bullet"""
if len(bullets) < ai_settings.bullets_allowed:
#create a new buller then add it into group
new_bullet = Bullet(ai_settings, screen, ship)
bullets.add(new_bullet)
bullet.py
---------------------------
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""manage bullet"""
def __init__(self, ai_settings, screen, ship):
#build a bullet at the position of the ship
super(Bullet, self).__init__()
self.screen = screen
#create a rect of bullet at (0,0), then put in the right place
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
#use float to store the position of the bullet
self.y = float(self.rect.y)
self.color = ai_settings.bullet_color
self.speed_factor = ai_settings.bullet_speed_factor
def update(self):
"""move up"""
self.y -= self.speed_factor
#update 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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。