好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > python實現簡單的貪吃蛇游戲

python實現簡單的貪吃蛇游戲

熱門標簽:西藏快速地圖標注地點 百應ai電銷機器人鄭州 如何在地圖標注文字 地圖標注推廣單頁 ai地圖標注 女王谷地圖標注 廈門crm外呼系統如何 長春人工智能電銷機器人官網 n400電話申請多少錢

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

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

游戲實現效果如下:

后面有完整代碼和解析

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()#導入開始和重啟按鈕圖片
 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()#找到鼠標光標所在位置
 x,y=self.position#指定的點擊按鈕位置
 w,h=self.gamebegin1.get_size()#定義按鈕尺寸
 x-=w/2
 y-=h/2
 if_x=x-w/2point_xx+w/2#判斷鼠標光標是否在按鈕上
 if_y=y-h/2point_yy+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():#如果鼠標光標在按鈕上,圖片顯示為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/2point_xx+w/2
 if_y=y-h/2point_yy+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))#初始界面顏色設置為黑色
 Rect1=pygame.Rect(300,300,200,100)#設置按鈕尺寸
 Rect2=pygame.Rect(300,450,200,100)
 pygame.draw.rect(screen,(136,189,186),Rect1)#創建按鈕
 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()#找到鼠標光標所在位置
 global a,b#定義全局變量
 if 300point_x500 and 300point_y400:#判斷光標是否在按鈕上
 if pygame.mouse.get_pressed()[0]:#判斷鼠標是否按下
  a=8#a表示沒秒執行8次
  b=1#b表示設定的一個數,與后面判斷執行時有關
 if 300point_x500 and 450point_y550:
 if pygame.mouse.get_pressed()[0]:
  a=12
  b=1
 pygame.display.update()#更新屏幕
 

def getResult():#游戲結束時出現得分結果和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()#初始化字符
 #需要在其它定義中出現的參數,這里還需要定義全局變量一下
 global score,screen,font,button,status,x,y,direction,snackhead,snackbody,foodposition,state,a,b
 screen=pygame.display.set_mode((640,640))#設置屏幕尺寸
 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))#定義按鈕數值 
 pygame.display.update()

 
 while True:
  
 for event in pygame.event.get():#遍歷可能出現的事件

  if event.type == pygame.QUIT:#點擊關閉,程序關閉
  pygame.quit()
  sys.exit()
 button.ispress()#顯示按鈕
 pygame.display.update()
 if button.ifpress():
  if pygame.mouse.get_pressed()[0]:   
  break #點擊按鈕,結束這里的循環,進入游戲選擇難度界面
 while True:
 for event in pygame.event.get():

  if event.type == pygame.QUIT:
  pygame.quit()
  sys.exit()
 choice()#運行choice()
 if b==1:#如果點擊了按鈕,結束循環,進入游戲運行界面
  break
 pygame.mixer.init()#初始化音樂
 pygame.mixer.music.load('D://music//Above Everything - Capo Productions.ogg')#導入音樂 
 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'
 #跳出判斷事件循環
 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
 #頭部坐標變化 
 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()#沒吃到則將位置變化前的尾部剔除出去,設的長度不變
 pygame.display.update()
 if food_eat==0:#遲到食物后生成新的食物
  x=random.randint(1,31)
  y=random.randint(1,31)
  for position in snackbody:#判斷食物是否出現在蛇的身體處,出現則重新生成
  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()
 #如果蛇撞到墻或自己的身體,則游戲結束
 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)#音樂循環
 
def restart():#重新開始按鈕
  
 getResult()#顯示結果
 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做一個簡單的貪吃蛇
  • 用Python實現童年貪吃蛇小游戲功能的實例代碼
  • python語言實現貪吃蛇游戲
  • 150行python代碼實現貪吃蛇游戲
  • python實現貪吃蛇游戲源碼
  • 教你一步步利用python實現貪吃蛇游戲
  • python簡單貪吃蛇開發
  • python實現貪吃蛇游戲
  • python貪吃蛇游戲代碼
  • 只需要100行Python代碼就可以實現的貪吃蛇小游戲

標簽:黔東 亳州 渭南 綿陽 內江 拉薩 興安盟 廊坊

巨人網絡通訊聲明:本文標題《python實現簡單的貪吃蛇游戲》,本文關鍵詞  python,實現,簡單,的,貪吃,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python實現簡單的貪吃蛇游戲》相關的同類信息!
  • 本頁收集關于python實現簡單的貪吃蛇游戲的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 蜜乳精品久久精品一二三精品网站| 娇妻ktv成了泄欲工具| 被窝电影电视剧在线观看| 亚洲欧美日韩高清| 欧美一区福利| 黑帮大佬大佬的365天第二季| 久久久夜色精品亚洲AV周冬雨| 老板让我夹震蛋器陪客人吃饭| 操妹子小说| 欧美黑粗大| 中文字幕一级毛片| 熟女少妇精品一区二区三区| 美国一级毛片免费看| 被男同桌强解开胸罩床吻| 国产欧美精品一区二区三区小说 | 精品一区二区三区免费毛片| 福利午夜最新| 污文小说| 国产免费av吧在线观看不卡| 日本熟妇无码亚洲成aV人片榴莲| 又黄又肉的小说| 亚洲成人激情片| 调教男子锁尿肉锁精h| 殴美女撒尿粉嫩嫩小BBBwww| 国产精品成人一区二区三区无码 | 午夜精品一区二区三区在线视频| 一级婬片120分钟试看好| 夜夜爽免费视频| 晚上睡不着看点害羞| 高h猛烈失禁双性| 别揉我胸?啊~喷水了| 西西人体A片无码视频| 客厅h| 亚洲精品玖玖玖AV在线看| 国产精东传媒成人AV电影| 赵小米kitty裸19分钟视频| j8又粗又大又长又爽又硬男男| 国产在线一区在线视频| 一级特黄大片欧美久久久久久| 亚洲午夜AAA片内谢无码理伦| 欧美男同gvvideos可播放|