当前位置:   article > 正文

武装飞船

武装飞船

Chapter 12 code

alien.invasion.py

--------------------

import pygame

from pygame.sprite import Group
from settings import Settings 
from ship import Ship


import game_functions as gf


def run_game():
#initiation and set a screen for the game
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))


pygame.display.set_caption("Alien Invasion")

ship = Ship(ai_settings, screen)


#create a group for active bullets
bullets = Group()


#start main loop
while True:


#monitor keyboard and mouse
gf.check_events(ai_settings, screen, ship, bullets)
ship.update()

gf.update_bullets(bullets)


gf.update_screen(ai_settings, screen, ship, bullets)

run_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)


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/123346
推荐阅读
相关标签
  

闽ICP备14008679号