Python貪吃蛇小遊戲例項分享

2022-09-25 03:54:06 字數 4285 閱讀 6375

pygame 、random 、pyautogui

1.設定初始引數

設定每個網格大小為 20 px ,size可以隨意修改但最好為20的倍數,設定初始方向向右,初始蛇長為 3 。

# 初始化引數

size = 320

screen = pygame.display.set_mode([size,size],0 , 32)

pygame.display.set_caption("貪吃蛇")

cell_size = 20

cell_num = int(size/20)

x , y = [60,0]

# 初始方向向右

dir_snake = 'r'

body_snake = [[0,0],[20,0],[40,0],[60,0]]

# 初始蛇長為3

len_snake = 3

# 初始食物座標

food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]

# 蛇移動速度

clock = pygame.time.clock()

2.鍵盤控制

鍵盤上下左右控制蛇方向,禁止反向。

3.食物

蛇頭吃到食物後,蛇長加一 ,random 乙個隨機座標,如果座標再蛇身上則繼續 random。

if [x,y] == food_coor[:2]:

food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]

if food_coor[:2] in body_snake[-2:-len_snake -1 :-1]:

food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]

len_snake +=1

4. 勝負判斷

蛇頭與蛇身碰撞判輸,彈出遊戲結束視窗,說明最終蛇長為多長。

if body_snake[-1] in body_snake[-2:-len_snake -1 :-1]:

pyautogui.alert(text='遊戲結束,最終蛇長為{}'.format(len_snake))

exit()

**import pygame , random ,pyautogui

from pygame.locals import *

# 初始化引數

size = 500

screen = pygame.display.set_mode([size,size],0 , 32)

pygame.display.set_caption("貪吃蛇")

cell_size = 20

cell_num = int(size/20)

x , y = [60,0]

# 初始方向向右

dir_snake = 'r'

body_snake = [[0,0],[20,0],[40,0],[60,0]]

# 初始蛇長為3

len_snake = 3

# 初始食物座標

food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]

# 蛇移動速度

clock = pygame.time.clock()

# bg

def bg():

for i in range(0, size , int(cell_size)):

pygame.draw.line(screen,[72,72,72],[i,0],[i,size])

for i in range(0, size, int(cell_size)):

pygame.draw.line(screen, [72, 72, 72], [0, i], [size,i])

# 蛇def snake(k):

for x,y in k[:-len_s程式設計客棧nake - 1:-1]:

pygame.draw.rect(screen,[255,255,255],[x,y,20,20],0)

# 食物

def food():

pygame.draw.rect(screen,[255,0,0],food_coor,0)

# 主迴圈

def running():

global x ,y , dir_snake ,food_coor , len_snake

while true:

for event in pygame.event.get():

if event.type == quit:

exit()

screen.fill((40, 43, 46))

# 方向規則

if dir_snake == 'u':

y -= 20

elif dir_snake == 'r':

x += 20

elif dir_snake == 'd':

y += 20

eli程式設計客棧f dir_snake == 'l':

x -= 20

# 鍵盤方向控制

if event.type == yyvacpxliokeydown:

if event.key == k_left and dir_snake != 'r':

dir_snake = 'l'

elif event.key == k_down and dir_snake != 'u':

dir_snake = 'd'

elif event.key == k_right and dir_snake != 'lyyvacpxlio':

dir_snake = 'r'

elif event.key == k_up and dir_snake != 'd':

dir_snake = 'u'

# 撞牆之後

if x < 0:

x += size

elif x >= size:

x -= size

elif y < 0:

y += size

elif y >= size:

y -= size

# draw蛇體

body_snake.append([x,y])

snake(body_snake)

food()

# 吃到食物後food換位置

if [x,y] == food_coor[:2]:

food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]

if food_coor[:2] in body_snake[-2:-len_snake -1 :-1]:

food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]

len_snake +=1

# 遊戲結束

if body_snake[-1] in body_snake[-2:-len_snake -1 :-1]:

pyautogui.alert(text='遊戲結束,最終蛇長為{}'.format(len_snake))

exit()

# 格線

bg()

# 右下角顯示蛇長

font = pygame.font.sysfont("simsunnsimsun", 40)

text_su***ce = font.render("{}".format(len_snake), true, (255,255, 255))

screen.blit(text_su***ce , (size-40,size-40))

pygame.display.update()

# 蛇的移動速度隨著蛇的長度而越來越快

clock.tick(len_snake * 2)

if __name__ == www.cppcns.com'__main__':

pygame.init()

running()

輸出結果

本文標題: python貪吃蛇小遊戲例項分享

本文位址:

貪吃蛇小遊戲

1 doctype html 2 html 3 head 4 title snake title 5head 6 body style text align center margih 100px background color aaaaaa 7 canvas id canv width 400 ...

C 貪吃蛇小遊戲

自學了幾天c 嘗試寫了乙個貪吃蛇小遊戲,比較詳細地做了注釋,實現 1 貪吃蛇基本功能 2 長按加速 3 計分板 xaml 及介面 namespace 貪吃蛇 新食物生成 public void newfoodgenerate 計時器每乙個計時週期內的時間處理程式 void timer tick ob...

貪吃蛇小遊戲實現

index.html style.css mapindex.js tools.js 自呼叫函式傳入window的目的,是讓變數可以被壓縮 防止undefined重新命名,也可以被壓縮 function window,undefined 暴露tools給window window.tools tool...