赞
踩
《外星人入侵》是很多Python初学者接触到的第一个项目,开发此项目时我属于只会Python语言的基本语法的新手。
《外星人入侵》,玩家控制着一艘最初出现在屏幕底部中央的飞船。玩家可以使用键盘控制飞船左右移动,还可使用空格键射击。游戏开始时,一群外星人出现在天空中,他们在屏幕中向下移动。玩家的任务是射杀这些外星人。玩家将所有外星人都消灭干净后,将出现一群新的外星人,他们移动速度更快。只要有外星人撞到了玩家的飞船或到达了屏幕底部,玩家就损失一艘飞船。玩家损失三艘飞船后,游戏结束。
可以看到,该游戏的元素有:飞机、外星人、记分牌、Play按钮以及飞机发射出的子弹,这些是显性的,还有些隐形的元素有游戏的设置模块(子弹速度、飞机移动速度等)、得分记录、游戏的运行函数。
整个程序的结构如下:
import pygame from pygame.sprite import Group from setting import settings from game_stats import GameStats from ship import ship import game_funcations as gf from button import Button from scoreboard import Scoreboard def run_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) bullets = Group() aliens = Group() gf.creat_fleet(ai_settings,screen,Ship,aliens) stats = GameStats(ai_settings) sb = Scoreboard(ai_settings,screen,stats) play_button = Button(ai_settings,screen,"play") #开始游戏主循环 while True: gf.check_events(ai_settings,screen,stats,sb,play_button,Ship,aliens,bullets) if stats.game_active: Ship.update() gf.update_bullets(ai_settings, screen,stats,sb, Ship, aliens, bullets) gf.update_aliens(ai_settings, stats,screen, sb,Ship, aliens, bullets) gf.update_screen(ai_settings,screen,stats,sb,Ship,aliens,bullets,play_button) run_game()
import sys import pygame from time import sleep from bullet import Bullet from alien import Alien def ckeck_keydown_events(event,Ship,ai_settings,screen,bullets): if event.key == pygame.K_d: Ship.moving_right = True elif event.key == pygame.K_a: Ship.moving_left = True elif event.key == pygame.K_SPACE: fire_bullet(ai_settings,screen,Ship,bullets) elif event.key ==pygame.K_q: sys.exit() def ckeck_keyup_events(event,Ship): if event.key == pygame.K_d: Ship.moving_right = False elif event.key == pygame.K_a: Ship.moving_left = False def check_events(ai_settings,screen,stats,sb,play_button,Ship,aliens,bullets): for event in pygame.event.get(): if event.type == pygame.KEYDOWN: ckeck_keydown_events(event,Ship,ai_settings,screen,bullets) elif event.type ==pygame.KEYUP: ckeck_keyup_events(event,Ship) elif event.type == pygame.MOUSEBUTTONDOWN: mouse_x,mouse_y = pygame.mouse.get_pos() check_play_button(ai_settings,screen,stats,sb,play_button,Ship,aliens,bullets,mouse_x,mouse_y) def update_bullets(ai_settings,screen,stats,sb,Ship,aliens,bullets): """更新子弹位置,并删除已经消失的子弹""" for bullet in bullets.copy(): if bullet.rect.bottom <= 0: bullets.remove(bullet) check_bullet_alien_collision(ai_settings,screen,stats,sb,Ship,aliens,bullets) def update_screen(ai_settings,screen,stats,sb,Ship,aliens,bullets,play_button): screen.fill(ai_settings.bg_color) for bullet in bullets.sprites(): bullet.draw_bullet() Ship.blitme() aliens.draw(screen) sb.show_score() if not stats.game_active: play_button.draw_button() pygame.display.flip() def fire_bullet(ai_settings,screen,Ship,bullets): if len(bullets) < ai_settings.bullet_allowed: new_bullet = Bullet(ai_settings, screen, Ship) bullets.add(new_bullet) def get_number_aliens_x(ai_settings,alien_width): available_space_x = ai_settings.screen_width - 2 * alien_width number_aliens_x = int(available_space_x / (2 * alien_width)) return number_aliens_x def get_number_rows(ai_settings,Ship_height,alien_height): available_space_y = (ai_settings.screen_height - (3*alien_height)-Ship_height) number_rows = int(available_space_y/(2*alien_height)) return number_rows def creat_alien(ai_settings,screen,aliens,alien_number,row_number): alien = Alien(ai_settings, screen) alien_width = alien.rect.width alien.x = alien_width + 2 * alien_width * alien_number alien.rect.x = alien.x alien.rect.y = alien.rect.height + 2*alien.rect.height*row_number aliens.add(alien) def creat_fleet(ai_settings,screen,Ship,aliens): alien = Alien(ai_settings,screen) number_aliens_x = get_number_aliens_x(ai_settings,alien.rect.width) number_rows = get_number_rows(ai_settings,Ship.rect.height,alien.rect.height) for row_number in range(number_rows): for alien_number in range(number_aliens_x): creat_alien(ai_settings,screen,aliens,alien_number,row_number) def check_fleet_edges(ai_settings,aliens): for alien in aliens.sprites(): if alien.check_edges(): change_fleet_direction(ai_settings,aliens) break def change_fleet_direction(ai_settings,aliens): for alien in aliens.sprites(): alien.rect.y += ai_settings.fleet_drop_speed ai_settings.fleet_direction *= -1 def update_aliens(ai_settings, stats, screen, sb,Ship, aliens, bullets): check_fleet_edges(ai_settings,aliens) aliens.update() check_aliens_bottom(ai_settings, stats, screen,sb, Ship, aliens, bullets) if pygame.sprite.spritecollide(Ship,aliens,True): Ship_hit(ai_settings, stats, screen,sb, Ship, aliens, bullets) print("Ship hit!!!") def check_bullet_alien_collision(ai_settings,screen,stats,sb,Ship,aliens,bullets): """响应子弹和外星人发生碰撞""" bullets.update() #删除发生碰撞的子弹和外星人 collisions = pygame.sprite.groupcollide(bullets, aliens, True, True) if collisions: for aliens in collisions.values(): stats.score += ai_settings.alien_points*len(aliens) sb.prep_score() check_high_score(stats,sb) if len(aliens) == 0: #如果整群外星人都被消灭,就提高一个等级 bullets.empty() ai_settings.increase_speed() #提高等级 stats.level +=1 sb.prep_level() creat_fleet(ai_settings, screen, Ship, aliens) def Ship_hit(ai_settings ,stats,screen,sb,Ship,aliens,bullets): if stats.Ship_left > 0: stats.Ship_left -= 1 #更新记分牌 #sb.prep_Ships() aliens.empty() bullets.empty() creat_fleet(ai_settings, screen, Ship, aliens) Ship.center_ship() sleep(0.5) else: stats.game_active =False pygame.mouse.set_visible(True) def check_aliens_bottom(ai_settings,stats,screen,sb,Ship,aliens,bullets): screen_rect = screen.get_rect() for alien in aliens.sprites(): if alien.rect.bottom >=screen_rect.bottom: Ship_hit(ai_settings, stats, screen, sb,Ship, aliens, bullets) break def check_play_button(ai_settings,screen,stats,sb,play_button,Ship,aliens,bullets,mouse_x,mouse_y): """玩家单击Play按钮时开始新游戏""" button_clicked = play_button.rect.collidepoint(mouse_x,mouse_y) if button_clicked and not stats.game_active: #重置游戏设置 ai_settings.initialize_dynamic_settings() #隐藏光标 pygame.mouse.set_visible(False) #重置游戏统计信息 stats.reset_stats() stats.game_active = True #重置记分牌图像 sb.prep_score() sb.prep_high_score() sb.prep_level() #sb.prep_ships() #清空外星人列表和子弹列表 aliens.empty() bullets.empty() creat_fleet(ai_settings,screen,Ship,aliens) Ship.center_ship() def check_high_score(stats,sb): """检查是否诞生了新的最高得分""" if stats.score > stats.high_score: stats.high_score =stats.score sb.prep_high_score()
import pygame from pygame.sprite import Sprite class Alien(Sprite): def __init__(self,ai_settings,screen): super().__init__() self.screen = screen self.ai_settings = ai_settings self.image2 = pygame.image.load('images/bug.bmp') self.image = pygame.transform.scale(self.image2, (60, 60)) self.rect = self.image.get_rect() self.rect.x =self.rect.width self.rect.y = self.rect.height self.x = float(self.rect.x) def blitme(self): self.screen.blit(self.image,self.rect) def check_edges(self): screen_rect =self.screen.get_rect() if self.rect.right >=screen_rect.right: return True elif self.rect.left<= 0: return True def update(self): self.x +=(self.ai_settings.alien_speed_factor*self.ai_settings.fleet_direction) self.rect.x= self.x
import pygame.font class Button(): def __init__(self,ai_settings,screen,msg): self.screen = screen self.screen_rect = screen.get_rect() self.width,self.height = 200,50 self.button_color = (0,255,0) self.text_color = (255,255,255) self.font = pygame.font.SysFont(None,48) self.rect = pygame.Rect(0,0,self.width,self.height) self.rect.center = self.screen_rect.center self.prep_msg(msg) def prep_msg(self,msg): self.msg_image = self.font.render(msg,True,self.text_color,self.button_color) self.msg_image_rect = self.msg_image.get_rect() self.msg_image_rect.center = self.rect.center def draw_button(self): self.screen.fill(self.button_color,self.rect) self.screen.blit(self.msg_image,self.msg_image_rect)
import pygame from pygame.sprite import Sprite class ship(): def __init__(self,ai_settings,screen): """初始化飞船,并设置其起始位置""" super(ship,self).__init__() self.screen = screen self.image = pygame.image.load('images/ship.bmp') self.image2=pygame.transform.scale(self.image,(80,80)) self.rect = self.image2.get_rect() self.screen_rect = screen.get_rect() self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom self.ai_settings = ai_settings self.center1 = float(self.rect.centerx) self.moving_right = False self.moving_left = False def update(self): if self.moving_right and self.rect.right < self.screen_rect.right: self.center1 += self.ai_settings.ship_speed_factor elif self.moving_left and self.rect.left > 0 : self.center1 -= self.ai_settings.ship_speed_factor self.rect.centerx = self.center1 def blitme(self): self.screen.blit(self.image2,self.rect) def center_ship(self): self.center1 = self.screen_rect.centerx
import pygame from pygame.sprite import Sprite class Bullet(Sprite): def __init__(self,ai_settings,screen,Ship): super(Bullet,self).__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)
import pygame.font from pygame.sprite import Group from ship import ship class Scoreboard(): """显示得分信息的类""" def __init__(self,ai_settings,screen,stats): """初始化显示得分涉及的属性""" self.screen =screen self.screen_rect = screen.get_rect() self.ai_settings = ai_settings self.stats =stats #显示得分信息时使用的字体设置 self.text_color =(30,30,30) self.font = pygame.font.SysFont(None,48) #准备包含最高得分和当前得分的图像 self.prep_score() self.prep_high_score() self.prep_level() #self.prep_Ships() def prep_score(self): """将得分转换为一幅渲染的图像""" rounded_score = int(round(self.stats.score,-1)) score_str = "{:,}".format(rounded_score) self.score_image = self.font.render(score_str, True, self.text_color, self.ai_settings.bg_color) # 将得分放在屏幕右上角 self.score_rect = self.score_image.get_rect() self.score_rect.right = self.screen_rect.right - 20 self.score_rect.top = 20 def prep_high_score(self): """将最高得分转换为渲染的图像""" high_score = int(round(self.stats.high_score,-1)) high_score_str = "{:,}".format(high_score) self.high_score_image = self.font.render(high_score_str,True,self.text_color,self.ai_settings.bg_color) #将最高得分放在屏幕顶部中央 self.high_score_rect =self.high_score_image.get_rect() self.high_score_rect.centerx =self.screen_rect.centerx self.high_score_rect.top =self.score_rect.top def prep_level(self): """将等级转换为渲染的图像""" self.level_image =self.font.render(str(self.stats.level),True,self.text_color,self.ai_settings.bg_color) #将等级放在得分下面 self.level_rect = self.level_image.get_rect() self.level_rect.right = self.score_rect.right self.level_rect.top = self.score_rect.bottom + 10 def show_score(self): """在屏幕上显示飞船和得分""" self.screen.blit(self.score_image,self.score_rect) self.screen.blit(self.high_score_image,self.high_score_rect) self.screen.blit(self.level_image,self.level_rect) # self.Ships.draw(self.screen) """ def prep_Ships(self): 显示还余下多少艘飞船 self.Ships = Group() for Ship_number in range(self.stats.Ship_left): Ship = ship(self.ai_settings,self.screen) Ship.rect.x =10 + Ship_number*Ship.rect.width Ship.rect.y = 10 self.Ships.add(Ship) """
class settings(): """储存游戏《外星人入侵》的所有设置类""" def __init__(self): #屏幕设置 self.screen_width = 1200 self.screen_height = 800 self.bg_color = (230,230,230) #飞船设置 self.ship_speed_factor = 1.5 self.Ship_limit =3 #子弹设置 self.bullet_speed_factor = 3 self.bullet_width = 5 self.bullet_height = 15 self.bullet_color = (10,10,230) self.bullet_allowed = 30 #外星人设置 self.fleet_drop_speed = 10 #加快游戏节奏的速度 self.speedup_scale =1.1 #外星人点数的提高速度 self.score_scale = 1.5 self.initialize_dynamic_settings() def initialize_dynamic_settings(self): '''初始化随游戏进行而变化的设置''' self.Ship_speed_factor = 1.5 self.bullet_speed_factor = 3 self.alien_speed_factor =1 #fleet_direction为1表示向左;为-1表示向右 self.fleet_direction = 1 #记分 self.alien_points = 50 def increase_speed (self): """提高速度设置""" self.ship_speed_factor *=self.speedup_scale self.bullet_speed_factor *=self.speedup_scale self.alien_speed_factor *=self.speedup_scale self.alien_points = int(self.alien_points*self.score_scale) print(self.alien_points)
class GameStats():
def __init__(self,ai_settings):
self.ai_settings = ai_settings
self.reset_stats()
self.game_active =False
#在任何情况下都不应重置最高得分
self.high_score = 0
self.level = 1
def reset_stats(self):
"""初始化随游戏进行可能变化的统计信息"""
self.Ship_left = self.ai_settings.Ship_limit
self.score = 0
1、自己对于形参的设置以后需要更加规范,程序再做的过程中,好几次报错是由于形参位置不规范造成的。
2、写代码一定要写注释
3、最后的剩余飞机数显示功能我没做出来,运行程序的时候一直报错说:add函数没有加的对象,目前还没找到原因
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。