0

I can't figure out how to move sideways and jump at the same time, only one or the other. I have tried asyncio and multithreading/multiprocessing and couldn't get it to work. I am all out of ideas and can't find anymore online. I also have another issue where I can jump and if I reach the apex of the jump and hold a or d I can move side to side floating.

class Player():
    def __init__(self,health,boosts,height,width):
        self.health = health
        self.booosts = boosts
        self.height = height
        self.width = width
def jump():
    global gravtest, press
    press.remove("'w'")
    gravtest = False
    y = player[0].ycor()
    for i in range (1, 10):
        player[0].sety(y+(i*5))
    time.sleep(0.05)
    #while player[0]
    gravtest = True
#   def powers(self, boosts):

import turtle as t
import time, random
from pynput.keyboard import Key, Listener
t.ht()
press = []
gravtest = True

wn = t.Screen()
wn.title("Jump Man")
wn.bgcolor("white")
wn.screensize(250, 250)
wn.setup(width=1.0, height=1.0)

player = [t.Turtle(), Player(100, [], 25, 25)]
player[0].speed(0)
player[0].shapesize(0.5)
player[0].shape("square")
player[0].color("black")
player[0].up()
player[0].goto(0, 0)

floor = t.Turtle()
floor.speed(0)
floor.shape("square")
floor.shapesize(100)
floor.color("black")
floor.up()
floor.goto(0, -1150)

def gravity():
    global gravtest
    if gravtest == True:
        grav = 0
        while player[0].distance(floor) > 1007:
            y = player[0].ycor()
            player[0].sety(y + grav)
            if grav > -5:
                grav -= 1
                player[0].sety(y + grav) 
            gravtest = False
    if player[0].distance(floor) < 1045:
        player[0].sety(-145)

def show(key):
    global press
    if not(format(key) in press):
        press.append(format(key))
    print(key)
def rem(key):
    global press
    if format(key) in press:
        press.remove(format(key))

def move():
    global press
    while "'a'" in press:
        player[0].setx(player[0].xcor()-2)
    while "'d'" in press:
        player[0].setx(player[0].xcor()+2)
    if press == '\'s\'':
        print()
    gravity()
    if "'w'" in press:
        jump()
with Listener(on_press = show, on_release = rem) as listener: 
    while 1:
        move()
        
        


1 Answer 1

0

Your problem with moving and jumping is that you have separate loops for each that try to handle one kind of movement of the movement in one place. That won't work properly if other stuff (like jumping while moving, or moving while falling under gravity) are supposed to be possible.

Instead, you need to have just one main loop. On each run of that loop, you should do one frame worth of movement of whatever kinds is appropriate (e.g. moving horizontally, falling or jumping). This may require some bookkeeping to keep track of how long the vertical acceleration from a jump lasts.

I'd make the main loop something like this:

on_the_ground = True
jumping = False

while True:
    horizontal_movement() # this is always possible if the buttons are pressed
    if on_the_ground or jumping:
        jump()            # can start or continue a jump if a button is pressed
    else:
        gravity()         # fall, if you're not grounded or accelerating in a jump
    handle_collisions_etc()
    time.sleep(1/FRAMERATE)

Note, I've made some assumptions about the game logic you want. If you don't want to be able to move horizontally while in the air (as you can in many platformer games), you'll need to change this a little bit.

The jump function will need to keep track of how long you've been jumping for, since you probably want the player to be limited in how high they can go. I'm not exactly sure I understand the logic of your current code, so I'll leave it up to you to figure out exactly what to track.

A final suggestion is to move some of the global variables into the Player class. Even the turtle object you're using to draw things could be stored as an attribute on the instance you create.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.