Python+Pygame實現(xiàn)簡單的射擊小游戲
前言
哈嘍!哈嘍。栗子上線啦~
要說什么游戲能夠獲得大家的喜愛?
唯射擊游戲莫屬。此前大火手游的《刺激戰(zhàn)場》當(dāng)然現(xiàn)在是叫做《和平精英》啦,想當(dāng)初我也是第一批下載的老玩家了!射擊游戲加上豐富的地圖不同的體驗?zāi)鞘窍喈?dāng)?shù)挠腥ず猛鎯骸?/p>
玩家在射擊游戲中,通過瞄準(zhǔn),擊殺敵人,能夠獲得及時的爽感反饋。射擊游戲很早就在游戲圈占據(jù)一席之地啦~
今天的游戲代碼靈感就是來源于此哦,簡約簡約,大制作小編一個程序員頭禿也搞不了啦。
本期來為大家推薦一款Python游戲代碼版本的“射擊游戲”——小編取名叫《無敵狙擊手》,也就是簡約的射擊游戲,看一下這款代碼你喜歡嘛?
一個適合零基礎(chǔ)開發(fā)游戲的小白案例啦!超級簡單呢
游戲玩法:點擊鼠標(biāo)左鍵對準(zhǔn)移動的靶子,射中一次看距離加分。射中最中間位置一次+3分哦
一、運行環(huán)境
1)環(huán)境安裝
Python3、 Pycharm 、Pygame模塊部分自帶模塊就不展示啦。
第三方庫的安裝:pip install pygame 或者
帶鏡像源 pip install -i https://pypi.douban.com/simple/ +模塊名
2)素材(圖片、音樂等)
二、代碼展示
主程序
import pygame,os,random from pygame.locals import * from pygame.sprite import * def load_image(name): fullname=os.path.join(os.path.join(os.path.split(os.path.abspath(__file__))[0],"filedata"),name) image=pygame.image.load(fullname) return image def load_sound(name): fullname=os.path.join(os.path.join(os.path.split(os.path.abspath(__file__))[0],"filedata"),name) sound=pygame.mixer.Sound(fullname) return sound WIDTH=700 HEIGHT=600 class Explosion(Sprite): def __init__(self,screen,posrect): super(Explosion,self).__init__() self.screen=screen self.posrect=posrect self.image=load_image("explosion.png") self.rect=self.image.get_rect() self.rect=self.posrect self.rates=0 def update(self): self.rates+=1 if self.rates>=40: self.kill() class Enemy(Sprite): def __init__(self,screen): super(Enemy,self).__init__() self.screen=screen self.screenrect=self.screen.get_rect() self.image=load_image("eatingfood.png") self.rect=self.image.get_rect() self.rect.center=(random.uniform(50,WIDTH-50), random.uniform(50,HEIGHT-50)) self.xspeed=random.uniform(1,2) self.yspeed=random.uniform(1,2) if random.choice([True,False]): self.xspeed=-self.xspeed if random.choice([True,False]): self.yspeed=-self.yspeed def update(self): self.rect.centerx+=self.xspeed self.rect.centery+=self.yspeed if self.rect.top>self.screenrect.height or self.rect.bottom<0: self.kill() elif self.rect.left>self.screenrect.width or self.rect.right<0: self.kill() def initmain(): pygame.init() screen=pygame.display.set_mode((WIDTH,HEIGHT)) pygame.display.set_caption("") gameFont=pygame.font.SysFont("宋體",26,True) rates=0 score=0 fpstime=pygame.time.Clock() cursor=load_image("aimcursor.png") cursorrect=cursor.get_rect() pygame.mouse.set_visible(False) enemys=Group() explosions=Group() sou=load_sound("sou.mp3") duang=load_sound("duang.mp3") while True: fpstime.tick(70) screen.fill((128,128,128)) screen.blit(gameFont.render("Score: "+str(score),True,(0,0,0)),(2,2)) rates+=1 enemys.draw(screen) enemys.update() explosions.draw(screen) explosions.update() if rates%25==0: enemys.add(Enemy(screen)) cursorrect.center=pygame.mouse.get_pos() screen.blit(cursor,cursorrect) for event in pygame.event.get(): if event.type==QUIT: pygame.quit() __import__("sys").exit() elif event.type==MOUSEBUTTONDOWN and event.button==1: cld=[] for sf in enemys: if sf.rect.collidepoint(event.pos): sf.kill() score+=int(abs(sf.xspeed)+abs(sf.yspeed)) explosions.add(Explosion(screen,sf.rect.center)) cld.append(True) else: cld.append(False) if not any(cld): sou.play() else: duang.play() pygame.display.flip() if __name__=="__main__": initmain()
?三、效果展示
都是靜態(tài)的截圖展示,科能效果不是很好!動態(tài)的展示我就沒錄制視頻了哈,大家拿代碼自己玩兒哦~
1)游戲界面
2)擊中效果
3)+3分
到此這篇關(guān)于Python+Pygame實現(xiàn)簡單的射擊小游戲的文章就介紹到這了,更多相關(guān)Python Pygame射擊游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django分頁查詢并返回jsons數(shù)據(jù)(中文亂碼解決方法)
這篇文章主要介紹了Django分頁查詢并返回jsons數(shù)據(jù)(中文亂碼解決方法),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Python面向?qū)ο蟪绦蛟O(shè)計多繼承和多態(tài)用法示例
這篇文章主要介紹了Python面向?qū)ο蟪绦蛟O(shè)計多繼承和多態(tài)用法,結(jié)合實例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計中多繼承、多態(tài)的概念、原理、實現(xiàn)方法及相關(guān)操作注意事項,需要的朋友可以參考下2019-04-04