电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

python實現(xiàn)簡單的貪吃蛇游戲

瀏覽:53日期:2022-06-24 14:17:55

本文實例為大家分享了python實現(xiàn)貪吃蛇游戲的具體代碼,加入按鈕、難度、音樂、得分功能,供大家參考,具體內(nèi)容如下

貪吃蛇小游戲,可以顯示出得分,又背景音樂,難度選擇和開始重啟按鈕設(shè)置。

游戲?qū)崿F(xiàn)效果如下:

python實現(xiàn)簡單的貪吃蛇游戲

python實現(xiàn)簡單的貪吃蛇游戲

python實現(xiàn)簡單的貪吃蛇游戲

python實現(xiàn)簡單的貪吃蛇游戲

后面有完整代碼和解析

import sysimport pygameimport randomclass Button(object):#定義按鈕類 def __init__(self,begin1,begin2,restart1,restart2,position,position2): self.gamebegin1=pygame.image.load(begin1).convert_alpha()#導(dǎo)入開始和重啟按鈕圖片 self.gamebegin2=pygame.image.load(begin2).convert_alpha() self.gamerestart1=pygame.image.load(restart1).convert_alpha() self.gamerestart2=pygame.image.load(restart2).convert_alpha() self.position=position self.position2=position2 def ifpress(self): point_x,point_y=pygame.mouse.get_pos()#找到鼠標(biāo)光標(biāo)所在位置 x,y=self.position#指定的點擊按鈕位置 w,h=self.gamebegin1.get_size()#定義按鈕尺寸 x-=w/2 y-=h/2 if_x=x-w/2<point_x<x+w/2#判斷鼠標(biāo)光標(biāo)是否在按鈕上 if_y=y-h/2<point_y<y+h/2 return if_x and if_y def ispress(self): x,y=self.position w,h=self.gamebegin1.get_size() x-=w/2 y-=h/2 if self.ifpress():#如果鼠標(biāo)光標(biāo)在按鈕上,圖片顯示為gamebegin1,否則顯示為gamebegin2 screen.blit(self.gamebegin1,(x-w/2+8,y-h/2+7)) else: screen.blit(self.gamebegin2,(x-w/2,y-h/2)) #定義重啟按鈕,方法同上 def ifrepress(self): point_x,point_y=pygame.mouse.get_pos() x,y=self.position2 w,h=self.gamerestart1.get_size() x-=w/2 y-=h/2 if_x=x-w/2<point_x<x+w/2 if_y=y-h/2<point_y<y+h/2 return if_x and if_y def isrepress(self): x,y=self.position2 w,h=self.gamerestart1.get_size() x-=w/2 y-=h/2 if self.ifrepress(): screen.blit(self.gamerestart1,(x-w/2,y-h/2)) else: screen.blit(self.gamerestart2,(x-w/2,y-h/2)) def choice():#定義選擇難度 screen.fill((0,0,0))#初始界面顏色設(shè)置為黑色 Rect1=pygame.Rect(300,300,200,100)#設(shè)置按鈕尺寸 Rect2=pygame.Rect(300,450,200,100) pygame.draw.rect(screen,(136,189,186),Rect1)#創(chuàng)建按鈕 screen.blit(font.render(’grade one:’,-1,(255,255,255)),(300,300)) pygame.draw.rect(screen,(136,189,186),Rect2) screen.blit(font.render(’grade two:’,-1,(255,255,255)),(300,450)) pygame.display.flip()#顯示到屏幕上 point_x,point_y=pygame.mouse.get_pos()#找到鼠標(biāo)光標(biāo)所在位置 global a,b#定義全局變量 if 300<point_x<500 and 300<point_y<400:#判斷光標(biāo)是否在按鈕上 if pygame.mouse.get_pressed()[0]:#判斷鼠標(biāo)是否按下 a=8#a表示沒秒執(zhí)行8次 b=1#b表示設(shè)定的一個數(shù),與后面判斷執(zhí)行時有關(guān) if 300<point_x<500 and 450<point_y<550: if pygame.mouse.get_pressed()[0]: a=12 b=1 pygame.display.update()#更新屏幕 def getResult():#游戲結(jié)束時出現(xiàn)得分結(jié)果和gameover final_text1=’Game_Over’ final_text2=’Your final score is: ’+str(score) ft1_font=pygame.font.SysFont(’Arial’,70) ft1_surf=font.render(final_text1,1,(242,3,36)) screen.blit(ft1_surf,[screen.get_width()/2-ft1_surf.get_width()/2,100]) pygame.display.flip()def main(): pygame.init()#初始化pygame clock=pygame.time.Clock()#定義時間 pygame.font.init()#初始化字符 #需要在其它定義中出現(xiàn)的參數(shù),這里還需要定義全局變量一下 global score,screen,font,button,status,x,y,direction,snackhead,snackbody,foodposition,state,a,b screen=pygame.display.set_mode((640,640))#設(shè)置屏幕尺寸 screen.fill((0,0,0))#填充屏幕顏色 #指明圖片路徑 beginname1=’D://picture//gamebegin1.png’ beginname2=’D://picture//gamebegin2.png’ restartname1=’D://picture//restart1.png’ restartname2=’D://picture//restart2.png’ pygame.display.set_caption(’sanckgame’) font=pygame.font.SysFont(’Arial’,40)#定義字符種類和大小 snackhead=[120,120]#定義初始蛇的頭部位置 foodposition=[360,360]#定義初始食物位置 snackbody=[[120,120],[100,120],[80,120]]#定義蛇全身位置 score=0 x=300 y=300 state=0 a=1 b=0 food_eat=1#食物被吃為0,否則為1 direction=’right’#定義初始運動方向 re_direction=direction#re_direction為變化后的方向 button = Button(beginname1,beginname2,restartname1,restartname2,(330,300),(600,320))#定義按鈕數(shù)值 pygame.display.update() while True: for event in pygame.event.get():#遍歷可能出現(xiàn)的事件 if event.type == pygame.QUIT:#點擊關(guān)閉,程序關(guān)閉 pygame.quit() sys.exit() button.ispress()#顯示按鈕 pygame.display.update() if button.ifpress(): if pygame.mouse.get_pressed()[0]: break #點擊按鈕,結(jié)束這里的循環(huán),進(jìn)入游戲選擇難度界面 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() choice()#運行choice() if b==1:#如果點擊了按鈕,結(jié)束循環(huán),進(jìn)入游戲運行界面 break pygame.mixer.init()#初始化音樂 pygame.mixer.music.load(’D://music//Above Everything - Capo Productions.ogg’)#導(dǎo)入音樂 pygame.display.update() status=True screen=pygame.display.set_mode((640,640)) screen.fill((0,0,0)) while status: clock.tick(a)#運行速度 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN:#判斷鍵盤事件 if event.key == pygame.K_RIGHT or event.key == ord(’d’): re_direction=’right’ if event.key == pygame.K_LEFT or event.key == ord(’a’): re_direction=’left’ if event.key == pygame.K_UP or event.key == ord(’w’): re_direction=’up’ if event.key == pygame.K_DOWN or event.key == ord(’s’): re_direction=’down’ #跳出判斷事件循環(huán) if re_direction==’right’and not direction ==’left’: direction=re_direction if re_direction==’left’and not direction ==’right’: direction=re_direction if re_direction==’up’and not direction ==’down’: direction=re_direction if re_direction==’down’and not direction ==’up’: direction=re_direction #頭部坐標(biāo)變化 if direction==’left’: snackhead[0]-=20 if direction==’right’: snackhead[0]+=20 if direction==’down’: snackhead[1]+=20 if direction==’up’: snackhead[1]-=20 snackbody.insert(0,list(snackhead))#每移動一次,把頭部新位置插入到最前面,形成新的頭部 if snackhead[0]==foodposition[0] and snackhead[1]==foodposition[1]:#判斷蛇是否吃到了食物 food_eat=0 score+=1 pygame.display.update() else: snackbody.pop()#沒吃到則將位置變化前的尾部剔除出去,設(shè)的長度不變 pygame.display.update() if food_eat==0:#遲到食物后生成新的食物 x=random.randint(1,31) y=random.randint(1,31) for position in snackbody:#判斷食物是否出現(xiàn)在蛇的身體處,出現(xiàn)則重新生成 if not x == position[0] and not y == position[1]: state=1 else: state=0 if state ==1: foodposition=[int(x*20),int(y*20)] food_eat=1 screen.fill((0,0,0)) for position in snackbody:#畫出蛇的身體和食物 rect2=pygame.Rect(position[0],position[1],20,20) pygame.draw.rect(screen,(166,188,199),rect2) rect1=pygame.Rect(foodposition[0],foodposition[1],20,20) pygame.draw.rect(screen,(169,111,102),rect1) screen.blit(font.render(’Score:’+str(score),-1,(255,255,255)),(100,50))#顯示出得分 pygame.display.update() #如果蛇撞到墻或自己的身體,則游戲結(jié)束 if snackhead[0]<0 or snackhead[0]>620: restart() if snackhead[1]<0 or snackhead[1]>620: restart() for body in snackbody[1:]: if body[0]==snackhead[0]and body[1]==snackhead[1]: restart() pygame.display.flip() pygame.display.update() if pygame.mixer.music.get_busy()==False: pygame.mixer.music.set_volume(0.3) pygame.mixer.music.play(-1)#音樂循環(huán) def restart():#重新開始按鈕 getResult()#顯示結(jié)果 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() button.isrepress() pygame.display.update() if button.ifrepress(): pygame.display.update() if pygame.mouse.get_pressed()[0]: main()main()

按鈕圖片如下,可以自己畫圖做,音樂最好用ogg格式的,否則可能出問題。

python實現(xiàn)簡單的貪吃蛇游戲

python實現(xiàn)簡單的貪吃蛇游戲

python實現(xiàn)簡單的貪吃蛇游戲

python實現(xiàn)簡單的貪吃蛇游戲

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 医疗仪器模块 健康一体机 多参数监护仪 智慧医疗仪器方案定制 血氧监护 心电监护 -朗锐慧康 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | 超声波清洗机_细胞破碎仪_实验室超声仪器_恒温水浴-广东洁盟深那仪器 | 印刷人才网 印刷、包装、造纸,中国80%的印刷企业人才招聘选印刷人才网! | 马尔表面粗糙度仪-MAHR-T500Hommel-Mitutoyo粗糙度仪-笃挚仪器 | 卫生人才网-中国专业的医疗卫生医学人才网招聘网站! | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 鲸鱼视觉 -数字展厅多媒体互动展示制作公司 | 斗式提升机_链式斗提机_带式斗提机厂家无锡市鸿诚输送机械有限公司 | 上海新光明泵业制造有限公司-电动隔膜泵,气动隔膜泵,卧式|立式离心泵厂家 | 电缆桥架生产厂家_槽式/梯式_热镀锌线槽_广东东莞雷正电气 | 健康管理师报考条件,考试时间,报名入口—首页 | 【法利莱住人集装箱厂家】—活动集装箱房,集装箱租赁_大品牌,更放心 | 工业冷却塔维修厂家_方形不锈钢工业凉水塔维修改造方案-广东康明节能空调有限公司 | 篮球地板厂家_舞台木地板品牌_体育运动地板厂家_凯洁地板 | 青岛美佳乐清洁工程有限公司|青岛油烟管道清洗|酒店|企事业单位|学校工厂厨房|青岛油烟管道清洗 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 单柱拉力机-橡胶冲片机-哑铃裁刀-江都轩宇试验机械厂 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 | 代做标书-代写标书-专业标书文件编辑-「深圳卓越创兴公司」 | 临海涌泉蜜桔官网|涌泉蜜桔微商批发代理|涌泉蜜桔供应链|涌泉蜜桔一件代发 | 南京试剂|化学试剂|分析试剂|实验试剂|cas号查询-专业60年试剂销售企业 | 南京试剂|化学试剂|分析试剂|实验试剂|cas号查询-专业60年试剂销售企业 | 耐酸碱胶管_耐腐蚀软管总成_化学品输送软管_漯河利通液压科技耐油耐磨喷砂软管|耐腐蚀化学软管 | 昆明化妆培训-纹绣美甲-美容美牙培训-昆明博澜培训学校 | 江苏大隆凯科技有限公司| 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 干法制粒机_智能干法制粒机_张家港市开创机械制造有限公司 | 右手官网|右手工业设计|外观设计公司|工业设计公司|产品创新设计|医疗产品结构设计|EMC产品结构设计 | BAUER减速机|ROSSI-MERSEN熔断器-APTECH调压阀-上海爱泽工业设备有限公司 | 房在线-免费房产管理系统软件-二手房中介房屋房源管理系统软件 | 六维力传感器_六分量力传感器_模腔压力传感器-南京数智微传感科技有限公司 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | 锂电池砂磨机|石墨烯砂磨机|碳纳米管砂磨机-常州市奥能达机械设备有限公司 | 质构仪_鱼糜弹性仪-上海腾拔仪器科技有限公司 | 医学动画公司-制作3d医学动画视频-医疗医学演示动画制作-医学三维动画制作公司 | 营养师网,营养师考试时间,报名入口—网站首页 | 防火门-专业生产甲级不锈钢钢质防火门厂家资质齐全-广东恒磊安防设备有限公司 | 接地电阻测试仪[厂家直销]_电缆故障测试仪[精准定位]_耐压测试仪-武汉南电至诚电力设备 | 艺术涂料_进口艺术涂料_艺术涂料加盟_艺术涂料十大品牌 -英国蒙太奇艺术涂料 | bkzzy在职研究生网 - 在职研究生招生信息咨询平台 |