亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python寫(xiě)捕魚(yú)達(dá)人的游戲?qū)崿F(xiàn)

瀏覽:65日期:2022-07-31 16:00:14

當(dāng)今最火的莫過(guò)于用Python寫(xiě)出捕魚(yú)達(dá)人的效果了。啥都不用說(shuō),亮代碼~~~

# coding:utf-8# 導(dǎo)入模塊import pygame,sys,time,randomfrom pygame.locals import *# 初始化pygame環(huán)境pygame.init()# 創(chuàng)建一個(gè)長(zhǎng)寬分別為800/480的窗口canvas = pygame.display.set_mode((800,480))canvas.fill((255,255,255))# 設(shè)置窗口標(biāo)題pygame.display.set_caption(’捕魚(yú)達(dá)人’)# 加載圖片bg = pygame.image.load('./images/bg.jpg')fish1 = pygame.image.load('./images/fish1_0.png')fish2 = pygame.image.load('./images/fish2_0.png')fish3 = pygame.image.load('./images/fish3_0.png')fish4 = pygame.image.load('./images/fish4_0.png')fish5 = pygame.image.load('./images/fish5_0.png')fish6 = pygame.image.load('./images/fish6_0.png')fish7 = pygame.image.load('./images/fish7_0.png')fish8 = pygame.image.load('./images/fish8_0.png')fish9 = pygame.image.load('./images/fish9_0.png')fish10 = pygame.image.load('./images/fish10_0.png')fish11 = pygame.image.load('./images/fish11_0.png')net = pygame.image.load('./images/net.png')gameover = pygame.image.load('./images/gameover.jpg')# 定義事件監(jiān)聽(tīng)函數(shù)def handleEvent(): for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() # 添加鼠標(biāo)移動(dòng)事件,讓鼠標(biāo)控制網(wǎng)的移動(dòng) if event.type == MOUSEMOTION: Game.net.x = event.pos[0] - Game.net.width/2 Game.net.y = event.pos[1] - Game.net.height/2# 定義時(shí)間間隔判斷函數(shù)def isActionTime(lastTime,interval): if lastTime == 0: return True currentTime = time.time() return currentTime - lastTime >= interval# 定義魚(yú)類class Fish(): def __init__(self,width,height,y,img): self.width = width self.height = height self.x = 800 - self.width self.y = y self.img = img def paint(self): canvas.blit(self.img,(self.x,self.y)) def step(self): self.x -= 10# 定義網(wǎng)類class Net(): def __init__(self,x,y): self.x = x self.y = y self.width = 160 self.height = 160 self.img = net def paint(self): canvas.blit(self.img,(self.x,self.y)) # 定義越界函數(shù) def outOfBounds(self): if self.x <= 0: self.x = 0 elif self.x >= 800 - self.width: self.x = 800 - self.width elif self.y <= 0: self.y = 0 elif self.y >= 480 - self.height: self.y = 480 - self.height # 定義碰撞函數(shù) def hit(self,c): return c.x > self.x - c.width and c.x < self.x + self.width and c.y > self.y - c.height and c.y < self.y + self.height# 定義存儲(chǔ)游戲數(shù)據(jù)的類class Game(): # 游戲狀態(tài) state = ’RUNNING’ # 魚(yú)的列表 fish = [] # 網(wǎng)的對(duì)象 net = Net(100,100) # 分?jǐn)?shù) score = 0 # 時(shí)間 t = 60 n = 1 # 上一次時(shí)間 lastTime = 0 # 時(shí)間間隔 interval = 0.5 # 所有魚(yú)的寬高 fish_pos = [[22,13],[50,48],[55,55],[73,73],[104,80],[60,60],[93,93],[94,81],[99,103],[180,140],[320,206],[100,96]]# 定義產(chǎn)生魚(yú)的函數(shù)def conEnter(): if not isActionTime(Game.lastTime,Game.interval): return Game.lastTime = time.time() r = random.randint(1,11) if Game.t <= 60: Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load('./images/fish'+str(r)+'_0.png'))) elif Game.t <= 30: Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load('./images/fish'+str(r)+'_0.png'))) Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load('./images/fish'+str(r)+'_0.png'))) elif Game.t <= 10: Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load('./images/fish'+str(r)+'_0.png'))) Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load('./images/fish'+str(r)+'_0.png'))) Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load('./images/fish'+str(r)+'_0.png')))# 定義畫(huà)組件函數(shù)def conPaint(): canvas.blit(bg,(0,0)) Game.net.paint() showScore() showTime() for fish in Game.fish: fish.paint()# 定義組件移動(dòng)函數(shù)def conStep(): Game.net.outOfBounds() for fish in Game.fish: fish.step()# 定義碰撞檢測(cè)函數(shù)def checkHit(): for fish in Game.fish: if Game.net.hit(fish) and len(Game.fish) != 0: Game.fish.remove(fish) Game.score += 1# 定義繪制分?jǐn)?shù)函數(shù)def showScore(): TextFont = pygame.font.SysFont(’SimHei’,40) TextScore = TextFont.render(’得分:’+str(Game.score),True,(255,255,255)) canvas.blit(TextScore,(20,20))# 定義繪制時(shí)間函數(shù)def showTime(): TextFont = pygame.font.SysFont(’SimHei’,40) TextScore = TextFont.render(’剩余時(shí)間:’+str(Game.t),True,(255,255,255)) canvas.blit(TextScore,(550,20)) if Game.n % 50 == 1: Game.t -= 1 Game.n += 1 if Game.t == 0: Game.state = ’END’# 定義主控制函數(shù)def control(): if Game.state == ’RUNNING’: conEnter() conPaint() conStep() checkHit() elif Game.state == ’END’: canvas.blit(gameover,(0,0)) TextFont = pygame.font.SysFont(’SimHei’,40) TextScore = TextFont.render(’最終得分:’+str(Game.score),True,(0,0,0)) canvas.blit(TextScore,(50,50))while True: # 調(diào)用主控制函數(shù) control() # 更新屏幕內(nèi)容 pygame.display.update() # 延遲10毫秒 pygame.time.delay(10) # 監(jiān)聽(tīng)事件 handleEvent()

這段代碼用了一些Python的基礎(chǔ)知識(shí),包括事件,定義函數(shù),取余,循環(huán),判斷,定義類,創(chuàng)建對(duì)象等。這些沒(méi)什么好說(shuō)的。導(dǎo)入的幾個(gè)庫(kù)也是很常用的庫(kù),基本算是程序員必備。把代碼擺這里主要是為了讓大家借鑒。要是寫(xiě)不出來(lái)真是沒(méi)臉繼續(xù)寫(xiě)Python了…

大家可以利用我的代碼,在做事件監(jiān)聽(tīng)等函數(shù)時(shí)應(yīng)該會(huì)方便一些。

圖片我發(fā)在下面了哈,需要的自取。

源碼下載

到此這篇關(guān)于Python寫(xiě)捕魚(yú)達(dá)人的游戲?qū)崿F(xiàn)的文章就介紹到這了,更多相關(guān)Python 捕魚(yú)達(dá)人內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 色综合加勒比 | 三级黄色片a | 久久视频精品36线视频在线观看 | 色在线国产 | 国产精品99久久免费黑人 | 大胆国模一区二区三区伊人 | 精品日本亚洲一区二区三区 | 日韩中文字幕久久久经典网 | 欧美日韩一区二区视频免费看 | 黄色美女免费 | 日韩高清一区二区 | 啪啪色视频 | 欧美高清性刺激毛片 | 爱爱小视频免费体验区在线观看 | 国产剧情视频在线观看 | 免费观看女人高清视频 | 夜夜草影院 | 欧美日韩中文国产一区 | 一级片一级片一级片一级片 | 黄色视频毛片 | 国产91色综合久久免费分享 | 在线看片视频 | 深夜成人性视频免费看 | 5月婷婷6月丁香 | 国产精品久久久久久久hd | 久久久99精品久久久久久 | 亚洲视频毛片 | 国内一级片 | 99热这里都是国产精品 | 99视频在线看观免费 | 免费看黄网址 | 国产亚洲精品日韩综合网 | 亚洲国产婷婷综合在线精品 | 中国wwwxxx| 国产亚洲高清视频 | 人人干视频在线观看 | 免费一级黄色录像影片 | 免费观看好看的国产片 | 国产一区二区精品 | 免费一级毛片在线播放 | 一级特级全黄 |