赞
踩
在游戏《外星人入侵》中,玩家控制着一艘最初出现在屏幕底部中央的飞船。玩家可以使用箭头键左右移动飞船,还可使用空格键进行射击。游戏开始时,一群外星人出 现在天空中,他们在屏幕中向下移动。玩家的任务是射杀这些外星人。玩家将所有外星 人都消灭干净后,将出现一群新的外星人,他们移动的速度更快。只要有外星人撞到了 玩家的飞船或到达了屏幕底部,玩家就损失一艘飞船。玩家损失三艘飞船后,游戏结束。
- import sys
- import pygame
- import game_functions as gf
- from pygame.sprite import Group
- from settings import Settings
- from ship import Ship
- from alien import Alien
- from game_stats import GameStats
- 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")
-
- # 创建Play按钮
- play_button = Button(ai_settings, screen, "Play")
-
- # 创建一个用于存储游戏统计信息的实例,并创建记分牌
- stats = GameStats(ai_settings)
- sb = Scoreboard(ai_settings, screen, stats)
-
- # 创建一艘飞船
- ship = Ship(ai_settings, screen)
-
- # 创建一个用于存储子弹的编组,用于存储所有有效的子弹,以便能够管理发射出去的所有子弹。
- bullets = Group()
-
- # 创建一个外星人编组
- aliens = Group()
-
- # 创建外星人群
- gf.create_fleet(ai_settings, screen, ship, aliens)
-
- # 开始游戏的主循环
- 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, screen, stats, sb, ship, aliens, bullets)
- gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
-
-
- run_game()
- class Settings():
- """"存储《外星人入侵》的所有设置的类"""
-
- def __init__(self):
- """初始化游戏的静态设置"""
- # 屏幕设置
- self.screen_width = 1100
- self.screen_height = 650
- self.bg_color = (230, 230, 230) # 浅灰色
-
- # 飞船设置
- self.ship_limit = 3
-
- # 子弹设置
- self.bullet_width = 3
- self.bullet_height = 15
- self.bullet_color = 60, 60, 60 # 深灰色子弹
- self.bullets_allowed = 5 # 限制子弹数量
-
- # 外星人设置
- 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 # 移动速度1.5个像素值
- self.bullet_speed_factor = 3
- self.alien_speed_factor = 1
- self.fleet_direction = 1 # 右移:1 ; 左移:-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)
- 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.bmp') # 函数返回一个表示飞船的surface
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。