首頁 > 軟體

python實現簡單貪吃蛇小遊戲

2022-06-08 18:02:38

本文範例為大家分享了python實現簡單貪吃蛇的具體程式碼,供大家參考,具體內容如下

1. 匯入遊戲庫

import pgzrun
import random

2.遊戲初始化

# 視窗大小
WIDTH = 600
HEIGHT = 480
# 貪吃蛇
bodys = [ [100,100],[80,100], [60,100], [40,100], [20,100]]
head = [100,100]
d = 'right'
# 食物
food = [290,290]
# 得分
score=0

3.遊戲視窗繪製

def draw():
#清空螢幕
screen.clear()
# 繪製網格
for i in range(1,24):
screen.draw.line((0,i*20),(600,i*20),'gray')
for i in range(1,30):
screen.draw.line((i*20,0),(i*20,480),'gray')
#繪製蛇
for body in bodys:
rect = Rect(body,(20,20))
screen.draw.filled_rect(rect,(0,0,255))
inner = [body[0]+2,body[1]+2]
rect = Rect(inner, (15, 15),center='center')
screen.draw.filled_rect(rect, (0, 0, 180))
# 繪製頭
rect = Rect(head, (20, 20))
screen.draw.filled_rect(rect, (0, 200,0))
inner = [head[0] + 2, head[1] + 2]
rect = Rect(inner, (15, 15))
screen.draw.filled_rect(rect, (0, 255, 12))
# 繪製食物
screen.draw.filled_circle(food,10, '#ffddee')
# 繪製得分
screen.draw.text('score:'+str(score),(20, 20), color="orange",fontsize=30)

4.蛇的移動功能

def run():
global food,d,head,bodys,score
# 新增一個格子的身體
if d=='right':
head[0] += 20
elif d=='left':
head[0] -= 20
elif d=='up':
head[1] -= 20
else:
head[1] += 20
bodys.insert(0,list(head))
if head[0] == food[0]-10 and head[1] == food[1] - 10:
food = [random.randint(1,30)*20-10,random.randint(1,20)*20-10]
score+=1
if score>3:
clock.unschedule(run)
clock.schedule_interval(run, 0.1)
else:
bodys.pop()
# 撞牆後重新開始
if head[0]<0 or head[0]>580 or head[1]<0 or head[1]>480 or head in
bodys[1:]:
# 蛇回到初始位置
bodys = [[100, 100], [80, 100], [60, 100], [40, 100], [20, 100]]
head = [100, 100]
# 方向向右
d = 'right'
# 得分清零
score=0
clock.unschedule(run)
clock.schedule_interval(run, 0.3)

5.按鍵控制蛇的行走方向

# 按鍵控制蛇的行走方向
def on_key_down(key):
global d
# 改變方向
if key == keys.DOWN and d != 'up':
d = 'down'
if key == keys.UP and d != 'down':
d = 'up'
if key == keys.LEFT and d != 'right':
d = 'left'
if key == keys.RIGHT and d != 'left':
d = 'right'

6.啟動遊戲

# 定時設定
clock.schedule_interval(run, 0.3)
# 播放音樂
music.play('music.mp3')
# 啟動遊戲
pgzrun.go()

完整程式碼

# 1.匯入庫
import pgzrun
import random

# 2.初始化
# 視窗大小
WIDTH = 600
HEIGHT = 480
# 貪吃蛇
bodys = [[100, 100], [80, 100], [60, 100], [40, 100], [20, 100]]
head = [100, 100]
d = 'right'
# 食物
food = [290, 290]
# 得分
score = 0

# 3.遊戲視窗繪製
def draw():
    # 清空螢幕
    screen.clear()
    # 繪製網格
    for i in range(1, 24):
        screen.draw.line((0, i * 20), (600, i * 20), 'gray')
    for i in range(1, 30):
        screen.draw.line((i * 20, 0), (i * 20, 480), 'gray')
    # 繪製蛇
    for body in bodys:
        rect = Rect(body, (20, 20))
        screen.draw.filled_rect(rect, (0, 0, 255))
        inner = [body[0] + 2, body[1] + 2]
        rect = Rect(inner, (15, 15), center='center')
        screen.draw.filled_rect(rect, (128, 0, 128)) # 紫色

    # 繪製頭
    rect = Rect(head, (20, 20))
    screen.draw.filled_rect(rect, (0, 200, 0))
    inner = [head[0] + 2, head[1] + 2]
    rect = Rect(inner, (15, 15))
    screen.draw.filled_rect(rect, (0, 255, 12))
    # 繪製食物 顏色為紅色
    screen.draw.filled_circle(food, 10, '#ff0000')
    # 繪製得分
    screen.draw.text('score:' + str(score), (20, 20), color="red", fontsize=30)

# 4.蛇的移動功能
def run():
    global food, d, head, bodys, score
    # 新增一個格子的身體
    if d == 'right':
        head[0] += 20
    elif d == 'left':
        head[0] -= 20
    elif d == 'up':
        head[1] -= 20
    else:
        head[1] += 20
    bodys.insert(0, list(head))
    if head[0] == food[0] - 10 and head[1] == food[1] - 10:
        food = [random.randint(1, 30) * 20 - 10, random.randint(1, 20) * 20 - 10]
        score += 1
        if score > 10:
            clock.unschedule(run)
            clock.schedule_interval(run, 0.1)
    else:
        bodys.pop()
    # 撞牆後重新開始
    if head[0] < 0 or head[0] > 580 or head[1] < 0 or head[1] > 480 or head in bodys[1:]:
        # 蛇回到初始位置
        bodys = [[100, 100], [80, 100], [60, 100], [40, 100], [20, 100]]
        head = [100, 100]
        # 方向向右
        d = 'right'
        # 得分清零
        score = 0
        clock.unschedule(run)
        clock.schedule_interval(run, 0.3)


# 按鍵控制蛇的行走方向
def on_key_down(key):
    global d
    # 改變方向
    if key == keys.DOWN and d != 'up':
        d = 'down'
    if key == keys.UP and d != 'down':
        d = 'up'
    if key == keys.LEFT and d != 'right':
        d = 'left'
    if key == keys.RIGHT and d != 'left':
        d = 'right'

# 6.啟動遊戲
# 定時設定
clock.schedule_interval(run, 0.3)
# 播放音樂
music.play('music.mp3')
# 啟動遊戲
pgzrun.go()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com