Python贪吃蛇小游戏
生活随笔
收集整理的這篇文章主要介紹了
Python贪吃蛇小游戏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python貪吃蛇
? python上機課時閑著沒事寫了一個貪吃蛇的小游戲玩了玩,之后又加了一些音效啥的東西,代碼里都有注釋。
? 該程序有聲音效果和計分機制,不能暫停。
可以對照注釋修改里面的參數。
按下任意鍵開始游戲,沒獲得5分時蛇的移動速度會加快,增加游戲難度。
運行截圖
]
程序截圖
import pygame from sys import exit import randomwidth = 1920 #800 屏幕寬的像素 hight = 960 #600 屏幕高的像素 ROW = 48 #30 行顯示多少個格子 CLO = 96 #40 列顯示多少個格子#顏色定義 white = (255, 255, 255) yellow = (255, 255, 102) black = (0, 0, 0) red = (213, 50, 80) green = (0, 255, 0) blue = (50, 153, 213)head_color = (221, 147, 64) #頭的顏色 snake_color = (19, 161, 14)#蛇身體的顏色 food_color = (254, 39, 0)#食物的顏色 back_color = (222, 240, 242) #背景顏色 score = 0 last_score = 0 #上次的分數pygame.init() #pygame初始化 window = pygame.display.set_mode((width,hight)) #新建游戲窗口 pygame.display.set_caption("貪吃蛇小游戲") #設定標題clock = pygame.time.Clock() #用來處理游戲的幀率#設定字體 font_style = pygame.font.SysFont("SimHei", 66) score_font = pygame.font.SysFont("SimHei", 35)#設定背景音樂 pygame.mixer.music.load("music/background.mp3") pygame.mixer.music.play(loops=-1) #無限循環播放#音效對象 eat_music = pygame.mixer.Sound("music/eat.wav") #吃食物音效 big_music = pygame.mixer.Sound("music/big.wav") #升級音效 die_music = pygame.mixer.Sound("music/die.wav") #死亡音效class Point(): #格子類的定義row = 0clo = 0def __init__(self,row = 0, col = 0):self.row = rowself.clo = coldef copy(self):return Point(row=self.row,col=self.clo)def gen_food(): #隨機生成食物while True:position = Point(row=random.randint(0,ROW - 1),col=random.randint(0,CLO - 1))is_ok = Trueif position.row == head.row and position.clo == head.clo:is_ok = Falsefor body in snake:if body.row == position.row and body.clo == position.clo:is_ok = Falsebreakif is_ok:breakreturn positiondef message(msg, color):mesg = font_style.render(msg, True, color)window.blit(mesg, [width / 4, hight / 4])def Your_score(score):value = score_font.render("Score: " + str(score), True, yellow)window.blit(value, [0, 0])def rect(point, color): #渲染格子left = point.clo * width / CLOtop = point.row * hight / ROWpygame.draw.rect(window, color, (left, top, width / CLO - 0, hight / ROW - 0))def died(score):if score > 0 and score <= 10:message("你怎么這么菜!!", red)if score > 10 and score <= 20:message("你好菜啊!!", red)if score == 0:message("666!", red)if score >= 20 and score <= 30:message("有點菜!!", red)if score >= 30 and score <= 40:message("還行吧!!", red)if score >= 40 and score <= 50:message("有點東西!!", red)if score >= 50 and score <= 70:message("牛牛牛!!", red)if score > 70:message("你就是神!!", red)pygame.display.update()pygame.time.delay(2000)die_music.play()head = Point(row=ROW / 2, col=CLO / 2) # 頭初始化 snake = [Point(row=ROW / 2, col=CLO / 2 + 1), Point(row=ROW / 2, col=CLO / 2 + 2),Point(row=ROW / 2, col=CLO / 2 + 3)] # 蛇身體初始化 direct = 'left' # 設定初始方向 food = gen_food() # 生成食物#-------------------------------主循環------------------------ def main_loop():head = Point(row=ROW / 2, col=CLO / 2) # 頭初始化snake = [Point(row=ROW / 2, col=CLO / 2 + 1), Point(row=ROW / 2, col=CLO / 2 + 2),Point(row=ROW / 2, col=CLO / 2 + 3)] # 蛇身體初始化direct = 'left' # 設定初始方向food = gen_food() # 生成食物frame = 8 # 幀率,控制游戲快慢global scorescore = 0is_ok = Truewhile is_ok: # 邏輯主函數clock.tick(frame) # 幀率設置for event in pygame.event.get(): # 產生按鍵中斷時進行處理if event.type == pygame.QUIT:state = Falsepygame.quit()exit(0)elif event.type == pygame.KEYDOWN:if event.key == 97 or event.key == 276: # a鍵if direct == 'top' or direct == 'bottom':direct = 'left'if event.key == 119 or event.key == 273: # w鍵if direct == 'left' or direct == 'right':direct = 'top'if event.key == 274 or event.key == 115: # s鍵if direct == 'left' or direct == 'right':direct = 'bottom'if event.key == 100 or event.key == 275: # d鍵if direct == 'top' or direct == 'bottom':direct = 'right'# 蛇移動刷新snake.insert(0, head.copy())if direct == 'left':head.clo -= 1if direct == 'right':head.clo += 1if direct == 'top':head.row -= 1if direct == 'bottom':head.row += 1# 碰撞檢測if head.row < 0 or head.row > ROW or head.clo < 0 or head.clo > CLO:died(score)breakfor point in snake:if head.row == point.row and head.clo == point.clo:died(score)is_ok = Falsebreak# 是否吃到食物is_eat = Falseif food.row == head.row and food.clo == head.clo: # 如果吃到食物is_eat = Truefood = gen_food()score += 1eat_music.play()if not is_eat:snake.pop()#評分每達到5升級,加快一點游戲速度if score % 5 == 0 and score != 0 and last_score != score:big_music.play()frame += 2# 渲染背景pygame.draw.rect(window, back_color, (0, 0, width, hight))# 渲染蛇和食物rect(head, head_color)rect(food, food_color)for body in snake:rect(body, snake_color)# 讓計算機進行刷新Your_score(score)pygame.display.update()last_score = scoreis_loop = Truewhile is_loop:window.fill(back_color)score_string = "Your Score:" + str(score)message(score_string, red)pygame.display.update()for event in pygame.event.get(): # 產生按鍵中斷時進行處理if event.type == pygame.KEYDOWN:main_loop()elif event.type == pygame.QUIT:is_loop = Falsepygame.quit()exit(0)如果要運行的話需要有Img和music目錄里的資源文件。
下載地址:
鏈接:https://pan.baidu.com/s/1sj2o8Led6srMEjHUzQD0Ig
提取碼:r4tj
總結
以上是生活随笔為你收集整理的Python贪吃蛇小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: div+js写弹出框
- 下一篇: TMDb数据分析报告