pygame實現(xiàn)貪吃蛇游戲
本文實例為大家分享了pygame實現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
為了簡化起見,游戲素材暫定為兩張簡單的圖片(文中用的是30*30)。
大家很方便就能制作。
背景也是純黑填充。
各種音樂字體特效玩法場景等大家可以自由發(fā)揮
import pygame import sys import random fps=6 ? ?#設(shè)置幀率 move={'up':[-30,0],'down':[30,0],'left':[0,-30],'right':[0,30]} ?#上下左右映射為值 ? ? class MOCCASIN(object): ?#蛇類 ? ? def __init__(self): ? ? ? ? self.scheme=pygame.image.load('moccasin.png') ? ? ? ? self.x=int(20*random.random())*30 ? ? ? ? self.y=int(20*random.random())*30 ? ? ? ? self.body=[[self.x,self.y],[self.x,self.y+30],[self.x,self.y+60]] ? ? ? ? self.direct='up' ? ? ? ? ? ? def update(self): ? ? ? ? self.x+=move[self.direct][1] ? ? ? ? self.y+=move[self.direct][0] ? ? ? ? self.body.insert(0,[self.x,self.y]) class FOOD(object): ?#食物類 ? ? def __init__(self): ? ? ? ? self.scheme=pygame.image.load('food.png') ? ? ? ? self.coordinate=(int(20*random.random())*30,int(20*random.random())*30) def updateMap(): ?#場景更新 ? ? screen.fill((0,0,0)) ? ? moccasin.update() ? ? screen.blit(food.scheme,food.coordinate) ? ? for x in moccasin.body: ? ? ? ? screen.blit(moccasin.scheme,tuple(x)+(30,30)) def bitself(): ?#是否咬到自己 ? ? dct={} ? ? for ll in moccasin.body: ? ? ? ? if tuple(ll) in dct :return True ? ? ? ? dct[tuple(ll)]=1 ? ? return False ? ? ?? ? ? ? ?? if __name__=='__main__': ? ? pygame.init() ? ? pygame.display.set_caption('「moccasin」') ? ? screen=pygame.display.set_mode((600,600)) ? ? food=FOOD() ? ? moccasin=MOCCASIN() ? ? clk=pygame.time.Clock() ? ? while True: ? ? ? ? clk.tick(fps) ? ? ? ? for event in pygame.event.get(): ? ? ? ? ? ? if event.type==pygame.QUIT: ? ? ? ? ? ? ? ? sys.exit() ? ? ? ? ? ? elif event.type==pygame.KEYDOWN: ? ? ? ? ? ? ? ? if event.key==pygame.K_UP and moccasin.direct!='down': ? ? ? ? ? ? ? ? ? ? moccasin.direct='up' ? ? ? ? ? ? ? ? if event.key==pygame.K_DOWN and moccasin.direct!='up': ? ? ? ? ? ? ? ? ? ? moccasin.direct='down' ? ? ? ? ? ? ? ? if event.key==pygame.K_LEFT and moccasin.direct!='right': ? ? ? ? ? ? ? ? ? ? moccasin.direct='left' ? ? ? ? ? ? ? ? if event.key==pygame.K_RIGHT and moccasin.direct!='left': ? ? ? ? ? ? ? ? ? ? moccasin.direct='right' ? ? ? ? ? ? ? ? ? ?? ? ? ? ? if not (0<=moccasin.x<600 and 0<=moccasin.y<600) or bitself():? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#檢測蛇是否死亡 ? ? ? ?? ? ? ? ? if food.coordinate==tuple(moccasin.body[0]): #是否吃到食物 ? ? ? ? ? ? food=FOOD() ? ? ? ? else: moccasin.body.pop() ? ? ? ? ? ?? ? ? ? ? updateMap() #更新地圖 ? ? ? ? pygame.display.update() ? ? ? ?? ? ? #蛇死亡,游戲結(jié)束 ? ? screen.fill((0,0,0)) ? ? tips="失 敗" ? ? tipsFont=pygame.font.Font('C:\Windows\Fonts\msyh.ttc',100) ? ? tipsSurf=tipsFont.render(tips,1,(255,255,255)) ? ? screen.blit(tipsSurf,(screen.get_width()/2-tipsSurf.get_width()/2,256)) ? ? pygame.display.flip()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python實現(xiàn)with結(jié)構(gòu)的@contextmanager方法詳解
這篇文章主要介紹了使用Python實現(xiàn)with結(jié)構(gòu)的@contextmanager方法詳解,這個結(jié)構(gòu)的好處,一個是簡潔,一個是當我們對文件操作的邏輯很長的時候,不會因為忘了關(guān)閉文件而造成不必要的錯誤,需要的朋友可以參考下2023-07-07Tensorflow中的降維函數(shù)tf.reduce_*使用總結(jié)
這篇文章主要介紹了Tensorflow中的降維函數(shù)tf.reduce_*使用總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04python網(wǎng)絡(luò)爬蟲精解之pyquery的使用說明
PyQuery是一個類似于jQuery的解析網(wǎng)頁工具,使用lxml操作xml和html文檔,它的語法和jQuery很像。和XPATH,Beautiful Soup比起來,PyQuery更加靈活,提供增加節(jié)點的class信息,移除某個節(jié)點,提取文本信息等功能2021-09-09Python+OpenCV圖像處理—— 色彩空間轉(zhuǎn)換
這篇文章主要介紹了Python+OpenCV如何對圖片進行色彩空間轉(zhuǎn)換,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下下2020-10-10