您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關l利用python3怎么實現(xiàn)一個飛機大戰(zhàn)游戲,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
具體方法如下:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 導入需要使用的模塊 import pygame from pygame.locals import * from sys import exit import random # 設置屏幕大小的變量 SCREEN_WIDTH = 480 SCREEN_HEIGHT = 800 import codecs # 子彈類 class Bullet(pygame.sprite.Sprite): def __init__(self,bullet_img,init_pos): # 實現(xiàn)父類的初始化方法 pygame.sprite.Sprite.__init__(self) self.image = bullet_img self.rect = self.image.get_rect() self.rect.midbottom = init_pos self.speed = 10 def move(self): self.rect.top -= self.speed # 玩家飛機類 class Player(pygame.sprite.Sprite): def __init__(self,plane_img,player_rect,init_pos): pygame.sprite.Sprite.__init__(self) self.image=[] for i in range(len(player_rect)): self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha()) self.rect = player_rect[0] self.rect.topleft = init_pos self.speed = 8 self.bullets = pygame.sprite.Group() #玩家飛機發(fā)射子彈的集合 self.img_index = 0 self.is_hit = False # 發(fā)射子彈 def shoot(self,bullet_img): bullet = Bullet(bullet_img,self.rect.midtop) self.bullets.add(bullet) # 將子彈放入玩家飛機的子彈集合 # 向上移動 def moveUp(self): if self.rect.top <= 0: self.rect.top = 0 else: self.rect.top -= self.speed # 向下移動 def moveDown(self): if self.rect.top >= SCREEN_HEIGHT - self.rect.height: self.rect.top = SCREEN_HEIGHT - self.rect.height else: self.rect.top += self.speed # 向左移動 def moveLeft(self): if self.rect.left <= 0: self.rect.left = 0 else: self.rect.left -= self.speed # 向右移動 def moveRight(self): if self.rect.left >= SCREEN_WIDTH - self.rect.width: self.rect.left = SCREEN_WIDTH - self.rect.width else: self.rect.left += self.speed # 敵機類 class Enemy(pygame.sprite.Sprite): # 飛機的圖片 敵機墜毀的圖片 敵機的位置 def __init__(self,enemy_img,enemy_down_imgs,init_pos): pygame.sprite.Sprite.__init__(self) self.image = enemy_img self.rect = self.image.get_rect() self.rect.topleft = init_pos self.down_imgs = enemy_down_imgs self.speed = 2 self.down_index = 0 # 移動 def move(self): self.rect.top += self.speed # 對文件的操作 # 寫入文本 # 要寫入的內(nèi)容,寫入方式,寫入文件所在的位置 def write_txt(contert, strim, path): f = codecs.open(path,strim, 'utf8') f.write(str(contert)) f.close() # 讀取文本 def read_txt(path): with open(path,'r',encoding='utf8') as f: lines = f.readlines() return lines # 初始化pygame pygame.init() # 設置游戲界面的大小,背景圖片,標題 # 界面startGame( screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT)) # 標題 pygame.display.set_caption('飛機大戰(zhàn)') # 圖標 ic_launcher = pygame.image.load('resources/image/ic_launcher.png').convert_alpha() pygame.display.set_icon(ic_launcher) # 背景圖 background = pygame.image.load('resources/image/background.png').convert() # 游戲結束 game_over = pygame.image.load('resources/image/gameover.png') # 飛機及子彈的圖片 plane_img = pygame.image.load('resources/image/shoot.png') def startGame(): # 1.設置玩家飛機不同狀態(tài)的圖片列表,多張圖片展示為動畫效果 player_rect = [] # 玩家飛機的圖片 player_rect.append(pygame.Rect(0,99,102,126)) player_rect.append(pygame.Rect(165,360,102,126)) # 玩家飛機爆炸的圖片 player_rect.append(pygame.Rect(165,234,102,126)) player_rect.append(pygame.Rect(330,634,102,126)) player_rect.append(pygame.Rect(330,498,102,126)) player_rect.append(pygame.Rect(432,624,102,126)) player_pos = [200,600] # 生成玩家飛機類 player = Player(plane_img,player_rect,player_pos) # 加入子彈的圖片 bullet_rect = pygame.Rect(69,77,10,21) bullet_img = plane_img.subsurface(bullet_rect) # 加入敵機圖片 enemy1_rect = pygame.Rect(534,612,57,43) #沒有爆炸前的圖片 enemy1_img = plane_img.subsurface(enemy1_rect) enemy1_down_imgs = [] #飛機銷毀后的圖片 enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267,347,57,43))) enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(873,679,57,43))) enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267,296,57,43))) enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(930,697,57,43))) # 存儲敵機的集合 enmies1 = pygame.sprite.Group() # 存儲被擊毀的敵機的集合 enemies_down = pygame.sprite.Group() # 初始子彈射擊頻率 shoot_frequency = 0 # 初始化敵機生成頻率 enemy_frequency = 0 # 玩家飛機被擊中后的效果處理 player_down_index = 16 # 設置游戲的幀數(shù) clock = pygame.time.Clock() # 初始化成績 score = 0 # 判斷循環(huán)結束的參數(shù) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() screen.fill(0) screen.blit(background,(0,0)) clock.tick(60) # 生成子彈 判斷玩家有沒有被擊中 if not player.is_hit: if shoot_frequency % 15 == 0: player.shoot(bullet_img) shoot_frequency += 1 if shoot_frequency >= 15: shoot_frequency = 0 for bullet in player.bullets: bullet.move() if bullet.rect.bottom<0: player.bullets.remove(bullet) # 顯示子彈 player.bullets.draw(screen) # 生成敵機,..需要控制頻率 if enemy_frequency % 50 == 0: #生成隨機的位置 enemy1_pos = [random.randint(0,SCREEN_WIDTH-enemy1_rect.width),0] # 初始化敵機 enemy1 = Enemy(enemy1_img,enemy1_down_imgs,enemy1_pos) # 存儲到集合中 enmies1.add(enemy1) enemy_frequency += 1 # 敵機生成到 100 則重新循環(huán) if enemy_frequency >= 100: enemy_frequency = 0 # 敵機的移動 for enemy in enmies1: enemy.move() # 敵機與玩家碰撞效果處理 if pygame.sprite.collide_circle(enemy,player): # pygame判定是否相撞的方法 enemies_down.add(enemy) # 將敵機加入到墜毀的集合中 enmies1.remove(enemy) # 從敵機集合中移除 player.is_hit = True break # 移動出屏幕的敵機 if enemy.rect.top < 0: enmies1.remove(enemy) # 與子彈碰撞 enemies1_down = pygame.sprite.groupcollide(enmies1,player.bullets,1,1) for enemy_down in enemies1_down: enemies_down.add(enemy_down) # 繪制玩家飛機 if not player.is_hit: screen.blit(player.image[player.img_index],player.rect) # 實現(xiàn)飛機動效 player.img_index = shoot_frequency // 8 else: # 玩家飛機被擊毀后的動畫效果 player.img_index = player_down_index // 8 screen.blit(player.image[player.img_index],player.rect) player_down_index += 1 if player_down_index > 47: running = False # 敵機被擊中的效果 for enemy_down in enemies_down: if enemy_down.down_index == 0: pass if enemy_down.down_index > 7: enemies_down.remove(enemy_down) score += 100 continue # 繪制碰撞動畫 screen.blit(enemy_down.down_imgs[enemy_down.down_index // 2],enemy_down.rect) enemy_down.down_index += 1 # 顯示敵機 enmies1.draw(screen) # 繪制當前得分 score_font = pygame.font.Font(None,36) score_text = score_font.render(str(score),True,(128,128,128)) text_rect = score_text.get_rect() text_rect.topleft = [10,10] screen.blit(score_text,text_rect) # 獲取鍵盤的輸入 key_pressed = pygame.key.get_pressed() if key_pressed[K_UP] or key_pressed[K_w]: player.moveUp() if key_pressed[K_DOWN] or key_pressed[K_s]: player.moveDown() if key_pressed[K_LEFT] or key_pressed[K_a]: player.moveLeft() if key_pressed[K_RIGHT] or key_pressed[K_d]: player.moveRight() pygame.display.update() # 繪制游戲結束畫面 screen.blit(game_over,(0,0)) # 繪制Game Over顯示最終分數(shù) font = pygame.font.Font(None,48) text = font.render("Score:"+str(score),True,(255,0,0)) text_rect = text.get_rect() text_rect.centerx = screen.get_rect().centerx # x軸位置 text_rect.centery = screen.get_rect().centery + 24 # y軸位置 screen.blit(text,text_rect) # 使用字體 xtfont = pygame.font.SysFont("jamrul",30) # 繪制重新開始按鈕 textstart = xtfont.render('Start',True,(255,0,0)) text_rect = textstart.get_rect() text_rect.centerx = screen.get_rect().centerx # x軸位置 text_rect.centery = screen.get_rect().centery + 120 # y軸位置 screen.blit(textstart,text_rect) # 排行榜按鈕 textstart = xtfont.render('Ranking',True,(255,0,0)) text_rect = textstart.get_rect() text_rect.centerx = screen.get_rect().centerx # x軸位置 text_rect.centery = screen.get_rect().centery + 180 # y軸位置 screen.blit(textstart,text_rect) # 判斷得分更新排行榜 # 臨時變量 j = 0 # 讀取文件 arrayscore = read_txt(r'score.txt')[0].split('mr') # 循環(huán)分數(shù)列表在列表里排序 for i in range(0,len(arrayscore)): if score > int(arrayscore[i]): # 大于排行榜上的內(nèi)容 把分數(shù)和當前分數(shù)進行替換 j = arraysco.re[i] arrayscore[i] = str(score) score = 0 # 替換下來的分數(shù)移動一位 if int(j) > int(arrayscore[i]): k = arrayscore[i] arrayscore[i] = str(j) j = k # 循環(huán)分數(shù)列表 寫入文檔 for i in range(0,len(arrayscore)): # 判斷列表的第一個分數(shù) if i == 0: write_txt(arrayscore[i]+'mr','w',r'score.txt') else: # 判斷是否是最后一個 if (i==9): # 最近添加內(nèi)容最后一個分數(shù)不加 mr write_txt(arrayscore[i],'a',r'score.txt') else: # 不是最后一個分數(shù),添加的時候加 mr write_txt(arrayscore[i]+'mr','a',r'score.txt') # 定義排行榜函數(shù) def gameRanking(): # 繪制背景圖片 screen2 = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT)) screen2.fill(0) screen2.blit(background,(0,0)) # 使用系統(tǒng)字體 xtfont = pygame.font.SysFont('jamrul',30) # 1.繪制標題 textstart = xtfont.render('Ranking',True,(255,0,0)) text_rect = textstart.get_rect() text_rect.centerx = screen.get_rect().centerx # x軸位置 text_rect.centery = 50 # y軸位置 screen.blit(textstart,text_rect) # 2.繪制重新開始按鈕 textstart = xtfont.render('Start',True,(255,0,0)) text_rect = textstart.get_rect() text_rect.centerx = screen.get_rect().centerx # x軸位置 text_rect.centery = screen.get_rect().centery + 120 # y軸位置 screen.blit(textstart,text_rect) # 3.展示排行榜的數(shù)據(jù) arrayscore = read_txt(r'score.txt')[0].split('mr') for i in range(0,len(arrayscore)): font = pygame.font.Font(None,48) # 編寫排名 k = i+1 text = font.render(str(k) + " "+arrayscore[i],True,(255,0,0)) text_rect = text.get_rect() text_rect.centerx = screen2.get_rect().centerx text_rect.centery = 80 + 30*k # 繪制分數(shù) screen2.blit(text,text_rect) startGame() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() # 監(jiān)控鼠標的點擊 elif event.type == pygame.MOUSEBUTTONDOWN: # 判定重新開始范圍 if screen.get_rect().centerx - 70 <= event.pos[0]\ and event.pos[0] <= screen.get_rect().centerx + 50\ and screen.get_rect().centery + 100 <= event.pos[1]\ and screen.get_rect().centery + 140 >= event.pos[1]: startGame() # 判定排行榜范圍 if screen.get_rect().centerx - 70 <= event.pos[0]\ and event.pos[0] <= screen.get_rect().centerx + 50\ and screen.get_rect().centery + 160 <= event.pos[1]\ and screen.get_rect().centery + 200 >= event.pos[1]: gameRanking() pygame.display.update()
以上就是l利用python3怎么實現(xiàn)一個飛機大戰(zhàn)游戲,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。