首页 > 解决方案 > 如何让演员在 y 轴上移动?

问题描述

我在 TigerJython (Python) 中编写 Jump&Run 程序。我实现了公式,所以演员跳起来(当按下向上按钮时),然后演员应该由于重力而自动下降。演员应该只在 y 轴上移动(因为平台在移动),所以 x 轴的值为 1。我真的尝试了很多东西,但这个 Jumpman 不想移动。

http://www.tigerjython.ch/index.php?inhalt_links=navigation.inc.php&inhalt_mitte=gamegrid/spriteanimation.inc.php

# 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)

标签: pythonjythonjython-2.7jython-2.5

解决方案


推荐阅读