python實(shí)現(xiàn)微信打飛機(jī)游戲
本文實(shí)例為大家分享了python實(shí)現(xiàn)微信打飛機(jī)游戲的具體代碼,供大家參考,具體內(nèi)容如下
import pygame import random import sys #初始化 pygame.init() pygame.display.set_caption('飛機(jī)火拼')#設(shè)置窗口標(biāo)題 screen= pygame.display.set_mode((320, 570), 0, 32) pygame.mouse.set_visible(False)#隱藏光標(biāo) #加載圖片 boom1=pygame.image.load('../Game/fly/src/boom1.png') boom2=pygame.image.load("../Game/fly/src/boom2.png") bullet=pygame.image.load('../Game/fly/src/bullet.png') plane=pygame.image.load("../Game/fly/src/plane.png").convert_alpha() enemy=pygame.image.load('../Game/fly/src/enemy.png') #設(shè)置窗口圖標(biāo) pygame.display.set_icon(plane) background=pygame.image.load("../Game/fly/src/bg_01.png") start=pygame.image.load('../Game/fly/src/start.png') pause=pygame.image.load("../Game/fly/src/pause.png") #pygame.mixer.init() #Xexplosion=pygame.mixer.Sound("explosion.mp3") #Xbullet=pygame.mixer.Sound("../Game/fly/sound/bullet.mp3") #XgameOver=pygame.mixer.Sound("../Game/fly/sound/game_over.mp3") #Xexplosion.set_volume(1) #pygame.mixer.music.load("../Game/fly/sound/game_music.mp3") #pygame.mixer.music.play(-1, 0.0) #pygame.mixer.music.set_volume(0.25) i=0 #分?jǐn)?shù) score=0 font=pygame.font.SysFont('微軟雅黑', 36) #子彈 bullets=[] #敵機(jī) enemies=[] #記錄敵機(jī)爆炸位置 boomplace=[] #游戲結(jié)束 gameover=False pygame.mouse.set_pos(200, 200) while True: i += 1 if i > 200: i =0 screen.blit(background, (0, 0)) #通過鼠標(biāo)控制飛機(jī) x, y=pygame.mouse.get_pos() #print(x, y) x -= plane.get_width()/2 y -= plane.get_height()/2 screen.blit(plane, (x, y)) #每25幀 調(diào)用一次 if i%25 == 0: #返回 按下左鍵 中鍵 右鍵 l,m,r=pygame.mouse.get_pressed() if l ==True: #生成子彈位置添加 bullets.append([x+plane.get_width()/2, y]) #Xshoot.play() for place in bullets: #子彈位置超出邊界 if place[1]<=0: bullets.remove(place) #移動(dòng)子彈位置 for place in bullets: place[1] -= 55 #繪制子彈 for place in bullets: screen.blit(bullet,(place[0],place[1])) #敵機(jī)生成 移動(dòng) 消失 if i%199 == 0: enemies.append([random.randint(0,320-enemy.get_width()),-enemy.get_width()/2]) for place in enemies: if place[1] >= 600: enemies.remove(place) for place in enemies: place[1] += 35 for place in enemies: screen.blit(enemy,(place[0],place[1])) #敵機(jī)爆炸 for place in boomplace: if place[2]>0: screen.blit(boom1,(place[0],place[1])) place[2] -=1 #子彈碰撞檢測(cè) for bulletplace in bullets: for enemyplace in enemies: if (bulletplace[0] > enemyplace[0] and bulletplace[0] < enemyplace[0] + enemy.get_width()) and \ (bulletplace[1] > enemyplace[1] and bulletplace[1] < enemyplace[1] + enemy.get_height()): boomflag = 75 enemyplace.append(boomflag) boomplace.append(enemyplace) enemies.remove(enemyplace) bullets.remove(bulletplace) #Sexplosion.play() score += 1 #飛機(jī)碰撞檢測(cè) for enemyplace in enemies: if (x + 0.7*plane.get_width() > enemyplace[0]) and (x + 0.3*plane.get_width()<enemyplace[0]+ enemy.get_width())\ and (y + 0.7*plane.get_height() > enemyplace[1]) and (y + 0.3*plane.get_height()<enemyplace[1]+ enemy.get_height()): enemies.remove(enemyplace) screen.blit(pause,(0,0)) screen.blit(boom2, (x, y)) #for place in bullets: #screen.blit(bullet, (place[0], place[1])) #for place in enemies: #screen.blit(enemy, (place[0], place[1])) text=font.render('Score%d'%score,True,(0,0,0)) screen.blit(text,(200,270)) text=font.render("Right ReStart",True,(0, 0,0)) screen.blit(text,(30,310)) pygame.display.update()#重新繪制窗口 gameover=True while gameover == True and r == False : l, m, r = pygame.mouse.get_pressed() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() #重置游戲 i=0 score=0 bullets=[] enemies=[] boomplace=[] x, y=185-plane.get_width()/2,550 - plane.get_height()/2 #檢測(cè) 暫停 繼續(xù) l,m,r=pygame.mouse.get_pressed() if r == True: screen.blit(background,(0,0)) screen.blit(start,(0,0)) screen.blit(plane,(x,y)) for place in bullets: screen.blit(bullet,(place[0],place[1])) for place in enemies: screen.blit(enemy,(place[0],place[1])) for place in boomplace: if place[2] >0: screen.blit(boom1,(place[0],place[1])) place[2] -=1 text=font.render(u'Score %d'%score,1,(0,0,0)) screen.blit(text,(50,0)) if gameover == True: x,y =185,550 gameover =False text =font.render(' left to start',True,(0,0,0)) screen.blit(text,(35,300)) else: x,y=pygame.mouse.get_pos() text=font.render(' left to continue ',True,(0,0,0)) screen.blit(text,(10,300)) pygame.display.update() while True : for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() l, m, r = pygame.mouse.get_pressed() if l == True: pygame.mouse.set_pos(x,y) break text=font.render(u"%d"%score, True, (0, 0, 0)) screen.blit(text,(50, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit()
效果圖:
更多關(guān)于python游戲的精彩文章請(qǐng)點(diǎn)擊查看以下專題:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python 使用SFTP和FTP實(shí)現(xiàn)對(duì)服務(wù)器的文件下載功能
這篇文章主要介紹了Python 使用SFTP和FTP實(shí)現(xiàn)對(duì)服務(wù)器的文件下載功能,本文通過實(shí)例代碼給大家介紹的非常想詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Python+OpenCV+圖片旋轉(zhuǎn)并用原底色填充新四角的例子
今天小編就為大家分享一篇Python+OpenCV+圖片旋轉(zhuǎn)并用原底色填充新四角的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12詳解如何通過Python實(shí)現(xiàn)批量數(shù)據(jù)提取
每天面對(duì)成堆的發(fā)票,無論是發(fā)票還是承兌單據(jù),抑或是其他各類公司數(shù)據(jù)要從照片、PDF等不同格式的內(nèi)容中提取,我們都有必要進(jìn)行快速辦公的能力提升。本文就教你如何利用Python實(shí)現(xiàn)批量數(shù)據(jù)提取吧2023-03-03配置python連接oracle讀取excel數(shù)據(jù)寫入數(shù)據(jù)庫的操作流程
這篇文章主要介紹了配置python連接oracle,讀取excel數(shù)據(jù)寫入數(shù)據(jù)庫,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03python GUI庫圖形界面開發(fā)之PyQt5工具欄控件QToolBar的詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5工具欄控件QToolBar的詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-02-02Python readline()和readlines()函數(shù)實(shí)現(xiàn)按行讀取文件
本文主要介紹了Python readline()和readlines()函數(shù)實(shí)現(xiàn)按行讀取文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python實(shí)現(xiàn)正弦信號(hào)的時(shí)域波形和頻譜圖示例【基于matplotlib】
這篇文章主要介紹了Python實(shí)現(xiàn)正弦信號(hào)的時(shí)域波形和頻譜圖,涉及Python數(shù)學(xué)運(yùn)算與圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2018-05-05