我在TigerJython(Python)中编写Jump&Run。我实现了公式,使演员跳起来(按向上按钮时),然后演员应由于重力而自动下降。演员只能在y轴上移动(因为平台正在移动),因此x轴的值是1。我确实尝试了很多事情,但是这个Jumpman不想移动。
# Jump & Run
from gturtle import *
from gamegrid import *
import math
from random import randint
from time import sleep
# ----------classJumpman-----------
class Jumpman(Actor):
def __init__(self):
Actor.__init__(self, "jumpman.gif")
self.speed = speed
self.dt = 0.005 * getSimulationPeriod()
def reset(self):
self.px = self.getX()
self.py = self.getY()
self.vx = 1
self.vy = self.speed * math.sin(math.radians(self.getDirection()))
def act(self):
self.vy = self.vy + g * self.dt
self.px = 1
self.py = self.py + self.vy * self.dt
self.setLocation(Location(int(self.px), int(self.py)))
self.setDirection(math.degrees(math.atan2(self.vy, self.vx)))
def collide(self, actor1, actor2):
self.setY(465)
return 0
# ----------classPlaforms----------
class Platform(Actor):
def __init__(self, path):
Actor.__init__(self, path)
def act(self):
self.move()
def generatePlatforms():
#var = randint(1,4)
#var_c = var
platform = Platform("platforms/" + str(randint(1,4)) + ".jpg")
jumpman.addCollisionActor(platform)
addActor(platform, Location(1600, 500), 180)
# ----------------------------------
def onKeyRepeated(keyCode):
if keyCode == 38:
jumpman.setDirection(jumpman.getDirection()+5)
g = 9.81
# GameGrid
makeGameGrid(1280, 720, 1, None, "retro.jpg", False)#, keyRepeated = onKeyRepeated)
jumpman = Jumpman()
addActor(jumpman, Location(400, 480), 90)
setSimulationPeriod(30)
show()
doRun()
while True:
generatePlatforms()
sleep(2)