Python+Pygame實(shí)現(xiàn)簡(jiǎn)單的射擊小游戲
前言
哈嘍!哈嘍。栗子上線啦~
要說(shuō)什么游戲能夠獲得大家的喜愛(ài)?
唯射擊游戲莫屬。此前大火手游的《刺激戰(zhàn)場(chǎng)》當(dāng)然現(xiàn)在是叫做《和平精英》啦,想當(dāng)初我也是第一批下載的老玩家了!射擊游戲加上豐富的地圖不同的體驗(yàn)?zāi)鞘窍喈?dāng)?shù)挠腥ず猛鎯骸?/p>
玩家在射擊游戲中,通過(guò)瞄準(zhǔn),擊殺敵人,能夠獲得及時(shí)的爽感反饋。射擊游戲很早就在游戲圈占據(jù)一席之地啦~
今天的游戲代碼靈感就是來(lái)源于此哦,簡(jiǎn)約簡(jiǎn)約,大制作小編一個(gè)程序員頭禿也搞不了啦。
本期來(lái)為大家推薦一款Python游戲代碼版本的“射擊游戲”——小編取名叫《無(wú)敵狙擊手》,也就是簡(jiǎn)約的射擊游戲,看一下這款代碼你喜歡嘛?
一個(gè)適合零基礎(chǔ)開(kāi)發(fā)游戲的小白案例啦!超級(jí)簡(jiǎn)單呢
游戲玩法:點(diǎn)擊鼠標(biāo)左鍵對(duì)準(zhǔn)移動(dòng)的靶子,射中一次看距離加分。射中最中間位置一次+3分哦
一、運(yùn)行環(huán)境
1)環(huán)境安裝
Python3、 Pycharm 、Pygame模塊部分自帶模塊就不展示啦。
第三方庫(kù)的安裝:pip install pygame 或者
帶鏡像源 pip install -i https://pypi.douban.com/simple/ +模塊名
2)素材(圖片、音樂(lè)等)

二、代碼展示
主程序
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)的截圖展示,科能效果不是很好!動(dòng)態(tài)的展示我就沒(méi)錄制視頻了哈,大家拿代碼自己玩兒哦~
1)游戲界面

2)擊中效果

3)+3分

到此這篇關(guān)于Python+Pygame實(shí)現(xiàn)簡(jiǎn)單的射擊小游戲的文章就介紹到這了,更多相關(guān)Python Pygame射擊游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenCV+Python幾何變換的實(shí)現(xiàn)示例
這篇文章主要介紹了OpenCV+Python幾何變換的實(shí)現(xiàn)示例,圖像的幾何變換是指將一幅圖像映射到另一幅圖像內(nèi)。有縮放、翻轉(zhuǎn)、仿射變換、透視、重映射等操作。感興趣的可以了解一下2021-03-03
在Python中使用PIL模塊對(duì)圖片進(jìn)行高斯模糊處理的教程
這篇文章主要介紹了在Python中使用PIL模塊對(duì)圖片進(jìn)行高斯模糊處理的教程,這個(gè)無(wú)圖形界面的腳本代碼非常簡(jiǎn)單,需要的朋友可以參考下2015-05-05
Django分頁(yè)查詢并返回jsons數(shù)據(jù)(中文亂碼解決方法)
這篇文章主要介紹了Django分頁(yè)查詢并返回jsons數(shù)據(jù)(中文亂碼解決方法),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Python面向?qū)ο蟪绦蛟O(shè)計(jì)多繼承和多態(tài)用法示例
這篇文章主要介紹了Python面向?qū)ο蟪绦蛟O(shè)計(jì)多繼承和多態(tài)用法,結(jié)合實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中多繼承、多態(tài)的概念、原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-04-04

