您好,登錄后才能下訂單哦!
簡介
使用python實現(xiàn)pygame版的飛機大戰(zhàn)游戲;
環(huán)境:Windows系統(tǒng)+python3.8.0
游戲規(guī)則:
1.點擊“PLAY”或者按鍵“P”開始游戲;
2.敵機根據(jù)設(shè)置頻率從頂部隨機位置生成,生成后向下移動;
3.飛船在底部中間生成,玩家使用上下左右鍵控制飛船移動,敲擊空格鍵發(fā)射子彈;
4.子彈打到敵機,該敵機產(chǎn)生爆炸效果并累計分?jǐn)?shù)到右上角;
5.消滅10只飛機后,等級升高,敵機生成頻率變快,下落速度也變快;
6.當(dāng)三條命都消失了,游戲結(jié)束。
游戲運行效果如下:
實現(xiàn)過程
1.新建文件“file.py”,用來存儲信息到文件和讀取文件的信息,本例用來存儲和讀取最高分;
import pickle # filename = 'file/stats.pkl' # 存儲信息到文件 def save_file(obj, filename): statsObj = load_file(filename) if statsObj == 0: # 不存在文件時,直接保存字典 with open(filename, 'wb') as f: pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL) else: # 存在文件時,只修改文件中的最高分 for key, val in statsObj.items(): # 獲取文件最高分的值(當(dāng)文件字段不止一個時候使用) if key == 'highScore': statsObj[key] = obj['highScore'] obj = statsObj with open(filename, 'wb') as f: pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL) # 讀取信息 def load_file(filename): try: with open(filename, 'rb') as f: return pickle.load(f) except FileNotFoundError: # 不存在文件則輸入錯誤信息 msg = "Sorry, the file " + filename + " does not exist." print(msg) return 0 # obj = {'highScore': 20, 'points': 5} # obj = {'highScore': 50} # save_file(obj, filename) # filedata = load_file(filename) # print(filedata)
2.k新建文件settings.py,用來定義一些必須的基本屬性和初始值;
import file as f class Settings(): def __init__(self): self.screen_width = 480 self.screen_height = 660 self.bg_color = (230, 230, 230) # 子彈設(shè)置(寬、高、顏色、最大數(shù)量) self.bullet_width = 5 self.bullet_height = 15 self.bullet_color = 255, 255, 255 # 敵機移動頻率 self.enemy_frequency = 0 # 加快游戲節(jié)奏的速度 self.speedup_scale = 1.1 # 分?jǐn)?shù)的提高速度 self.score_scale = 1.5 self.initialize_settings() # 初始化統(tǒng)計信息 self.reset_stats() # 統(tǒng)計信息文件路徑 self.filename = 'file/stats.pkl' # 游戲剛啟動時處于非活動狀態(tài) self.game_active = False # 讀取文件的最高分,在任何情況下都不應(yīng)重置最高得分 statsObj = f.load_file(self.filename) if statsObj == 0: # 不存在文件則顯示最高分0 highScore = 0 else: for key, val in statsObj.items(): # 獲取文件最高分的值(當(dāng)文件字段不止一個時候使用) if key == 'highScore': highScore = val self.high_score = highScore def initialize_settings(self): """初始化隨游戲進(jìn)行而變化的設(shè)置""" self.player_move_speed = 2.5 self.bullet_speed = 3 self.enemy_move_speed = 1 # 記分 self.one_points = 50 def increase_speed(self): """提高速度設(shè)置""" # self.player_move_speed *= self.speedup_scale self.bullet_speed *= self.speedup_scale self.enemy_move_speed *= self.speedup_scale self.one_points = int(self.one_points * self.score_scale) def reset_stats(self): """初始化在游戲運行期間可能變化的統(tǒng)計信息""" # 可射失的數(shù)量 self.player_limit = 3 # 射擊分?jǐn)?shù) self.score = 0 # 等級 self.level = 1 # 打中多少矩形升一級 self.level_number = 10 # 生成敵機頻率間隔 self.enemy_frequency_space = 50
3.新建文件enemy.py,用來定義敵機類(位置topleft隨機生成)和聲明方法move;
import pygame import random from pygame.sprite import Sprite class Enemy(Sprite): def __init__(self, enemy_down_imgs, settings): super(Enemy, self).__init__() self.image = pygame.image.load('images/enemy1.png') self.rect = self.image.get_rect() self.rect.topleft = [random.randint(0, settings.screen_width - self.rect.width), 0] self.down_imgs = enemy_down_imgs self.speed = settings.enemy_move_speed self.down_index = 0 # 敵機移動,邊界判斷及刪除在游戲主循環(huán)里處理 def move(self): self.rect.top += self.speed
4.新建文件player.py,用來定義玩家類(可上下左右移動)和相應(yīng)的方法;
import pygame from pygame.sprite import Sprite class Player(Sprite): def __init__(self, settings, screen): super(Player, self).__init__() self.settings = settings self.screen = screen self.screen_rect = self.screen.get_rect() # 引入飛船圖片并定位 self.image = pygame.image.load('images/player.png') self.rect = self.image.get_rect() self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom # 移動標(biāo)志 self.move_left = False self.move_right = False self.move_down = False self.move_up = False def rotate(self, angle): # 圖片旋轉(zhuǎn) self.image = pygame.transform.rotate(self.image, angle) def scale(self, multiple): # 圖片縮放 self.image = pygame.transform.smoothscale(self.image, (multiple, multiple)) def update(self): if self.move_left and self.rect.left > self.screen_rect.left: self.rect.centerx -= self.settings.player_move_speed if self.move_right and self.rect.right < self.screen_rect.right: self.rect.centerx += self.settings.player_move_speed if self.move_down and self.rect.bottom < self.screen_rect.bottom: self.rect.centery += self.settings.player_move_speed if self.move_up and self.rect.top > 0: self.rect.centery -= self.settings.player_move_speed def draw_player(self): """繪制飛船到屏幕""" self.screen.blit(self.image, self.rect)
5.新建文件“bullet.py”,用來定義子彈類(位置在飛船的頂部,并往上移動)和相應(yīng)的方法;
import pygame from pygame.sprite import Sprite class Bullet(Sprite): """ 一個對飛船發(fā)射的子彈進(jìn)行管理的類 """ def __init__(self, settings, screen, player): """ 在飛船所處的位置創(chuàng)建一個子彈對象 """ super(Bullet, self).__init__() self.screen = screen # 在 (0,0) 處創(chuàng)建一個表示子彈的矩形,再設(shè)置正確的位置 self.rect = pygame.Rect(0, 0, settings.bullet_width, settings.bullet_height) self.rect.centerx = player.rect.centerx # 飛船頂部 self.rect.bottom = player.rect.top # 存儲用小數(shù)表示的子彈位置 self.y = float(self.rect.y) self.color = settings.bullet_color self.speed = settings.bullet_speed def update(self): """向上移動子彈""" # 更新表示子彈位置的小數(shù)值(子彈往右) self.y -= self.speed # 更新表示子彈的rect的位置 self.rect.y = self.y def draw_bullet(self): """在屏幕上繪制子彈""" pygame.draw.rect(self.screen, self.color, self.rect)
6.新建文件“button.py”,用來定義按鈕類和相應(yīng)方法,本例使用于繪制“PLAY”按鈕;
import pygame.font class Button(): def __init__(self, screen, msg): """初始化按鈕的屬性""" self.screen = screen self.screen_rect = screen.get_rect() # 設(shè)置按鈕的尺寸和其他屬性 self.width, self.height = 100, 30 self.button_color = (216, 30, 6) self.text_color = (255, 255, 255) self.font = pygame.font.SysFont(None, 36) # 創(chuàng)建按鈕的rect對象,并使其居中 self.rect = pygame.Rect(0, 0, self.width, self.height) self.rect.center = self.screen_rect.center # 按鈕的標(biāo)簽只需創(chuàng)建一次 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)
7.新建文件“scoreboard.py”,用來定義記分板,本例使用于繪制左上角飛船(生命數(shù))、頂部中間的“最高分”、右上角的“積分”和“等級”;
import pygame.font from pygame.sprite import Group from player import Player class Scoreboard(): """顯示得分信息的類""" def __init__(self, settings, screen): """初始化顯示得分涉及的屬性""" self.screen = screen self.screen_rect = screen.get_rect() self.settings = settings # 顯示得分信息時使用的字體設(shè)置 self.text_color = (255, 255, 255) self.font = pygame.font.SysFont(None, 30) # 飛船縮放值 self.scaleValue = 20 # 準(zhǔn)備初始得分圖像\最高得分\等級 self.prep_score() self.prep_high_score() self.prep_level() self.prep_players() def prep_score(self): """將得分轉(zhuǎn)換為渲染的圖像""" rounded_score = int(round(self.settings.score, -1)) score_str = '{:,}'.format(rounded_score) self.score_image = self.font.render(score_str, True, self.text_color) # 將得分放在屏幕右上角 self.score_rect = self.score_image.get_rect() self.score_rect.right = self.screen_rect.right -20 self.score_rect.top = 10 def prep_high_score(self): """ 將最高得分轉(zhuǎn)換為渲染的圖像 """ high_score = int(round(self.settings.high_score, -1)) high_score_str = "{:,}".format(high_score) self.high_score_image = self.font.render(high_score_str, True, self.text_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): """將等級轉(zhuǎn)換為渲染的圖像""" self.level_image = self.font.render(str(self.settings.level), True, self.text_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 prep_players(self): """ 顯示還余下多少艘飛船 """ self.players = Group() for player_number in range(self.settings.player_limit): player = Player(self.settings, self.screen) # 縮放球大小并賦值位置 player.scale(self.scaleValue) player.rect.x = 10 + player.rect.width * player_number * 0.5 player.rect.y = self.score_rect.top self.players.add(player) 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.players.draw(self.screen)
8.新建文件“game_functions.py”,存放跟游戲有關(guān)的所有業(yè)務(wù)邏輯函數(shù)(代碼有詳細(xì)的注釋信息);
import sys import pygame from bullet import Bullet from enemy import Enemy import file as f # 事件 def check_events(settings, screen, player, play_button, scoreboard, bullets, fireSound): """ 響應(yīng)按鍵和鼠標(biāo)事件 """ for event in pygame.event.get(): if event.type == pygame.QUIT: save_file(settings) sys.exit() elif event.type == pygame.KEYDOWN: check_keydown_events(event, settings, screen, player, scoreboard, bullets, fireSound) elif event.type == pygame.KEYUP: check_keyup_events(event, player) elif event.type == pygame.MOUSEBUTTONDOWN: mouse_x, mouse_y = pygame.mouse.get_pos() check_play_button(settings, play_button, scoreboard, mouse_x, mouse_y) def check_keydown_events(event, settings, screen, player, scoreboard, bullets, fireSound): """ 響應(yīng)按鍵 """ if event.key == pygame.K_DOWN: player.move_down = True elif event.key == pygame.K_UP: player.move_up = True elif event.key == pygame.K_LEFT: player.move_left = True elif event.key == pygame.K_RIGHT: player.move_right = True elif event.key == pygame.K_SPACE: fireSound.play() # 點擊空格鍵創(chuàng)建一顆子彈 fire_bullet(settings, screen, player, bullets) elif event.key == pygame.K_p: start_game(settings, scoreboard) elif event.key == pygame.K_q: save_file(settings) sys.exit() def check_keyup_events(event, player): """ 響應(yīng)松開 """ if event.key == pygame.K_DOWN: player.move_down = False elif event.key == pygame.K_UP: player.move_up = False elif event.key == pygame.K_LEFT: player.move_left = False elif event.key == pygame.K_RIGHT: player.move_right = False def check_play_button(settings, play_button, scoreboard, mouse_x, mouse_y): """在玩家單擊Play按鈕時開始新游戲""" button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y) if button_clicked and not settings.game_active: start_game(settings, scoreboard) def start_game(settings, scoreboard): """開始游戲""" # 重置游戲設(shè)置 settings.initialize_settings() # 隱藏光標(biāo) pygame.mouse.set_visible(False) # 重置游戲統(tǒng)計信息 settings.reset_stats() settings.game_active = True # 重置記分牌圖像 scoreboard.prep_score() scoreboard.prep_high_score() scoreboard.prep_level() scoreboard.prep_players() def save_file(settings): # 保持文件 obj = {'highScore': settings.high_score} f.save_file(obj, settings.filename) # 敵機 def update_enemies(settings, screen, scoreboard, enemies, enemies_down, enemy_down_imgs, player, bullets, explosiveSound): # 生成敵機,需要控制生成頻率 if settings.enemy_frequency % settings.enemy_frequency_space == 0: enemy1 = Enemy(enemy_down_imgs, settings) enemies.add(enemy1) settings.enemy_frequency += 1 if settings.enemy_frequency >= 100: settings.enemy_frequency = 0 for enemy in enemies: # 移動敵機 enemy.move() # 敵機與玩家飛機碰撞效果處理 兩個精靈之間的圓檢測 if pygame.sprite.collide_circle(enemy, player): enemies_down.add(enemy) enemies.remove(enemy) settings.player_limit -= 1 scoreboard.prep_players() break # 移動出屏幕后刪除飛機 if enemy.rect.top < 0: enemies.remove(enemy) # 敵機被子彈擊中效果處理 # 將被擊中的敵機對象添加到擊毀敵機 Group 中,用來渲染擊毀動畫 # 方法groupcollide()是檢測兩個精靈組中精靈們的矩形沖突 enemies1_down = pygame.sprite.groupcollide(enemies, bullets, True, True) if enemies1_down: explosiveSound.play() # 計算分?jǐn)?shù)并渲染 for enemys in enemies1_down.values(): settings.score += settings.one_points * len(enemys) scoreboard.prep_score() # 渲染最高分 check_high_score(settings, scoreboard) # 等達(dá)到等級數(shù)量升級并渲染新等級 settings.level_number -= 1 if settings.level_number == 0: settings.increase_speed() settings.level += 1 scoreboard.prep_level() # 還原為4(同settings一致) settings.level_number = 10 # 加快生成敵機 if settings.enemy_frequency_space > 10: settings.enemy_frequency_space -= 10 # 遍歷key值 返回的碰撞敵機 for enemy_down in enemies1_down: # 點擊銷毀的敵機到列表 enemies_down.add(enemy_down) # 敵機被子彈擊中效果顯示 for enemy_down in enemies_down: if enemy_down.down_index == 0: pass if enemy_down.down_index > 7: enemies_down.remove(enemy_down) continue #顯示碰撞圖片 screen.blit(enemy_down.down_imgs[enemy_down.down_index // 2], enemy_down.rect) enemy_down.down_index += 1 # 顯示精靈 enemies.draw(screen) # 子彈 def fire_bullet(settings, screen, player, bullets): """創(chuàng)建子彈""" new_bullet = Bullet(settings, screen, player) bullets.add(new_bullet) def update_bullets(screen, bullets): """更新子彈的位置,并刪除已消失的子彈""" # 更新子彈的位置 bullets.update() # 刪除已消失的子彈并同時更新飛船的生命 for bullet in bullets.copy(): if bullet.rect.top < screen.get_rect().top: bullets.remove(bullet) # 分?jǐn)?shù) def check_high_score(settings, scoreboard): """檢查是否誕生了新的最高得分""" if settings.score > settings.high_score: settings.high_score = settings.score scoreboard.prep_high_score() # 屏幕 def update_screen(settings, screen, player, play_button, scoreboard, enemies, bullets): """ 更新屏幕上的圖像,并切換到新屏幕 """ # 繪制飛船到屏幕 player.draw_player() # 繪制子彈到屏幕 for bullet in bullets.sprites(): bullet.draw_bullet() # 渲染記分牌信息 scoreboard.show_score() # if settings.player_limit == 0: settings.game_active = False settings.reset_stats() # 清空矩形列表和子彈列表 enemies.empty() bullets.empty() screen_rect = screen.get_rect() player.rect.centerx = screen_rect.centerx player.rect.bottom = screen_rect.bottom # 如果游戲處于非活動狀態(tài),就繪制 Play 按鈕 if not settings.game_active: play_button.draw_button() # 讓最近繪制的屏幕可見 pygame.display.flip()
9.新建文件shootingenemy.py,主函數(shù)用來初始化程序,并同步更新程序的信息;
import pygame from pygame.sprite import Group from settings import Settings from button import Button from player import Player import game_functions as gf from scoreboard import Scoreboard def run_game(): pygame.init() # 初始化全部音頻,并加載爆炸聲音樂 pygame.mixer.init() # 等待1s pygame.time.delay(1000) pygame.mixer.music.load('file/bgsound.mp3') # -1代表無限循環(huán)(背景音樂) pygame.mixer.music.play(-1) # 爆炸聲 explosiveSound = pygame.mixer.Sound('file/explosiveSound.wav') # 槍聲 fireSound = pygame.mixer.Sound('file/fireSound.wav') # 游戲循環(huán)幀率設(shè)置 clock = pygame.time.Clock() settings = Settings() screen = pygame.display.set_mode((settings.screen_width, settings.screen_height)) # 全屏顯示 # screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) pygame.display.set_caption('飛機大戰(zhàn)') # 左上角圖標(biāo) ic_launcher = pygame.image.load('images/ic_launcher.png').convert_alpha() pygame.display.set_icon(ic_launcher) # 背景圖 background = pygame.image.load('images/background.png').convert_alpha() # 敵機圖片 enemy_img1= pygame.image.load('images/enemy1.png') enemy_img2= pygame.image.load('images/enemy2.png') enemy_img3= pygame.image.load('images/enemy3.png') enemy_img4= pygame.image.load('images/enemy4.png') # 敵機不同狀態(tài)的圖片列表,多張圖片展示為動畫效果 enemy_down_imgs = [] enemy_down_imgs.append(enemy_img1) enemy_down_imgs.append(enemy_img2) enemy_down_imgs.append(enemy_img3) enemy_down_imgs.append(enemy_img4) # 儲存敵機 enemies = Group() # 存儲被擊毀的飛機,用來渲染擊毀動畫 enemies_down = Group() # 創(chuàng)建Play按鈕 play_button = Button(screen, 'Play') # 創(chuàng)建飛船 player = Player(settings, screen) # 創(chuàng)建子彈的編組 bullets = Group() # 創(chuàng)建記分牌 scoreboard = Scoreboard(settings, screen) while True: # 繪制背景 screen.blit(background, (0, 0)) # 控制游戲最大頻率 clock.tick(60) # 檢查玩家輸入(不加會導(dǎo)致一直加載) gf.check_events(settings, screen, player, play_button, scoreboard, bullets, fireSound) if settings.game_active: # 更新飛船位置 player.update() # 更新敵機 gf.update_enemies(settings, screen, scoreboard, enemies, enemies_down, enemy_down_imgs, player, bullets, explosiveSound) # 更新子彈位置 gf.update_bullets(screen, bullets) # 更新屏幕信息 gf.update_screen(settings, screen, player, play_button, scoreboard, enemies, bullets) run_game(),
10.在文件shootingenemy.py目錄路徑下,執(zhí)行命令“python shootingenemy.py”彈出窗口,即可對其操作游玩。
結(jié)語
該游戲加入了背景音樂、射擊聲、子彈射中敵機的爆炸聲和爆炸效果、生命數(shù)、積分、等級、最高分和開始按鈕等元素,大家也可以自行加入其它好玩的元素。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。