学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。
可以在线运行,效果如下(在新页面中打开链接):
代码如下(用到平台自定义的xdf库):
from xdf import * import math class Body: def __init__(self, x, y, w, h, dynamic = True): self.x = x self.y = y self.w = w self.h = h self.vx = 0 self.vy = 0 self.isStand = False self.dynamic = dynamic def hit(self, bd): return self.x <= bd.x + bd.w and self.x + self.w >= bd.x and self.y <= bd.y + bd.h and self.y + self.h >= bd.y class World: def __init__(self, gravity): self.bodies = [] self.gravity = gravity def addBody(self, bd): self.bodies.append(bd) def removeBody(self, bd): self.bodies.remove(bd) def clear(self): self.bodies.clear() def tryMoveX(self, bd, dt): bd.x += bd.vx * dt if self.hit(bd): bd.x -= bd.vx * dt bd.vx = 0 def tryMoveY(self, bd, dt): bd.vy += self.gravity * dt bd.y += bd.vy * dt bd.isStand = False if self.hit(bd): bd.isStand = bd.vy > 0 bd.y -= bd.vy * dt bd.vy = 0 def hit(self, bd): for bd0 in self.bodies: if bd0 != bd and bd.hit(bd0): return True return False def update(self, dt): for bd in self.bodies: if bd.dynamic: self.tryMoveX(bd, dt) self.tryMoveY(bd, dt) PX_M = 30 def m2px(m): return m * PX_M def px2m(px): return px / PX_M GRAVITY = m2px(10 * 2) JUMP_SPEED = m2px(-10) X_SPEED = m2px(1) class Game: def __init__(self): self.world = World(GRAVITY) self.CAMERAX = 200 self.hero = Body(self.CAMERAX, 0, m2px(0.5), m2px(1.8), True) self.tf = text("", 0, 50, 30) self.overTf = text("Game Over", 100, "red", "center") self.left = False self.right = False self.jump = False self.isOver = True self.sx = 0 self.lastFloor = None self.reset() def reset(self): if self.isOver: self.isOver = False self.sx = 0 self.overTf.hide() self.world.clear() self.hero.x = self.CAMERAX self.hero.y = 0 self.hero.vx = self.hero.vy = 0 self.world.addBody(self.hero) self.lastFloor = None self.addFloor() def gameOver(self): self.isOver = True self.overTf.show() def addFloor(self): if not self.lastFloor: self.lastFloor = Body(0, 800, 768, 40, False) self.world.addBody(self.lastFloor) else: if self.sx + 768 >= self.lastFloor.x + self.lastFloor.w: x0 = self.lastFloor.x + self.lastFloor.w + random(200) + 100 y0 = 800 + random(100) - 50 w0 = random(200) + 100 self.lastFloor = Body(x0, y0, w0, 40, False) self.world.addBody(self.lastFloor) def update(self, dt): if self.isOver: return self.hero.vx *= 0.9 if self.left: self.hero.vx -= X_SPEED if self.right: self.hero.vx += X_SPEED if self.jump and self.hero.isStand: self.hero.vy = JUMP_SPEED * (1 + px2m(px2m(self.hero.vx))) if self.hero.x - self.sx < 20 and self.hero.vx < 0: self.hero.vx = 0 self.world.update(dt) self.addFloor() if self.hero.x - self.sx > self.CAMERAX: self.sx += self.hero.x - self.sx - self.CAMERAX if self.hero.y > 1024: self.gameOver() self.tf.change(str(int(px2m(self.sx))) + "米") for bd in reversed(self.world.bodies): if bd != self.hero and bd.x + bd.w < self.sx: pass #self.world.removeBody(bd) keys = {} game = Game() rects = [] def refresh(): n0 = len(game.world.bodies) n1 = len(rects) for i in range(n1, n0): rects.append(box(0, 0, 1, 1, "transparent", "black")) for i in range(n1): if i < n0: bd = game.world.bodies[i] rects[i].move(bd.x + bd.w / 2 - game.sx, bd.y + bd.h /2) rects[i].size(bd.w, bd.h) rects[i].show() else: rects[i].hide() def loop(): game.jump = game.left = game.right = False if keys.get("37", False): game.left = True if keys.get("38", False): game.jump = True if keys.get("39", False): game.right = True N = 10 for i in range(N): game.update(0.05 / N) refresh() def tap(): game.reset() def keyup(code, key): keys[str(code)] = False def keydown(code, key): keys[str(code)] = True
也可以下载代码,本地python环境运行(用pygame封装了xdf库)。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。