最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年03月18日 16:26:37   作者:carrot_interrupt  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

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

后面有完整代碼和解析

import sys
import pygame
import random

class Button(object):#定義按鈕類
 def __init__(self,begin1,begin2,restart1,restart2,position,position2):
 self.gamebegin1=pygame.image.load(begin1).convert_alpha()#導(dǎo)入開(kāi)始和重啟按鈕圖片
 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#指定的點(diǎn)擊按鈕位置
 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表示沒(méi)秒執(zhí)行8次
  b=1#b表示設(shè)定的一個(gè)數(shù),與后面判斷執(zhí)行時(shí)有關(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é)束時(shí)出現(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()#定義時(shí)間
 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'#定義初始運(yùn)動(dòng)方向
 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:#點(diǎn)擊關(guān)閉,程序關(guān)閉
  pygame.quit()
  sys.exit()
 button.ispress()#顯示按鈕
 pygame.display.update()
 if button.ifpress():
  if pygame.mouse.get_pressed()[0]:   
  break #點(diǎn)擊按鈕,結(jié)束這里的循環(huán),進(jìn)入游戲選擇難度界面
 while True:
 for event in pygame.event.get():

  if event.type == pygame.QUIT:
  pygame.quit()
  sys.exit()
 choice()#運(yùn)行choice()
 if b==1:#如果點(diǎn)擊了按鈕,結(jié)束循環(huán),進(jìn)入游戲運(yùn)行界面
  break
 pygame.mixer.init()#初始化音樂(lè)
 pygame.mixer.music.load('D://music//Above Everything - Capo Productions.ogg')#導(dǎo)入音樂(lè) 
 pygame.display.update()
 status=True
 screen=pygame.display.set_mode((640,640))
 screen.fill((0,0,0))
 while status:
 clock.tick(a)#運(yùn)行速度
 
 for event in pygame.event.get():

  if event.type == pygame.QUIT:
  pygame.quit()
  sys.exit()
  
  elif event.type == pygame.KEYDOWN:#判斷鍵盤(pán)事件
  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))#每移動(dòng)一次,把頭部新位置插入到最前面,形成新的頭部
 
 if snackhead[0]==foodposition[0] and snackhead[1]==foodposition[1]:#判斷蛇是否吃到了食物
  food_eat=0
  score+=1
  pygame.display.update()
 else:
  snackbody.pop()#沒(méi)吃到則將位置變化前的尾部剔除出去,設(shè)的長(zhǎng)度不變
 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:#畫(huà)出蛇的身體和食物
  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)#音樂(lè)循環(huán)
 
def restart():#重新開(kāi)始按鈕
  
 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()

按鈕圖片如下,可以自己畫(huà)圖做,音樂(lè)最好用ogg格式的,否則可能出問(wèn)題。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實(shí)現(xiàn)將圖片轉(zhuǎn)換為ASCII字符畫(huà)

    Python實(shí)現(xiàn)將圖片轉(zhuǎn)換為ASCII字符畫(huà)

    這篇文章主要介紹了Python實(shí)現(xiàn)將圖片轉(zhuǎn)換為ASCII字符畫(huà),要將圖片轉(zhuǎn)換為字符圖其實(shí)很簡(jiǎn)單,我們首先將圖片轉(zhuǎn)換為灰度圖像,這樣圖片的每個(gè)像素點(diǎn)的顏色值都是0到255,然后我們選用一些在文字矩形框內(nèi)占用面積從大到小的ASCII碼字符
    2022-08-08
  • fastcgi文件讀取漏洞之python掃描腳本

    fastcgi文件讀取漏洞之python掃描腳本

    這篇文章主要介紹了fastcgi文件讀取漏洞之python掃描腳本,需要的朋友可以參考下
    2017-04-04
  • Python實(shí)現(xiàn)圖片格式轉(zhuǎn)換

    Python實(shí)現(xiàn)圖片格式轉(zhuǎn)換

    經(jīng)常會(huì)遇到圖片格式需要轉(zhuǎn)換的情況,這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)圖片格式轉(zhuǎn)換,文中示例代碼介紹的非常詳細(xì)、實(shí)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Python?print函數(shù)使用由淺入深全面詳解

    Python?print函數(shù)使用由淺入深全面詳解

    這篇文章主要為大家介紹了Python?print函數(shù)使用由淺入深全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Flask框架鉤子函數(shù)功能與用法分析

    Flask框架鉤子函數(shù)功能與用法分析

    這篇文章主要介紹了Flask框架鉤子函數(shù)功能與用法,簡(jiǎn)單描述了flask框架鉤子函數(shù)的概念、功能并結(jié)合實(shí)例形式分析了flask框架鉤子函數(shù)的基本用法,需要的朋友可以參考下
    2019-08-08
  • Python?SQLAlchemy建立模型基礎(chǔ)關(guān)系模式過(guò)程詳解

    Python?SQLAlchemy建立模型基礎(chǔ)關(guān)系模式過(guò)程詳解

    SQLAlchemy是Python編程語(yǔ)言下的一款開(kāi)源軟件。提供了SQL工具包及對(duì)象關(guān)系映射(ORM)工具,使用MIT許可證發(fā)行。SQLAlchemy“采用簡(jiǎn)單的Python語(yǔ)言,為高效和高性能的數(shù)據(jù)庫(kù)訪問(wèn)設(shè)計(jì),實(shí)現(xiàn)了完整的企業(yè)級(jí)持久模型”。SQL數(shù)據(jù)庫(kù)的量級(jí)和性能重要于對(duì)象集合
    2022-12-12
  • python框架Django實(shí)戰(zhàn)商城項(xiàng)目之工程搭建過(guò)程圖文詳解

    python框架Django實(shí)戰(zhàn)商城項(xiàng)目之工程搭建過(guò)程圖文詳解

    這篇文章主要介紹了python框架Django實(shí)戰(zhàn)商城項(xiàng)目之工程搭建過(guò)程,這個(gè)項(xiàng)目很像京東商城,項(xiàng)目開(kāi)發(fā)采用前后端不分離的模式,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2020-03-03
  • Python著名游戲?qū)崙?zhàn)之方塊連接 我的世界

    Python著名游戲?qū)崙?zhàn)之方塊連接 我的世界

    讀萬(wàn)卷書(shū)不如行萬(wàn)里路,學(xué)的扎不扎實(shí)要通過(guò)實(shí)戰(zhàn)才能看出來(lái),本篇文章手把手帶你模仿著名游戲——我的世界,大家可以在過(guò)程中查缺補(bǔ)漏,看看自己掌握程度怎么樣
    2021-10-10
  • pytorch使用-tensor的基本操作解讀

    pytorch使用-tensor的基本操作解讀

    這篇文章主要介紹了pytorch使用-tensor的基本操作解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 如何使用python轉(zhuǎn)移mysql數(shù)據(jù)庫(kù)中的全部數(shù)據(jù)

    如何使用python轉(zhuǎn)移mysql數(shù)據(jù)庫(kù)中的全部數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了如何使用python轉(zhuǎn)移mysql數(shù)據(jù)庫(kù)中的全部數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解下
    2024-11-11

最新評(píng)論

右玉县| 曲靖市| 开封市| 商城县| 阳山县| 北宁市| 龙门县| 万年县| 田阳县| 丰县| 登封市| 乌拉特后旗| 财经| 新河县| 平阳县| 青阳县| 内黄县| 屯门区| 清流县| 明光市| 柳河县| 霍邱县| 济源市| 聂荣县| 冷水江市| 松桃| 沁水县| 宜良县| 古浪县| 阿巴嘎旗| 定州市| 德阳市| 思南县| 尉氏县| 克什克腾旗| 泰和县| 屏边| 周至县| 龙口市| 河西区| 蓬溪县|