如何利用pygame實現打飛機小遊戲
效果預覽
最近上實訓課,寫瞭這麼一個簡單的小玩意。運行效果如下:(這個是有音效的,不過這個展示不瞭因為這裡隻能上傳GIF)
項目結構
遊戲對屏幕的適配
由於我使用的是筆記本所以對於屏幕來說是進行瞭縮放的,例如,我的筆記本縮放瞭125%
但是問題在於我們的pygame和其他的一些庫例如selenium其實是按照100%顯示的像素來算的。所以這個時候我們需要進行一個換算。
這個也好算: 當前顯示像素比 = 100%顯示像素比 X 縮放比
我們隻需要換算一下就好瞭。這裡我定義瞭一個類,來實現我們的需求,自動檢測我們的電腦的屏幕縮放比,之後換算。
from win32 import win32api, win32gui, win32print from win32.lib import win32con from win32.win32api import GetSystemMetrics class ChangeRealSize(object): ''' 該類主要對屏幕進行像素適配,按照縮放比對像素進行換算為100%顯示 示例: RealSize = ChangeRealSize() x=RealSize.getreal_xy(500) 此時就可以換算為當前屏幕的像素 ''' def get_real_resolution(self): """獲取真實的分辨率""" hDC = win32gui.GetDC(0) w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES) h = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES) return w, h def get_screen_size(self): """獲取縮放後的分辨率""" w = GetSystemMetrics (0) h = GetSystemMetrics (1) return w, h def getreal_xy(self,x): '''返回按照100%來算的真實的像素值''' real_resolution = self.get_real_resolution() screen_size = self.get_screen_size() screen_scale_rate = round(real_resolution[0] / screen_size[0], 2) try: x = x/screen_scale_rate except: #對筆記本進行適配,一般而言在100%比的機器上x不會出錯 x=1.25 return int(x)
遊戲屏幕的繪制與飛機創建
屏幕繪制直接使用pygame.display.set_mode()的bitl()繪制方法,進行繪制。當然這裡的背景是會動的。所以使用到瞭一個精靈的派生類。
import pygame,random from ChangeRealSize import ChangeRealSize GetReal = ChangeRealSize() class GameSprite(pygame.sprite.Sprite): def __init__(self,image_path,speed=1): super().__init__() self.image = pygame.image.load(image_path) self.rect = self.image.get_rect() self.speed = speed def update(self): self.rect.y+=self.speed class Background(GameSprite): def __init__(self, image_path="./Plane_Img/background1.png",flag=False): super().__init__(image_path) if flag: self.rect.y = -self.rect.height def update(self): self.rect.y+=1 if self.rect.y >= self.rect.height: self.rect.y = -self.rect.height
實現的具體方法如下:
飛機類的實現
這個我是自己定義的所以的話,沒有辦法直接使用那個自帶的碰撞檢測,我還定義瞭一個碰撞檢測方法。
飛機的移動
這個和遊戲類的事件檢測配合。
具體思路是用pygame.event.get()進行事件檢測。之後檢測到按下某一個按鍵,例如向前移動是,就會設置向前移動的信號,那麼這個時候飛機就會一直往前走。當松開後,那麼設置信號為假,那麼飛機就會停下來。由於飛機會一直在循環裡面檢測有沒有按下那個向前,所以當你長按往前時,飛機會一直往前,直到你松開。
子彈與敵機類
這個子彈和敵機都是精靈派生類的子類。所以的話就一起說一下。
重點要說的時子彈類的碰撞檢測,和敵機的碰撞檢測。這裡主要說一下子彈類,因為這個和敵機類似。隻是有些細節不一樣。
class Bullet(GameSprite): def __init__(self,P_rect,is_hero=False,bullet_image = "./Plane_Img/bullet-3.gif",hero_rect=None): self.hero_rect = hero_rect self.bullet_image = bullet_image self.speed = 4 self.is_hero = is_hero self.P_rect = P_rect self.screen_height = GetReal.getreal_xy(800) self.screen_width = GetReal.getreal_xy(500) self.actarct_plan = False self.actract_hero=[] super().__init__(self.bullet_image,self.speed) self.rect.x = self.P_rect.x+((self.P_rect.width-self.rect.width)/2) self.rect.y = self.P_rect.y def enemy_bullet(self): #可以在這裡計算飛機被擊中瞭多少次 #被擊中減少5點 if not self.is_hero: bullet_x = self.rect.x + int(self.rect.width / 2) bullet_y = self.rect.y + int(self.rect.height / 2) hero_plane_x = self.hero_rect.x + int(self.hero_rect.width / 2) hero_plane_y = self.hero_rect.y + int(self.hero_rect.height / 2) subtract_y = abs(int(bullet_y - hero_plane_y)) subtract_x = abs(int(bullet_x - hero_plane_x)) if subtract_y <= int((self.rect.height + self.hero_rect.height) / 2) and \ subtract_x <= int((self.rect.width + self.hero_rect.height) / 2): self.actract_hero.append(1) return True def update(self): if self.enemy_bullet(): #直接在這裡計算數字一次減少5 global HERO_PLANE_HP HERO_PLANE_HP-=5 # print(HERO_PLANE_HP) self.kill() if self.is_hero: self.rect.y-=self.speed else: super().update() if self.rect.bottom >=self.screen_height-3 : self.kill()
子彈的話分兩種,一個是飛機子彈,一個是敵機子彈,敵機的自帶檢測碰撞。一方面是方便分數統計(敵機擊中飛機幾次)瞭另一方面也是因為飛機是自定義的沒有辦法用pygame的事件檢測(自帶的)
敵機的爆炸
這個其實又和飛機的爆炸類似。
主要是檢測有沒有撞到飛機,之後通過切換圖片就好瞭。當然這個時候我是開瞭線程的。不然會很快閃過去,換瞭和沒換一樣你看不到效果。
切換圖片的函數,切換完畢,刪除這個敵機類減少內存消耗
遊戲小彩蛋
這個其實就是一個自己的後面
具體作用就是修改自己的飛機的HP值為99萬
當然這個小遊戲還沒有做完,無敵也有點無聊,玩久瞭的話。
def CheatEngine(): global HERO_PLANE_HP print("HP is %d" % HERO_PLANE_HP) while 1: key = input("maybe you can input something:") if key=='break': print("enjoy your game please Bye~") return elif key=="Huterox is best": HERO_PLANE_HP=999999 print("Now your HP is %d!!!"%HERO_PLANE_HP) return
完整代碼
import pygame,random from ChangeRealSize import ChangeRealSize GetReal = ChangeRealSize() class GameSprite(pygame.sprite.Sprite): def __init__(self,image_path,speed=1): super().__init__() self.image = pygame.image.load(image_path) self.rect = self.image.get_rect() self.speed = speed def update(self): self.rect.y+=self.speed class Background(GameSprite): def __init__(self, image_path="./Plane_Img/background1.png",flag=False): super().__init__(image_path) if flag: self.rect.y = -self.rect.height def update(self): self.rect.y+=1 if self.rect.y >= self.rect.height: self.rect.y = -self.rect.height
········································································································
import pygame,random,time,threading,os from ChangeRealSize import ChangeRealSize from GameSprite import GameSprite,Background GetReal = ChangeRealSize() GREATE_ENMEY_EVENT = pygame.USEREVENT#隻能出現一次 HERO_FIRE_BULLTE = pygame.USEREVENT+1 #第二個事件 HERO_PLANE_HP = 300 def CheatEngine(): global HERO_PLANE_HP print("HP is %d" % HERO_PLANE_HP) while 1: key = input("maybe you can input something:") if key=='break': print("enjoy your game please Bye~") return elif key=="Huterox is best": HERO_PLANE_HP=999999 print("Now your HP is %d!!!"%HERO_PLANE_HP) return class Base(): '''飛機的初始化樣式,位置''' def __init__(self,x,y,width,height,path): self.x = GetReal.getreal_xy(x) self.y = GetReal.getreal_xy(y) self.width = GetReal.getreal_xy(width) self.height = GetReal.getreal_xy(height) self.image = pygame.image.load(path) self.rect = pygame.Rect(self.x,self.y,self.height,self.width) class Planer(Base): def __init__(self,x,y,width,height,path,screen): Base.__init__(self,x,y,width,height,path) self.Killed = False self.screen = screen self.GoStrange=False self.TurnLeft = False self.TurnRight =False self.GoBack = False self.Fire_flag = False self.Boom_path="./Plane_Img/hero_blowup_n{}.png" self.bullet_group=pygame.sprite.Group() pygame.time.set_timer(HERO_FIRE_BULLTE,250) self.Plane_need_Killed=[]#由於會重復執行隻能去用列表的數量來判斷 def Move(self): if self.Killed: self.rect=pygame.Rect(0,0,0,0) return if self.GoStrange: if self.rect.bottom <= 300: self.show() return else: self.rect.y -= 3 self.show() if self.TurnLeft: if self.rect.x<=3: self.show() return else: self.rect.x-=2 self.show() if self.TurnRight: if self.rect.x>=Game.screen_x-self.rect.width-3: self.show() return else: self.rect.x+=2 self.show() if self.GoBack: if self.rect.bottom>=Game.screen_y-30: self.show() return else: self.rect.y +=2 self.show() self.show() def Get_bullet(self): #子彈加載 if self.Killed: return if self.Fire_flag: MusicPlay().PlayPlanSound() bullet = Bullet(self.rect,True) self.bullet_group.add(bullet) def Fire(self): #子彈發射 if self.Killed: return self.bullet_group.update() self.bullet_group.draw(self.screen) def Goal(self): pass def show(self): if self.Killed: return Game.screen.blit(self.image,self.rect) # pygame.display.update() def __plane_Boom(self): if self.Killed: return for i in range(1, 5): self.image_path = self.Boom_path.format(i) self.image = pygame.image.load(self.image_path) time.sleep(0.3) time.sleep(1) self.Killed =True def Plane_Live(self): if self.Killed: return global HERO_PLANE_HP if HERO_PLANE_HP<=0: HERO_PLANE_HP =0 self.Plane_need_Killed.append(1) if len(self.Plane_need_Killed)==1: t = threading.Thread(target=self.__plane_Boom) t.start() #音樂播放類 class MusicPlay(): def __init__(self): self.bgmusic = pygame.mixer.music def PlayBg(self): self.bgmusic.load("./music/PlaneWarsBackgroundMusic.mp3") self.bgmusic.set_volume(0.3) self.bgmusic.play(-1) def StarBg(self): self.bgmusic.stop() def PlayPlanSound(self): self.PlayPlaneMusic = pygame.mixer.Sound("./music/hero_fire.wav") self.PlayPlaneMusic.set_volume(0.2) self.PlayPlaneMusic.play() def StopPlayPlanSound(self): pass #子彈類 class Bullet(GameSprite): def __init__(self,P_rect,is_hero=False,bullet_image = "./Plane_Img/bullet-3.gif",hero_rect=None): self.hero_rect = hero_rect self.bullet_image = bullet_image self.speed = 4 self.is_hero = is_hero self.P_rect = P_rect self.screen_height = GetReal.getreal_xy(800) self.screen_width = GetReal.getreal_xy(500) self.actarct_plan = False self.actract_hero=[] super().__init__(self.bullet_image,self.speed) self.rect.x = self.P_rect.x+((self.P_rect.width-self.rect.width)/2) self.rect.y = self.P_rect.y def enemy_bullet(self): #可以在這裡計算飛機被擊中瞭多少次 #被擊中減少5點 if not self.is_hero: bullet_x = self.rect.x + int(self.rect.width / 2) bullet_y = self.rect.y + int(self.rect.height / 2) hero_plane_x = self.hero_rect.x + int(self.hero_rect.width / 2) hero_plane_y = self.hero_rect.y + int(self.hero_rect.height / 2) subtract_y = abs(int(bullet_y - hero_plane_y)) subtract_x = abs(int(bullet_x - hero_plane_x)) if subtract_y <= int((self.rect.height + self.hero_rect.height) / 2) and \ subtract_x <= int((self.rect.width + self.hero_rect.height) / 2): self.actract_hero.append(1) return True def update(self): if self.enemy_bullet(): #直接在這裡計算數字一次減少5 global HERO_PLANE_HP HERO_PLANE_HP-=5 # print(HERO_PLANE_HP) self.kill() if self.is_hero: self.rect.y-=self.speed else: super().update() if self.rect.bottom >=self.screen_height-3 : self.kill() #敵機類 class Enemy(GameSprite): def __init__(self,hero_plane,screen,image_path="./Plane_Img/enemy0.png"): self.speed = random.randint(1,3) self.image_path =image_path self.screen = screen self.hero_plane = hero_plane self.hero_bullet = self.hero_plane.bullet_group self.fire_interval = False #通過這個和另一個計時線程配合來實現子彈的間斷發射 self.collied_with_plan = [] super().__init__(self.image_path,self.speed) self.screen_height = GetReal.getreal_xy(800) self.screen_width = GetReal.getreal_xy(500) self.Turn_L_Flag = True self.Boom_path = "./Plane_Img/enemy0_down{}.png" self.rect.y = GetReal.getreal_xy(self.rect.top-self.rect.bottom) self.rect.x = random.randint(0, self.screen_width - self.rect.width) self.bullet_group = pygame.sprite.Group() def __Boom(self): #這裡還可以對以後飛機擊落敵機的數量計數 #敵機應該檢測自己有沒有和飛機的子彈相撞 flag_killed_by_bullet = pygame.sprite.spritecollide(self,self.hero_bullet,True) if flag_killed_by_bullet or self.__IS_collied_with_plan(): #被用戶撞瞭HP值減少20 global HERO_PLANE_HP if len(self.collied_with_plan)==1: HERO_PLANE_HP -=20 t = threading.Thread(target=self.Boom_ing) t.start() pass def Boom_ing(self): for i in range(1, 5): self.image_path = self.Boom_path.format(i) self.image = pygame.image.load(self.image_path) time.sleep(0.2) self.kill() def __IS_collied_with_plan(self): #碰撞檢查是否與用戶飛機碰撞 center_enemy_x = self.rect.x +int(self.rect.width/2) center_enemy_y = self.rect.y +int(self.rect.height/2) center_plane_x = self.hero_plane.rect.x+int(self.hero_plane.rect.width/2) center_plane_y = self.hero_plane.rect.y+int(self.hero_plane.rect.height/2) subtract_y = abs(int(center_enemy_y-center_plane_y)) subtract_x = abs(int(center_enemy_x-center_plane_x)) if subtract_y <= int((self.rect.height+self.hero_plane.rect.height)/2) and\ subtract_x <= int((self.rect.width+self.hero_plane.rect.height)/2): self.collied_with_plan.append(1) return True def __Bullet_building(self): if self.rect.y% 50 ==0: buttle = Bullet(self.rect,bullet_image="./Plane_Img/bullet-1.gif",hero_rect=self.hero_plane.rect) self.bullet_group.add(buttle) def __Shut(self): self.bullet_group.update() self.bullet_group.draw(self.screen) def update(self): super().update() #定義敵機的出現 self.__Boom()#監聽是否碰撞和子彈被射擊 self.__Bullet_building() self.__Shut() if self.rect.top >= self.screen_height + 3: #//越界判斷 # self.rect.y = -20 self.kill() if self.Turn_L_Flag: self.rect.x += random.randint(1,2) if self.rect.right >= self.screen_width - 3: self.Turn_L_Flag = False else: self.rect.x -= random.randint(2, 3) if self.rect.left <= 3: self.Turn_L_Flag = True class PlayGame(object): def __init__(self): pygame.init() self.screen_x, self.screen_y = GetReal.getreal_xy(500), GetReal.getreal_xy(800) self.screen = pygame.display.set_mode((self.screen_x, self.screen_y)) self.Flush_Clcok = pygame.time.Clock() pygame.display.set_caption('英雄無敵!!!') self.enemy_group = pygame.sprite.Group() self.hero_palne = Planer(200, 500, 100, 125, "./Plane_Img/hero1.png", self.screen) pygame.time.set_timer(GREATE_ENMEY_EVENT,1000)#綁定常量事件 def __game_over(self): global HERO_PLANE_HP if self.hero_palne.Killed: while True: bye = pygame.image.load("./Plane_Img/gameover_.png") self.screen.blit(bye,(0,0)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() os._exit(0) def __Listening_keyboard(self,hero_palne): '''鍵盤按鍵事件偵聽''' '''hero_palne部分是偵聽用戶飛機的 其餘的是其他的事件偵聽 ''' for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() os._exit(0) elif event.type == pygame.KEYDOWN: #檢測鍵盤按下 if event.key == pygame.K_w or event.key == pygame.K_UP: hero_palne.GoStrange = True if event.key == pygame.K_a or event.key == pygame.K_LEFT: hero_palne.TurnLeft = True if event.key == pygame.K_d or event.key == pygame.K_RIGHT: hero_palne.TurnRight = True if event.key == pygame.K_s or event.key == pygame.K_DOWN: hero_palne.GoBack = True if event.key == pygame.K_SPACE: hero_palne.Fire_flag= True if event.type == HERO_FIRE_BULLTE: hero_palne.Get_bullet() elif event.type == pygame.KEYUP: # 檢測鍵盤松開 if event.key == pygame.K_w or event.key == pygame.K_UP: hero_palne.GoStrange = False elif event.key == pygame.K_a or event.key == pygame.K_LEFT: hero_palne.TurnLeft = False elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: hero_palne.TurnRight = False elif event.key == pygame.K_s or event.key == pygame.K_DOWN: hero_palne.GoBack = False if event.key == pygame.K_SPACE: hero_palne.Fire_flag = False #敵機出現偵聽 elif event.type == GREATE_ENMEY_EVENT: self.__Enemy_init() def __doc__(self): pass def __BackGround_init(self): bg1 = Background() bg2 = Background(flag=True) self.back_ground = pygame.sprite.Group(bg1,bg2) def __ShowBackGround(self): self.back_ground.update() self.back_ground.draw(self.screen) def __Enemy_init(self): #臨時的東西 enemy = Enemy(self.hero_palne,self.screen) self.enemy_group.add(enemy) def __Show_enemy(self): if self.enemy_group: self.enemy_group.update() self.enemy_group.draw(self.screen) def __Check_planecollied_enemy(self): pass def star_game(self): PlayerMusic = MusicPlay() PlayerMusic.PlayBg() hero_palne = Planer(200, 500, 100, 125, "./Plane_Img/hero1.png", self.screen) self.__BackGround_init()#加載背景 self.__Enemy_init()#加載敵機 while 1: self.__ShowBackGround() self.__Show_enemy() self.__Listening_keyboard(self.hero_palne ) self.hero_palne.Move() self.hero_palne.Fire() self.hero_palne.Plane_Live() self.__game_over() pygame.display.update() # 敲黑板這個方法最好隻出現一次,就在你的遊戲主循環裡面實現 self.Flush_Clcok.tick(60) if __name__ == '__main__': t = threading.Thread(target=CheatEngine) t.start() Game = PlayGame() Game.star_game()
項目獲取
(不會玩git的痛苦!!!)
點擊這裡下載
總結
到此這篇關於如何利用pygame實現打飛機小遊戲的文章就介紹到這瞭,更多相關pygame打飛機小遊戲內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Python+Pygame實戰之實現小蜜蜂歷險記遊戲
- Pygame庫200行代碼實現簡易飛機大戰
- python第三方庫pygame的使用詳解
- Python遊戲開發之精靈和精靈組
- Pygame實戰之實現經典外星人遊戲