15
2024
11

python编程学习-警察抓小偷

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

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

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

from xdf import *
from math import *
W = 768
H = 1024
CX = W / 2
CY = H / 2
ps = []
ls = [[0,1],[0,3],[0,4], [1,2], [2,3], [3,4], [4,5], [5,1]]
r = 64
R = 256
cur = None
isOver = False
isMoving = False
y0 = 10
ps.append([CX, CY])
for i in range(5):
    ang = pi*i*2/5-pi
    ps.append([CX - R * sin(ang), CY + R * cos(ang)])
    pass
for a in ls:
    line(ps[a[0]][0], ps[a[0]][1], ps[a[1]][0], ps[a[1]][1])
    pass
for a in ps:
    circle(a[0], a[1], r, "white", "black")
    pass
thief = [
    circle(ps[1][0], ps[1][1], r, "white", "black"),
    circle(ps[1][0], ps[1][1], r, "white", "red"),
    text("小偷", ps[1][0], ps[1][1] + y0, 30, "center"),
    1
]
police = [
    circle(ps[0][0], ps[0][1], r, "white", "black"),
    circle(ps[0][0], ps[0][1], r, "white", "red"),
    text("警察", ps[0][0], ps[0][1] + y0, 30, "center"),
    0
]
winTf = text("游戏结束","red","center", W / 2, 100, 50)
AI = None
def chooseAI():
    global AI
    a = input("选择角色,1、小偷,2、警察")
    if a and a == "1":
        AI = police
    else:
        AI = thief
    setTurn(thief)
    pass
def setTurn(v):
    global cur
    if v == thief:
        cur = thief
        thief[1].show()
        police[1].hide()
    else:
        cur = police
        thief[1].hide()
        police[1].show()
    if AI == cur:
        aiTurn()
    pass
def move(v, i, dt=0):
    a = ps[i]
    v[0].move(a[0], a[1], dt)
    v[1].move(a[0], a[1], dt)
    v[2].move(a[0], a[1] + y0, dt),
    v[3] = i
    pass

def reset():
    global AI,isMoving
    AI = None
    isMoving = False
    winTf.hide()
    move(thief, 1)
    move(police, 0)
    chooseAI()

def tap():
    global isOver, AI, isMoving
    if isOver:
        isOver = False
        reset()
        return
    if not AI:
        return
    if isMoving:
        return
    if cur == AI:
        return
    for i in range(len(ps)):
        a = ps[i]
        dx = x - a[0]
        dy = y - a[1]
        if dx * dx + dy * dy < r * r and canMove(cur, i):
            move(cur, i, 300)
            checkWin()
            break
        pass
    pass
def canMove(c, i):
    if not inLine(i, c[3]):
        return False
    if c == thief and police[3] == i:
        return False
    return True

def inLine(a, b):
    for c in ls:
        if (c[0] == a and c[1] == b) or (c[0] == b and c[1] == a):
            return True
    return False

def checkWin():
    global isOver,isMoving
    if police[3] == thief[3]:
        winTf.show()
        isOver = True
    else:
        isMoving = True
        delay(swapTurn, 500)
    pass
def swapTurn():
    global isMoving
    isMoving = False
    setTurn(police if cur == thief else thief)
def aiTurn():
    delay(aiTap, 2000)

def getAllPath(c):
    arr = []
    for i in range(len(ps)):
        if canMove(c, i):
            arr.append(i)
    return arr

def choosePos(arr, b, i):
    if len(arr) == 1:
        return arr[0]
    aa = []
    for a in arr:
        d = dm[i][a]
        if b:
            if d == 0:
                return a
            if d == 2 and a == 0:
                return a
        if d == 2:
            aa.append(a)
    if len(aa) > 0:
        return aa[random(len(aa)) - 1]
    return arr[random(len(arr)) - 1]

def aiTap():
    arr = getAllPath(AI)
    other = police if AI==thief else thief
    p = choosePos(arr, AI==police, other[3])
    move(AI, p, 300)
    checkWin()
    pass

dm = [[0,1,2,1,1,2],[1,0,1,2,2,1],[2,1,0,1,2,2],[1,2,1,0,1,2],[1,2,2,1,0,1],[2,1,2,2,1,0]]

reset()

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



« 上一篇下一篇 »

相关文章:

python编程学习-贪食蛇  (2024-11-15 11:38:41)

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)

python编程学习-拼图  (2024-11-15 11:33:25)

发表评论:

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