150行python代碼實現(xiàn)貪吃蛇游戲
上篇博文我們實現(xiàn)了兩百行代碼實現(xiàn)貪吃蛇游戲,這次我們來實現(xiàn)一個代碼量更加少,功能卻更加完整的
python代碼實現(xiàn)貪吃蛇游戲,具體代碼與文件可以訪問我的GitHub地址獲取
游戲截圖
動圖
貪吃蛇python代碼
"""貪吃蛇""" import random import sys import time import pygame from pygame.locals import * from collections import deque SCREEN_WIDTH = 600 # 屏幕寬度 SCREEN_HEIGHT = 480 # 屏幕高度 SIZE = 20 # 小方格大小 LINE_WIDTH = 1 # 網(wǎng)格線寬度 # 游戲區(qū)域的坐標(biāo)范圍 SCOPE_X = (0, SCREEN_WIDTH // SIZE - 1) SCOPE_Y = (2, SCREEN_HEIGHT // SIZE - 1) # 食物的分值及顏色 FOOD_STYLE_LIST = [(10, (255, 100, 100)), (20, (100, 255, 100)), (30, (100, 100, 255))] LIGHT = (100, 100, 100) DARK = (200, 200, 200) # 蛇的顏色 BLACK = (0, 0, 0) # 網(wǎng)格線顏色 RED = (200, 30, 30) # 紅色,GAME OVER 的字體顏色 BGCOLOR = (40, 40, 60) # 背景色 def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)): imgText = font.render(text, True, fcolor) screen.blit(imgText, (x, y)) # 初始化蛇 def init_snake(): snake = deque() snake.append((2, SCOPE_Y[0])) snake.append((1, SCOPE_Y[0])) snake.append((0, SCOPE_Y[0])) return snake def create_food(snake): food_x = random.randint(SCOPE_X[0], SCOPE_X[1]) food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1]) while (food_x, food_y) in snake: # 如果食物出現(xiàn)在蛇身上,則重來 food_x = random.randint(SCOPE_X[0], SCOPE_X[1]) food_y = random.randint(SCOPE_Y[0], SCOPE_Y[1]) return food_x, food_y def get_food_style(): return FOOD_STYLE_LIST[random.randint(0, 2)] def main(): pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('貪吃蛇') font1 = pygame.font.SysFont('SimHei', 24) # 得分的字體 font2 = pygame.font.Font(None, 72) # GAME OVER 的字體 fwidth, fheight = font2.size('GAME OVER') # 如果蛇正在向右移動,那么快速點擊向下向左,由于程序刷新沒那么快,向下事件會被向左覆蓋掉,導(dǎo)致蛇后退,直接GAME OVER # b 變量就是用于防止這種情況的發(fā)生 b = True # 蛇 snake = init_snake() # 食物 food = create_food(snake) food_style = get_food_style() # 方向 pos = (1, 0) game_over = True start = False # 是否開始,當(dāng)start = True,game_over = True 時,才顯示 GAME OVER score = 0 # 得分 orispeed = 0.5 # 原始速度 speed = orispeed last_move_time = None pause = False # 暫停 while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == KEYDOWN: if event.key == K_RETURN: if game_over: start = True game_over = False b = True snake = init_snake() food = create_food(snake) food_style = get_food_style() pos = (1, 0) # 得分 score = 0 last_move_time = time.time() elif event.key == K_SPACE: if not game_over: pause = not pause elif event.key in (K_w, K_UP): # 這個判斷是為了防止蛇向上移時按了向下鍵,導(dǎo)致直接 GAME OVER if b and not pos[1]: pos = (0, -1) b = False elif event.key in (K_s, K_DOWN): if b and not pos[1]: pos = (0, 1) b = False elif event.key in (K_a, K_LEFT): if b and not pos[0]: pos = (-1, 0) b = False elif event.key in (K_d, K_RIGHT): if b and not pos[0]: pos = (1, 0) b = False # 填充背景色 screen.fill(BGCOLOR) # 畫網(wǎng)格線 豎線 for x in range(SIZE, SCREEN_WIDTH, SIZE): pygame.draw.line(screen, BLACK, (x, SCOPE_Y[0] * SIZE), (x, SCREEN_HEIGHT), LINE_WIDTH) # 畫網(wǎng)格線 橫線 for y in range(SCOPE_Y[0] * SIZE, SCREEN_HEIGHT, SIZE): pygame.draw.line(screen, BLACK, (0, y), (SCREEN_WIDTH, y), LINE_WIDTH) if not game_over: curTime = time.time() if curTime - last_move_time > speed: if not pause: b = True last_move_time = curTime next_s = (snake[0][0] + pos[0], snake[0][1] + pos[1]) if next_s == food: # 吃到了食物 snake.appendleft(next_s) score += food_style[0] speed = orispeed - 0.03 * (score // 100) food = create_food(snake) food_style = get_food_style() else: if SCOPE_X[0] <= next_s[0] <= SCOPE_X[1] and SCOPE_Y[0] <= next_s[1] <= SCOPE_Y[1] \ and next_s not in snake: snake.appendleft(next_s) snake.pop() else: game_over = True # 畫食物 if not game_over: # 避免 GAME OVER 的時候把 GAME OVER 的字給遮住了 pygame.draw.rect(screen, food_style[1], (food[0] * SIZE, food[1] * SIZE, SIZE, SIZE), 0) # 畫蛇 for s in snake: pygame.draw.rect(screen, DARK, (s[0] * SIZE + LINE_WIDTH, s[1] * SIZE + LINE_WIDTH, SIZE - LINE_WIDTH * 2, SIZE - LINE_WIDTH * 2), 0) print_text(screen, font1, 30, 7, f'速度: {score//100}') print_text(screen, font1, 450, 7, f'得分: {score}') if game_over: if start: print_text(screen, font2, (SCREEN_WIDTH - fwidth) // 2, (SCREEN_HEIGHT - fheight) // 2, 'GAME OVER', RED) pygame.display.update() if __name__ == '__main__': main()
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python中subprocess介紹及如何使用詳細(xì)講解
在實際開發(fā)過程中,我們經(jīng)常會遇到需要從Python腳本中調(diào)用外部程序或腳本的場景,下面這篇文章主要給大家介紹了關(guān)于Python中subprocess介紹及如何使用詳細(xì)講解的相關(guān)資料,需要的朋友可以參考下2024-09-09Python中的Joblib庫使用學(xué)習(xí)總結(jié)
這篇文章主要介紹了Python中的Joblib庫使用學(xué)習(xí)總結(jié),Joblib是一組在Python中提供輕量級流水線的工具,Joblib已被優(yōu)化得很快速,很健壯了,特別是在大數(shù)據(jù)上,并對numpy數(shù)組進(jìn)行了特定的優(yōu)化,需要的朋友可以參考下2023-08-08Python數(shù)據(jù)分析庫pandas基本操作方法
下面小編就為大家分享一篇Python數(shù)據(jù)分析庫pandas基本操作方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04使用matplotlib修改坐標(biāo)軸,將y軸的間距設(shè)置為某一個值
這篇文章主要介紹了使用matplotlib修改坐標(biāo)軸,將y軸的間距設(shè)置為某一個值方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02Pandas缺失值填充 df.fillna()的實現(xiàn)
本文主要介紹了Pandas缺失值填充 df.fillna()的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Python提取特定時間段內(nèi)數(shù)據(jù)的方法實例
今天小編就為大家分享一篇關(guān)于Python提取特定時間段內(nèi)數(shù)據(jù)的方法實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04