15
2024
11

python编程学习-密室逃生

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

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

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

from xdf import *

W0 = 768
H0 = 1024
N = 12 #玩不过去,可以改小一点,比如8,要能被W0整除
CW = W0 / N
R = CW / 4
offX = 0.001
offY = 0.001

cubesUp = []
cubesDown = []
mpUp = []
mpDown = []
offUp = 0
offUp0 = 0
countDown = 100
rockY = 0
isOver = False
heroX = 0

for i in range(N):
    b0 = box(offX, offY, CW, H0, "gray", "black")
    cubesUp.append(b0)
    b1 = box(offX, offY, CW, H0, "gray", "black")
    cubesDown.append(b1)
    mpUp.append(0)
    mpDown.append(0)

hero = circle(W0 / 2 + offX, H0 / 2 + offY - R, R, "green", "black")
overTxt = text("Game Over", 100, "red", "center")

def x2i(x):
    return int((x - offX) / CW)

def i2x(i):
    return i * CW + offX + CW / 2

def refresh():
    global cubesUp,cubesDown,offUp,offY,offX,rockY,CW
    for i in range(N):
        b0 = cubesUp[i]
        b1 = cubesDown[i]
        oy0 = mpUp[i] + offUp + offY + rockY
        oy1 = H0 + mpDown[i] + offY
        xi = CW * i + CW / 2 + offX
        b0.move(xi, oy0)
        b1.move(xi, oy1)

def reset():
    global isOver,overTxt,countDown,rockY
    isOver = False
    countDown = 100
    rockY = 0
    initMap()
    refresh()
    overTxt.hide()
        
def tap():
    global isOver,countDown
    if isOver:
        reset()
    else:
        if countDown >= 25:
            i = x2i(x)
            moveHero(i)
        
def gameOver():
    global isOver,overTxt
    isOver = True
    overTxt.show()
    
def checkOver():
    global heroX,mpUp,mpDown
    i = x2i(heroX)
    if mpUp[i] == mpDown[i]:
        gameOver()
    
def loop():
    global countDown,offUp,offUp0,rockY, isOver
    if isOver:
        return
    countDown = countDown - 1
    if countDown > 50:
        pass
    elif countDown > 30:
        rockY = random(20) - 1 - 20 / 2
    elif countDown >= 25:
        rockY = 0
        offUp = offUp0 * (countDown - 25) / 5
    elif countDown == 20:
        checkOver()
    elif countDown < 0:
        countDown = 0
        reset()
    refresh()

def initMap():
    global hero,mpUp, mpDown, offUp, offUp0, rockY
    rockY = 0
    offUp0 = offUp = -H0 / 2 + CW * 2
    for i in range(N):
        oyi = random(3) - 2
        mpUp[i] = oyi * CW
        mpDown[i] = oyi * CW
    for i in range(2):
        mi = random(N) - 1
        mpUp[mi] = mpDown[mi] - CW * random(2)

    md = int(N // 2) - 1
    moveHero(md)
    
def moveHero(i):
    global heroX,hero,mpDown
    if i < 0 or i >= len(mpDown):
        return
    heroX = i2x(i)
    hero.move(heroX, H0 / 2 - R + mpDown[i])

reset()

# xdf库的坑
# 1、坐标为0会被忽略掉,导致出问题,所以加了个offX,offY
# 2、创建的时候,box是以左上角为锚点的,move的时候,是以中心为锚点的
# 3、random函数,正常random(n)是生成[0,n-1]的随机(整)数,
#    xdf库中random(n)生成的是[1,n]的随机(整)数

 

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



« 上一篇下一篇 »

相关文章:

python编程学习-对角棋  (2024-11-15 11:32:8)

python编程学习-围住神经猫  (2024-11-15 11:31:6)

python编程学习-四子棋  (2024-11-15 11:30:32)

python编程学习-华容道  (2024-11-14 16:43:25)

python编程学习-动画  (2024-11-14 16:39:37)

python编程学习-别踩白块  (2024-11-14 16:38:59)

python编程学习-俄罗斯方块  (2024-11-14 16:38:19)

python编程学习-井字棋  (2024-11-14 16:34:9)

python编程学习-五子棋  (2024-11-14 16:33:23)

python编程学习-乒乓  (2024-11-14 16:32:46)

发表评论:

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