这是主程序1.py
#1.py主程序 import sys import pygame from settings import Settings from ship import Ship from gamefunctions import update_screen from bullet import Bullet from gamefunctions import check_events from pygame.sprite import Group def run_game(): pygame.init() my_set = Settings() screen = pygame.display.set_mode(my_set.set_mode) my_ship = Ship(screen,my_set) screen = pygame.display.set_mode(my_set.set_mode) pygame.display.set_caption(my_set.set_caption) bullets = Group() while True: update_screen(my_ship,my_set,screen,bullets) run_game()
#bullet.py 这是子弹的定义程序 import pygame from pygame.sprite import Sprite class Bullet(Sprite): def __init__(self,ai_settings,screen,ship): super().__init__() self.screen = screen 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 self.y = float(self.rect.y) self.color = ai_settings.bullet_color self.speed_factor = ai_settings.bullet_speed_factor def update(self): self.y -= self.speed_factor self.rect.y =self.y def draw_bullet(self): pygame.draw.rect(self.screen,self.color,self.rect)
#gamefunctions.py import pygame import sys from bullet import Bullet def check_keydown(event,ship,screen,bullets,settings): if event.key == pygame.K_DOWN: ship.moving_down = True if event.key == pygame.K_UP: ship.moving_up = True if event.key == pygame.K_LEFT: ship.moving_left = True if event.key == pygame.K_RIGHT: ship.moving_right = True if event.key == pygame.K_SPACE: new_bullet = Bullet(settings,screen,ship) bullets.add(new_bullet) def check_keyup(event,ship): if event.key == pygame.K_RIGHT: ship.moving_right = False if event.key == pygame.K_LEFT: ship.moving_left = False if event.key == pygame.K_UP: ship.moving_up = False if event.key == pygame.K_DOWN: ship.moving_down = False def check_events(ship,screen,bullets,settings): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: check_keydown(event,ship,screen,bullets,settings) if event.type == pygame.KEYUP: check_keyup(event,ship) def update_screen(ship,settings,screen,bullets): check_events(ship,screen,bullets,settings) ship.update() screen.fill(settings.color) bullets.update() ship.blitme() for bullet in bullets.sprites(): bullet.draw_bullet() pygame.display.flip()
#settings.py 这是设置程序 class Settings(): def __init__(self): self.set_mode = (1000,800) self.set_caption = "alien invasion" self.speed_factor = 1.5 self.color = (255,255,255) self.bullet_speed_factor = 1 self.bullet_color = 60,60,60 self.bullet_width = 3 self.bullet_height = 15
#ship.py import pygame class Ship(): def __init__(self,screen,settings): self.screen = screen self.settings = settings self.image = pygame.image.load("ship.bmp") self.rect = self.image.get_rect() self.screen_rect = self.screen.get_rect() self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom self.moving_left = False self.moving_right = False self.moving_up = False self.moving_down = False self.center_x = float(self.rect.centerx) self.settings = settings def blitme(self): self.screen.blit(self.image,self.rect) def update(self): if self.moving_left and self.rect.left > 0: self.center_x -= self.settings.speed_factor if self.moving_right and self.rect.right < self.screen_rect.right: self.center_x += self.settings.speed_factor if self.moving_up: self.rect.bottom -= 1 if self.moving_down: self.rect.bottom += 1 self.rect.centerx = self.center_x
本文所用到的ship.bmp的下载 链接: https://pan.baidu.com/s/1Dn5yaQGZLIsN0BohM8PzoA 密码: 38ie
运行结果: