網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了pygame實(shí)現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
為了簡化起見,游戲素材暫定為兩張簡單的圖片(文中用的是30*30)。
大家很方便就能制作。
背景也是純黑填充。
各種音樂字體特效玩法場(chǎng)景等大家可以自由發(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(): ?#場(chǎng)景更新 ? ? 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; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#檢測(cè)蛇是否死亡 ? ? ? ?? ? ? ? ? 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()
原文鏈接:https://blog.csdn.net/qq_51449531/article/details/122441860
相關(guān)推薦
- 2022-06-04 R語言批量讀取某路徑下文件內(nèi)容的方法_R語言
- 2022-10-02 iOS開發(fā)實(shí)現(xiàn)搜索框(UISearchController)_IOS
- 2022-01-09 ng-zorro中樹(nz-tree)的拖拽
- 2022-08-10 Pandas?reindex重置索引的使用_python
- 2022-11-26 詳解Golang?ProtoBuf的基本語法總結(jié)_Golang
- 2022-07-03 C#入門之定義類成員與接口實(shí)現(xiàn)_C#教程
- 2023-10-16 springboot 集成webservice
- 2022-09-18 iOS開發(fā)retina屏幕下的點(diǎn)與像素關(guān)系詳解_IOS
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支