学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。
可以在线运行,效果如下(在新页面中打开链接):
代码如下(用到平台自定义的xdf库):
from xdf import * L = 32 SQRT3 = 1.732 CW = L * SQRT3 CH = L * 2 M = int(1024 / CH) - 1 N = int(768 / CW) ox = (1024 - CH * M) * 0.5 + 0.001 oy = (768 - CW * N) * 0.5 + 0.001 def ij2xy(i, j): if j % 2 == 0: return ((j + 0.5) * CW + ox, (i + 0.5) * CH + oy) return ((j + 0.5) * CW + ox, (i + 1.0) * CH + oy) def xy2ij(x, y): i0 = int((y - oy) / CH) - 1 j0 = int((x - ox) / CW) - 1 d0 = CH * CH i2 = i0 j2 = j0 for i in range(3): for j in range(3): i1 = i0 + i j1 = j0 + j xy = ij2xy(i1, j1) dx = x - xy[0] dy = y - xy[1] d = dx * dx + dy * dy if d < d0: d0 = d i2 = i1 j2 = j1 return (i2, j2) def ij2n(i, j): return i * N + j def n2ij(n): return (n // N, n % N) cubes = [] for i in range(M): for j in range(N): xy = ij2xy(i, j) cb1 = box(xy[0] - L, xy[1] - L * 0.5, CW, L, '#CCFF00') cb2 = box(xy[0] - L, xy[1] - L * 0.5, CW, L, '#CCFF00') cb3 = box(xy[0] - L, xy[1] - L * 0.5, CW, L, '#CCFF00') cb1.rotate(90) cb2.rotate(210) cb3.rotate(330) cubes.append([cb1, cb2, cb3, i, j, 0]) ci = int(M/2) cj = int(N/2) xy = ij2xy(ci, cj) cat = [ circle(xy[0], xy[1], L * 0.5, "red"), ci, cj ] def tap(): global state, cooldown if state == 1: ij = xy2ij(x, y) i = ij[0] j = ij[1] if i >= 0 and i < M and j >= 0 and j < N and (cat[1] != i or cat[2] != j): n = ij2n(i, j) cb = cubes[n] if cb[5] == 0: setCube(cb, 1) cooldown = 20 state = 2 checkWin() elif state == 3: reset() state = 1 cooldown = 20 NEIGHBOR_01=[[-1,-1],[0,-1], [-1,0],[1,0], [-1,1],[0,1]] NEIGHBOR_02=[[0,-1],[1,-1], [-1,0],[1,0], [0,1],[1,1]] winTxt = text("You Win", 100, "red", "center") loseTxt = text("You Lose", 100, "red", "center") def reset(): global state, cooldown, ci, cj winTxt.hide() loseTxt.hide() state = 1 cooldown = 20 catMoveTo(ci, cj) for i in range(len(cubes)): setCube(cubes[i], 0) mm = 0 while mm < 6: mm += 1 n0 = random(M*N - 1) ij = n2ij(n0) if cat[1] == ij[0] and cat[2] == ij[1]: mm -= 1 elif cubes[n0][5] != 0: mm -= 1 else: setCube(cubes[n0], 1) def loop(): global state, cooldown if state == 1: #等待点击 pass elif state == 2: cooldown -= 1 if cooldown == 0: state = 1 catMove() def catMove(): ij = getPath() if ij[0] == -1 and ij[1] == -1: ij = getRandomPath() i = ij[0] j = ij[1] if i == cat[1] and j == cat[2]: win() else: catMoveTo(i, j) if i == 0 or i == M - 1 or j == 0 or j == N - 1: lose() def catMoveTo(i, j): xy = ij2xy(i, j) cat[0].move(xy[0], xy[1]) cat[1] = i cat[2] = j def checkWin(): i = cat[1] j = cat[2] def win(): global state state = 3 winTxt.show() def lose(): global state state = 3 loseTxt.show() def setCube(cb, ty): cb[5] = ty if ty == 0: cb[0].change('#CCFF00') cb[1].change('#CCFF00') cb[2].change('#CCFF00') else: cb[0].change('#778B01') cb[1].change('#778B01') cb[2].change('#778B01') def getRandomPath(): i0 = cat[1] j0 = cat[2] nextI = i0 nextJ = j0 neighbor= NEIGHBOR_01 if j0 % 2 != 0: neighbor = NEIGHBOR_02 for i in range(len(neighbor)): ii = i0 + neighbor[i][0] jj = j0 + neighbor[i][1] if ii < 0 or jj < 0 or ii >= M or jj >= N: continue n = ij2n(ii, jj) cb = cubes[n] if cb[5] != 0: continue nextI = ii nextJ = jj break return (nextI, nextJ) #寻路 def getPath(): startP= [cat[1], cat[2], None] openTable=[] tempTable=[] openTable.append(startP) stepTable=[] for i in range(M): stepTable.append([]) for j in range(N): stepTable[i].append(0) exitP = [-1, -1, None] canMove = False n = 0 out = True while(out): n += 1 while(out): if len(openTable) == 0: break p = openTable.pop() neighbor = NEIGHBOR_01 if p[1] % 2 != 0: neighbor = NEIGHBOR_02 for i in range(6): ii = p[0] + neighbor[i][0] jj = p[1] + neighbor[i][1] if ii < 0 or jj < 0 or ii >= M or jj >= N: continue if ii == startP[0] and jj == startP[1]: continue if ii == cat[1] and jj == cat[2]: continue if cubes[ij2n(ii, jj)][5] != 0: continue if stepTable[ii][jj] != 0: continue p1= [ii, jj, p] tempTable.append(p1) stepTable[ii][jj] = n canMove = True if ii==0 or jj==0 or ii == M - 1 or jj == N - 1: exitP = p1 out = False break if len(tempTable) == 0: break; if not out: break; openTable=tempTable tempTable=[] if not canMove: #print("没有路了") return (cat[1], cat[2]) if exitP[2] == None: #print("没有出口") return (-1, -1) #print("出口位置:", exitP) nextP = exitP while nextP[2] != startP: nextP = nextP[2] return (nextP[0], nextP[1]) reset()
也可以下载代码,本地python环境运行(用pygame封装了xdf库)。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。