学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。
可以在线运行,效果如下(在新页面中打开链接):
代码如下(用到平台自定义的xdf库):
from xdf import * import math SW = 768 SH = 1024 R = SW / 4 R2 = R * 2 RR = R * R HFR = R / 2 SQR3 = math.sqrt(3) SQR32 = SQR3 / 2 SQR3R = SQR3 * R SQR32R = SQR32 * R FY = 800 COLOR = 'blue' COLOR1 = 'white' SPEED = 1 SPEED_CANNON = 20 COOL_DOWN = 10 GRAVITY = 1 JUMP_SPEED = -10 SPEED_X = 2 rect = box(0, FY, SW, SH - FY, COLOR) sx = 0 waves = [ circle(R / 2, FY + SQR32R, R, COLOR), circle(3 * R / 2, FY - SQR32R, R, COLOR1), circle(5 * R / 2, FY + SQR32R, R, COLOR), circle(7 * R / 2, FY - SQR32R, R, COLOR1), circle(9 * R / 2, FY + SQR32R, R, COLOR), circle(11 * R / 2, FY - SQR32R, R, COLOR1), ] def getByX(xx): n0 = xx // R x0 = R / 2 + n0 * R dr = 1 - (n0 % 2) * 2 y0 = FY + dr * SQR32R dx = xx - x0 dy = math.sqrt(RR - dx * dx) ty = FY + dr * (SQR32R - dy) ang = -dr * math.atan2(dy, dx) + dr * math.pi / 2 ang = ang * 180 / math.pi return [ty, ang] by = 100 ba = 100 boat = stamp("boat001@001N", 0, SH) boat.vy = 0 boat.isJump = False boat.ox = 100 life = 5 lifes = [] for i in range(life): lf = stamp("heart001@001P", 15 + i * 25, 15, 20) lifes.append(lf) def updateLife(): i = 0 for lf in lifes: if i < life: lf.show() else: lf.hide() i += 1 isOver = False overTf = text("Game Over", 50, "red", "center") score = 0 scoreTf = text("得分", 10, 60, 30, "red") fishes = [] fishPool = [] def getFish(): if len(fishPool): return fishPool.pop() return stamp("fish001@001O") def addFish(): fish = getFish() fish.vx = -(1 + random(10) / 10) * SPEED fx = SW fishes.append(fish) x0 = sx + SW + 50 a0 = getByX(x0) fish.move(fx, a0[0]) fish.rotate(a0[1]) fish.show() addFish() def updateFish(): for fish in fishes: fx = fish.x + fish.vx x0 = sx + fx a0 = getByX(x0) fish.move(fx, a0[0]) fish.rotate(a0[1]) if fish.x < -50: fishes.remove(fish) fishPool.append(fish) cannons = [] cannonPool = [] def getCannon(): if len(cannonPool): return cannonPool.pop() return circle(0, 0, 5, "black") def addCannon(): ang = ba * math.pi / 180 cannon = getCannon() cannon.show() cannon.move(boat.x, boat.y) cannon.vx = SPEED_CANNON * math.cos(ang) cannon.vy = SPEED_CANNON * math.sin(ang) cannons.append(cannon) def updateCannon(): for cannon in cannons: cannon.vy += GRAVITY cannon.move(cannon.x + cannon.vx, cannon.y + cannon.vy) if cannon.x > SW or cannon.y > SH: cannons.remove(cannon) cannonPool.append(cannon) cannon.hide() def updateWave(): global sx, R x0 = sx % R2 i = 0 for c in waves: c.move(R / 2 + i * R - x0, c.y) i += 1 def updateBoat(): global sx, by, ba, score if boat.ox < 20: boat.ox = 20 elif boat.ox > SW - 20: boat.ox = SW - 20 a0 = getByX(sx + boat.ox) by = a0[0] ba = a0[1] if boat.isJump: boat.vy += GRAVITY boat.move(boat.ox, boat.y + boat.vy) if boat.y > by - 10: boat.isJump = False boat.vy = 0 if not boat.isJump: boat.move(boat.ox, by - 10) boat.rotate(ba) def removeFish(fish): fishes.remove(fish) fishPool.append(fish) fish.hide() def removeCannon(cannon): cannons.remove(cannon) cannonPool.append(cannon) cannon.hide() def checkHit(): global score for cannon in cannons: for fish in fishes: if cannon.hits(fish): removeFish(fish) removeCannon(cannon) score += 1 break global life for fish in fishes: if fish.hits(boat): removeFish(fish) life -= 1 if life == 0: gameOver() break def gameOver(): global isOver overTf.show() isOver = True def reset(): global isOver, life, sx, coolDown coolDown = 0 isOver = False overTf.hide() sx = 0 boat.isJump = False boat.vy = 0 boat.ox = 100 while len(fishes): removeFish(fishes[0]) while len(cannons): removeCannon(cannons[0]) life = 5 def tap(): global isOver, coolDown if isOver: return reset() if x > SW / 2: fire() else: jump() def fire(): global coolDown if coolDown <= 0: addCannon() coolDown = COOL_DOWN def jump(): if not boat.isJump: boat.isJump = True boat.vy = JUMP_SPEED def keydown(code, key): global isOver if isOver: return if code == 37: boat.ox -= SPEED_X elif code == 39: boat.ox += SPEED_X elif code == 38: jump() elif code == 32: fire() def loop(): global isOver if isOver: return global sx, score, coolDown if coolDown > 0: coolDown -= 1 if (sx // HFR) != ((sx + SPEED) // HFR): addFish() sx += SPEED updateWave() updateBoat() updateFish() updateCannon() checkHit() updateLife() scoreTf.change("得分:" + str(score)) reset()
也可以下载代码,本地python环境运行(用pygame封装了xdf库)。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。