学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。
可以在线运行,效果如下(在新页面中打开链接):
代码如下(用到平台自定义的xdf库):
from xdf import * import math mazeSpr = None rowArr = [] colArr = [] M = 2 N = 2 W = 768 H = 1024 CW = 20 CH = 20 OX = 0 OY = 0 R = int(CW / 2) linePool = [] lineArr = [] level = 1 start = circle(-100, -100, R, "red") end = circle(-100, -100, R, "green") tf = text("第1关", 0, 60, 50, "red") restartTf = text("重新开始", W, 60, 50, "red", "right") def xy2ij(x, y): return [int((x - OX) / CW), int((y - OY) / CH)] def ij2xy(i, j): return [OX + i * CW, OY + j * CH] def inittMaze(): global CW,CH,OX,OY,R CH = CW = int(min((W - 100) / M, (H - 100) / N)) OX = int((W - CW * M) / 2) OY = int((H - CH * N) / 2) R = int(CW / 2) moveTo(start, 0, 0) moveTo(end, M - 1, N - 1) start.size(R * 0.8) end.size(R) rowArr.clear() for i in range(M + 1): rowArr.append([]) for j in range(N): rowArr[i].append(1) colArr.clear() for i in range(M): colArr.append([]) for j in range(N+1): colArr[i].append(1) segment(0, M, 0, N) drawMaze() pass def drawMaze(): clearLine() for i in range(M + 1): for j in range(N): if rowArr[i][j] != 0: drawLine(i, j, i, j + 1) for i in range(M): for j in range(N + 1): if colArr[i][j] != 0: drawLine(i, j, i + 1, j) pass def segment(left, right, top, bottom): segI = -1 segJ = -1 if left == right - 1: for i in range(top + 1, bottom): colArr[left][i] = 0 return elif top == bottom - 1: for i in range(left + 1, right): rowArr[i][top] = 0 return segI = random(left + 1, right - 1) segJ = random(top + 1, bottom - 1) rand = random(0, 3) randIJ = 0 if rand != 0: randIJ = random(top, segJ - 1) rowArr[segI][randIJ] = 0 if rand != 1: randIJ = random(segJ, bottom - 1) rowArr[segI][randIJ] = 0 if rand != 2: randIJ = random(left, segI - 1) colArr[randIJ][segJ] = 0 if rand != 3: randIJ = random(segI, right - 1) colArr[randIJ][segJ] = 0 segment(left, segI, top, segJ) segment(segI, right, top, segJ) segment(left, segI, segJ, bottom) segment(segI, right, segJ, bottom) pass def clearLine(): linePool.extend(lineArr) lineArr.clear() pass def drawLine(i1, j1, i2, j2): p1 = ij2xy(i1, j1) p2 = ij2xy(i2, j2) setLine(getLine(), p1[0], p1[1], p2[0], p2[1]) def getLine(): li = None if len(linePool): li = linePool.pop() else: li = line(-100, -100, -200, -200, 5, "black") lineArr.append(li) return li def rad2ang(rad): return 180 * rad / math.pi def setLine(li, x0, y0, x1, y1): dx = x1 - x0 dy = y1 - y0 sz = math.sqrt(dx * dx + dy * dy) rot = math.atan2(dy, dx) li.move(x0, y0) li.rotate(rad2ang(rot) + 90) li.size(sz) pass def reset(): inittMaze() refresh() def refresh(): for li in linePool: li.hide() for li in lineArr: li.show() end.front() start.front() def nextLevel(): global level, M, N level += 1 M += 1 N += 1 tf.change("第" + str(level) + "关") reset() pass def moveTo(c, i, j): p = ij2xy(i + 0.5, j + 0.5) c.move(p[0], p[1]) def moveLeft(): move(rowArr, -1, 0) ''' ij = xy2ij(start.x, start.y) i = ij[0] j = ij[1] if rowArr[i][j] == 0: moveTo(start, i - 1, j) checkOver() ''' pass def moveRight(): move(rowArr, 1, 0) ''' ij = xy2ij(start.x, start.y) i = ij[0] j = ij[1] if rowArr[i + 1][j] == 0: moveTo(start, i + 1, j) checkOver() ''' pass def moveUp(): move(colArr, 0, -1) ''' ij = xy2ij(start.x, start.y) i = ij[0] j = ij[1] if colArr[i][j] == 0: moveTo(start, i, j - 1) checkOver() ''' pass def moveDown(): move(colArr, 0, 1) ''' ij = xy2ij(start.x, start.y) i = ij[0] j = ij[1] if colArr[i][j + 1] == 0: moveTo(start, i, j + 1) checkOver() ''' pass def move(arr, di, dj): ij = xy2ij(start.x, start.y) i = ij[0] j = ij[1] ti = i + di tj = j + dj ii = i if di <= 0 else ti jj = j if dj <= 0 else tj if arr[ii][jj] == 0: moveTo(start, ti, tj) checkOver() pass def checkOver(): p1 = xy2ij(start.x, start.y) p2 = xy2ij(end.x, end.y) if p1[0] == p2[0] and p1[1] == p2[1]: nextLevel() pass def keyup(key, code): if key == 37: moveLeft() elif key == 38: moveUp() elif key == 39: moveRight() elif key == 40: moveDown() elif key == 82 or key == 32: reset() pass restartTf.tap = reset reset() class MyKeyBoard: def __init__(self, x, y, w): hw = w / 2 hhw = w * 0.75 self.arr = [ box(x, y - w, w, w, "white", "red"), text("↑", x + hw, y - w + hhw, w, "center", "red"), box(x, y, w, w, "white", "red"), text("↓", x + hw, y + hhw, w, "center", "red"), box(x - w, y, w, w, "white", "red"), text("←", x - hw, y + hhw, w, "center", "red"), box(x + w, y, w, w, "white", "red"), text("→", x + w + hw, y + hhw, w, "center", "red"), ] self.arr[0].tap=self.clickUp self.arr[2].tap=self.clickDown self.arr[4].tap=self.clickLeft self.arr[6].tap=self.clickRight delay(self.loop, 100) def loop(self): for i in range(len(self.arr)): self.arr[i].front() delay(self.loop, 100) def onKeydown(self, keyCode, key): try: keyup(keyCode, key) except e: pass def clickUp(self): self.onKeydown(38, "ArrowLeft") def clickDown(self): self.onKeydown(40, "ArrowDown") def clickLeft(self): self.onKeydown(37, "ArrowLeft") def clickRight(self): self.onKeydown(39, "ArrowRight") MyKeyBoard(768-160, 1024-80, 80)
也可以下载代码,本地python环境运行(用pygame封装了xdf库)。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。