昆明做網(wǎng)站建設(shè)的公司全國疫情最新消息今天新增
前言
大家早好、午好、晚好吖 ? ~
現(xiàn)在這年頭,無論玩?zhèn)€什么游戲都有健康機制,
這讓我們愉悅玩游戲得步伐變得承重起來,
于是無聊之下我寫了個貪吃蛇小游戲,來玩?zhèn)€快樂
代碼展示
導(dǎo)入模塊
import random
import sys
import time
import pygame
from pygame.locals import *
from collections import deque
( 源碼、教程、文檔、軟件點擊此處跳轉(zhuǎn)跳轉(zhuǎn)文末名片加入君羊,找管理員小姐姐領(lǐng)取呀~ )
外框設(shè)定
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 snakedef 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_ydef 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 = Truestart = False # 是否開始,當(dāng)start = True,game_over = True 時,才顯示 GAME OVERscore = 0 # 得分orispeed = 0.5 # 原始速度speed = orispeedlast_move_time = Nonepause = 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 = Truegame_over = Falseb = Truesnake = init_snake()food = create_food(snake)food_style = get_food_style()pos = (1, 0)# 得分score = 0last_move_time = time.time()elif event.key == K_SPACE:if not game_over:pause = not pauseelif event.key in (K_w, K_UP):# 這個判斷是為了防止蛇向上移時按了向下鍵,導(dǎo)致直接 GAME OVERif b and not pos[1]:pos = (0, -1)b = Falseelif event.key in (K_s, K_DOWN):if b and not pos[1]:pos = (0, 1)b = Falseelif event.key in (K_a, K_LEFT):if b and not pos[0]:pos = (-1, 0)b = Falseelif 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 = Truelast_move_time = curTimenext_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()
尾語 💝
好了,今天的分享就差不多到這里了!
完整代碼、更多資源、疑惑解答直接點擊下方名片自取即可。
有更多建議或問題可以評論區(qū)或私信我哦!一起加油努力叭(? ?_?)?
喜歡就關(guān)注一下博主,或點贊收藏評論一下我的文章叭!!!