15
2024
11

python编程学习-贪食蛇

学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。

可以在线运行,效果如下(在新页面中打开链接):

代码如下(用到平台自定义的xdf库):

from xdf import *

cubes = []
data = []
M = 16
N = 12
W = 64
oxy=0.001
stepN = 20
count = 0
isOver = False
snake = []
face = 1
level = 1
score = 0

def ij2xy(i, j):
    return (j * W+oxy, i * W+oxy)

def xy2ij(x, y):
    return (int((x-oxy)//W), int((j-oxy)//W))

def ij2n(i, j):
    return i * N + j

def n2ij(n):
    return (int(n//N), n%N)

for i in range(M):
    for j in range(N):
        xy = ij2xy(i,j)
        cb = box(xy[0],xy[1], W, W, "red")
        cubes.append(cb)
        data.append(0)
overTxt = text("Game Over", 100, "green", "center")
levalTxt = text("第1关", 1, 80, 50, "green")
scoreTxt = text("得分:0", 1, 150, 50, "green")

def reset():
    global data, snake, isOver, overTxt, stepN, count, face
    global level, score
    n = len(data)
    for i in range(n):
        data[i] = 0
    snake = [ij2n(9, 6),ij2n(8, 6),ij2n(7, 6)]
    setBySnake()
    isOver = False
    overTxt.hide()
    stepN = 20
    count = 0
    face = 1
    level = 1
    score = 0
    addFood()
    addFood()

def move():
    global count, snake, face, data
    count = 0
    n = len(snake)
    head = snake[n-1]
    foot = snake[0]
    ij = n2ij(head)
    i = ij[0]
    j = ij[1]
    ti = i
    tj = j
    if face == 1:
        ti -= 1
    elif face == 2:
        tj += 1
    elif face == 3:
        ti += 1
    elif face == 4:
        tj -= 1
    #
    tn = ij2n(ti, tj)
    #
    if ti < 0 or ti >= M or tj < 0 or tj >= N:
        #print("撞墙了")
        #print(i, j, n, tn)
        #print(ti, tj, tn)
        #print(head, foot)
        #print(data)
        #print(snake)
        gameOver()
    elif data[tn] == 1 and tn != foot:
        #print("撞自己了")
        #print(i, j, n, tn)
        #print(ti, tj, tn)
        #print(head, foot)
        #print(data)
        #print(snake)
        gameOver()
    elif data[tn] == 2:
        snake.append(tn)
        setBySnake()
        addFood()
        sddScore()
    else:
        clearBySnake()
        for i in range(n-1):
            snake[i] = snake[i + 1]
        snake[n-1]=tn
        setBySnake()

def sddScore():
    global score, level, count, stepN, snake
    score += 1
    if len(snake) >= 13:
        level += 1
        count = 0
        score += 10
        if stepN > 1:
            stepN -= 1
        clearBySnake()
        snake = [snake.pop(), snake.pop(), snake.pop()]
        snake.reverse()
        setBySnake()

def addFood():
    global data
    l = len(data)
    n = random(l) - 1
    while data[n] != 0:
        n = random(l) - 1
    data[n] = 2

def gameOver():
    global isOver, overTxt
    isOver = True
    overTxt.show()

def setFace(v):
    global face
    if abs(v-face) == 2:
        return
    #if face == v:
    #    move()
    face = v
    move()

def keydown(keycode, key):
    if keycode == 37:
        setFace(4)
    elif keycode == 38:
        setFace(1)
    elif keycode == 39:
        setFace(2)
    elif keycode == 40:
        setFace(3)
    elif keycode == 32:
        global isOver
        if isOver:
            reset()

def keyup(keycode, key):
    pass
    

def setBySnake():
    updateBySnake(1)
        
def clearBySnake():
    updateBySnake(0)
        
def updateBySnake(v):
    global snake, data
    l = len(snake)
    for i in range(l):
        n = snake[i]
        data[n] = v

def loop():
    global isOver, count, stepN, data, cubes, level, score
    if isOver:
        return
    count += 1
    if count >= stepN:
        move()
    n = len(data)
    for i in range(n):
        s = data[i]
        if s == 2:
            cubes[i].change("blue")
        elif s == 1:
            cubes[i].change("white")
        else:
            cubes[i].change("red")
    levalTxt.change("第"+str(level)+"关")
    scoreTxt.change("得分:"+str(score))

reset()

也可以下载代码,本地python环境运行(用pygame封装了xdf库)。



« 上一篇下一篇 »

相关文章:

python编程学习-赛车  (2024-11-15 11:39:38)

python编程学习-警察抓小偷  (2024-11-15 11:38:7)

python编程学习-英雄天梯  (2024-11-15 11:37:35)

python编程学习-翻滚的方块  (2024-11-15 11:37:10)

python编程学习-玛丽跳跳跳  (2024-11-15 11:36:43)

python编程学习-汉诺塔  (2024-11-15 11:36:16)

python编程学习-模拟键盘按键  (2024-11-15 11:35:50)

python编程学习-显示帧频  (2024-11-15 11:35:26)

python编程学习-收不完的西瓜  (2024-11-15 11:35:0)

python编程学习-推箱子  (2024-11-15 11:34:1)

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。