赞
踩
需要一个 飞船和外星人和所以的设置类,还有飞船发射子弹的类,最后也需要一个记录分数的尅,有最高分,和当前分数,每个类都有自己的功能
下面是所以每个类的代码:
主函数,运行
# -*- coding: utf-8 -*- import pygame from pygame.sprite import Group from settings import Settings from game_stats import GameStats from scoreboard import Scoreboard from button import Button from ship import Ship from alien import Alien import game_functions as gf def run_game(): pygame.mixer.init() # 加载音乐 pygame.mixer.music.load("images/alien.mp3") # Initialize pygame, settings, and screen object. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") """创建play按钮""" play_button = Button(ai_settings,screen,"Play") """创建一个用于存储游戏统计信息的实例""" stats = GameStats(ai_settings) sb = Scoreboard(ai_settings,screen,stats) # Set the background color. bg_color = (230, 230, 230) """Make a ship.""" ship = Ship(ai_settings, screen) # Make a group to store bullets in. bullets = Group() aliens = Group() """创建外星人群""" gf.create_fleet(ai_settings,screen,ship,aliens) # Start the main loop for the game. while True: # 检查音乐流播放,有返回True,没有返回False # 如果没有音乐流则选择播放 if pygame.mixer.music.get_busy() == False: pygame.mixer.music.play() """获取 所有的事件""" 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,screen,stats,sb,ship,aliens,bullets) """重新绘制 屏幕""" gf.update_screen(ai_settings, screen,stats, sb,ship,aliens, bullets,play_button) run_game()
#子弹类
#开始按钮
# -*- coding: utf-8 -*- # 子弹 import pygame from pygame.sprite import Sprite class Bullet(Sprite): """A class to manage bullets fired from the ship""" def __init__(self, ai_settings, screen, ship,stats): """Create a bullet object at the ship's current position.""" super(Bullet, self).__init__() self.screen = screen self.num = stats.ships_left # Create a bullet rect at (0, 0) and then set correct position. 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 # Store the bullet's position as a decimal value. self.y = float(self.rect.y) self.color = ai_settings.bullet_color self.speed_factor = ai_settings.bullet_speed_factor if self.num == 2: self.rect1 = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height) self.rect1.centerx = ship.rect.centerx + 20 self.rect1.top = ship.rect.top # Store the bullet's position as a decimal value. self.y1 = float(self.rect.y) if self.num == 1: """第二发子弹""" self.rect1 = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height) self.rect1.centerx = ship.rect.centerx + 20 self.rect1.top = ship.rect.top # Store the bullet's position as a decimal value. self.y1 = float(self.rect1.y) self.x1 = float(self.rect1.x) """第三发子弹""" self.rect2 = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height) self.rect2.centerx = ship.rect.centerx - 20 self.rect2.top = ship.rect.top # Store the bullet's position as a decimal value. self.y2 = float(self.rect2.y) self.x2 = float(self.rect2.x) def update(self): """Move the bullet up the screen.""" # Update the decimal position of the bullet. self.y -= self.speed_factor # Update the rect position. self.rect.y = self.y if self.num == 2 : self.y1 -= self.speed_factor # Update the rect position. self.rect1.y = self.y1 if self.num == 1: self.y1 -= self.speed_factor self.x1 += 0.5 # Update the rect position. self.rect1.y = self.y1 self.rect1.x = self.x1 self.y2 -= self.speed_factor self.x2 -= 0.5 # Update the rect position. self.rect2.y = self.y2 self.rect2.x = self.x2 def draw_bullet(self): """Draw the bullet to the screen.""" pygame.draw.rect(self.screen, self.color, self.rect) if self.num == 2: pygame.draw.rect(self.screen, self.color, self.rect1) if self.num == 1: pygame.draw.rect(self.screen, self.color, self.rect1) pygame.draw.rect(self.screen, self.color, self.rect2)
# -*- coding: utf-8 -*- 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) #创建按钮的rect对象,并使其剧中 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): """将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)
# -*- coding: utf-8 -*- class GameStats(): """跟踪游戏的统计信息""" def __init__(self,ai_settings): """初始化统计信息""" self.ai_settings = ai_settings self.reset_stats() #游戏刚开始处于0活跃状态 self.game_active = False #在任何情况下都不应重置最高得分 self.high_score = 0 def reset_stats(self): """初始化在游戏与逆行期间可能变化的统计信息""" self.ships_left = self.ai_settings.ship_limit self.score = 0 self.level = 1
#分数类
# -*- coding: utf-8 -*- 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() #准备包含最高分和当前的分的图像 self.prep_score() self.prep_high_score() def prep_score(self): """将得分转化为一副渲染的图像""" rounded_score = int(round(self.stats.score,-1)) score_str = "Score:"+"{: ,}".format(rounded_score) self.score_image = self.font.render(score_str,True,self.text_color, self.ai_settings.bg_color1) #将得分放在屏幕右上角 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 = "High Score:"+"{: ,}".format(high_score) self.high_score_image = self.font.render(high_score_str, True, self.text_color,self.ai_settings.bg_color1) # 将最高得分放在屏幕中央 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 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_level(self): """将等级转化为渲染的图像""" level_str = "level:" + "{: ,}".format(self.stats.level) self.level_image = self.font.render(level_str,True, self.text_color,self.ai_settings.bg_color1) #将等级放在得分下面 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 prep_ships(self): """显示还剩下多少搜飞船""" self.ships = Group() for ship_number in range(self.stats.ships_left-1): ship = Ship(self.ai_settings,self.screen) ship.rect.x = 10+ship_number * ship.rect.width ship.rect.y = 10 self.ships.add(ship)
#设置类
# -*- coding: utf-8 -*- import pygame # 管理所有与游戏设置有关的操作 class Settings(): """A class to store all settings for Alien Invasion.""" def __init__(self): """Initialize the game's settings.""" # Screen settings self.screen_width = 550 self.screen_height = 520 self.bg_color = (230, 230, 230) self.bg_color1 = pygame.image.load('images/b.jpg') self.rect = self.bg_color1.get_rect() # 飞船设置 #self.ship_speed_factor = 1.5 #self.bullet_speed_factor = 3 self.ship_limit = 3 # Bullet settings self.bullet_speed_factor = 1.5 self.bullet_width = 3 self.bullet_height = 25 self.bullet_color = 225, 67, 209 self.bullets_allowed = 5 #外星人设置 #self.alien_speed_factor = 1 # self.fleet_drop_speed = 10 #fleet_direction为1表示向右移动,-1表示向左移动 self.fleet_direction = 1 #以什么样的速度加快游戏节奏 self.fleet_drop_speed = 20 #加快游戏节奏的速度 self.speedup_scale = 1.1 #外星人点数提高 self.score_scale = 1.5 self.initialize_dynamic_settings() 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) def initialize_dynamic_settings(self): """初始化岁游戏进行二变化的设置""" self.ship_speed_factor = 2 self.bullet_speed_factor = 5 self.alien_speed_factor = 1.5 # fleet_direction为1表示向右移动,-1表示向左移动 self.fleet_direction = 1 #积分 self.alien_points = 50
#飞船类
# -*- coding: utf-8 -*- # ship import pygame from pygame.sprite import Sprite class Ship(Sprite): def __init__(self, ai_settings, screen): super(Ship,self).__init__() """初始化飞船并设置其初始位置""" self.screen = screen self.ai_settings = ai_settings """加载飞船图像并获取其外接矩形""" self.image = pygame.image.load('images/ship.png') self.rect = self.image.get_rect() self.screen_rect = screen.get_rect() """将每艘飞船放在屏幕底部中央""" self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom self.center = 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.center += self.ai_settings.ship_speed_factor elif self.moving_left and self.rect.left > self.screen_rect.left: self.center -= self.ai_settings.ship_speed_factor self.rect.centerx = self.center def blitme(self): """在制定位置绘制飞船""" self.screen.blit(self.image, self.rect) def center_ship(self): self.center = self.screen_rect.centerx
#外星人类
# -*- coding: utf-8 -*- # 外星人类 import pygame from pygame.sprite import Sprite class Alien(Sprite): """表示单个外星人""" def __init__(self,ai_settings,screen): """初始化外星人并设置其起始位置""" super(Alien,self).__init__() self.screen = screen self.ai_settings = ai_settings #加载外星人图像,并设置rect属性 self.image = pygame.image.load('images/alien.png') 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 check_edges(self): """如果外星人位于屏幕边缘,就返回True""" 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.fleet_direction * self.ai_settings.alien_speed_factor) self.rect.x = self.x def blitme(self): """在指定位置绘制外星人""" self.screen.blit(self.image,self.rect).rect.x = self.x
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。