7 Replies - 1217 Views - Last Post: 19 July 2015 - 03:56 PM Rate Topic: -----

#1 sliziky   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 16-July 15

Pygame - Moving and jumping at the same time

Posted 18 July 2015 - 02:00 PM

Hi guys ,at first my code:
import pygame
class App():
    def __init__(self):
        pygame.init()
        self.clock=pygame.time.Clock()
        pygame.key.set_repeat(50,50) #drzi klavesu a pohybuje sa
        self.window_height=480 #velkost okna
        self.window_width=640 #velkost okna
        self.screen=pygame.display.set_mode((self.window_width,self.window_height))
        
        
        self._y=400 #y suradnice
        self._x=100 #x suradnice
        self.ground_height=40 #vyska zeme
        self.ground_width=1000 #sirka zeme
        self.rect_width=40 #strana "hraca"-kocky
        self.rect_height=40
        
        self.white=(255,255,255) #farby RGB
        self.black=(0,0,0)
        self.red=(255,0,0)
        self.green=(0,255,0)
        self.blue=(0,0,255)
        
        self.FPS=30
        self.isOnGround=True #ci je kocka na "zemi"
        self.running=True #kvoli main
        self.speed=-10 #ako vysoko skoci
         
    def checkBound(self,x,y): ##kontroluje ci je stvorec vo vnutri okna
        if x<0:
            self._x=0
        if y<0:
            self._y=0
        if x>self.window_width-self.rect_width:
            self._x=self.window_width-self.rect_width
        if y>self.window_height-self.rect_height-self.ground_height:
            self._y=self.window_height-self.rect_height-self.ground_height
    def jump(self): ##skok
        
        self.isOnGround=False
        while self.isOnGround==False:
            
            self._y+=self.speed
            self.speed+=1
            
            
            print(self._y)
            if self._y>=self.window_height-self.ground_height-self.rect_height: #refresni 
                
                
                self._y=self.window_height-self.ground_height-self.rect_height
                self.speed=-10
                self.isOnGround=True
            self.update() #update scrn
            

            
            
        
        
        
    def event(self): #stlacanie klaves
        self.keys=pygame.key.get_pressed()
        for e in pygame.event.get():
            if e.type==pygame.QUIT:
                self.running=False
            if e.type==pygame.KEYDOWN:
                if self.keys[pygame.K_RIGHT]:
                    self._x+=10
                if self.keys[pygame.K_LEFT]:
                    self._x-=10
                if e.key==pygame.K_SPACE and self.isOnGround:
                    self.jump()
                    
                
    def update(self): #update okna
        self.screen.fill((self.white))
        self.checkBound(self._x,self._y)
        pygame.draw.rect(self.screen,(self.blue),(0,self.window_height-self.ground_height,self.ground_width,self.ground_height))
        pygame.draw.rect(self.screen,(self.red),(self._x,self._y,self.rect_width,self.rect_height))
        pygame.display.update()
    def main(self): #main
        while self.running:
            self.clock.tick(self.FPS) #kvoli FPS
            self.event()
            self.update()
    
            
myApp=App()
myApp.main()
pygame.quit()
quit()
            
        
problem is ,as the title says,when I jump I cant move to the left or right and I dont know how to figure it out:P Thanks for your help

Is This A Good Question/Topic? 0
  • +

Replies To: Pygame - Moving and jumping at the same time

#2 RamonRobben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 600
  • Joined: 19-May 14

Re: Pygame - Moving and jumping at the same time

Posted 18 July 2015 - 02:09 PM

I am a beginner at python but maybe you can make an loop or if statement check for wheb both keys are pressed so you can perform both actions at the same time.
Was This Post Helpful? 0
  • +
  • -

#3 sliziky   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 16-July 15

Re: Pygame - Moving and jumping at the same time

Posted 18 July 2015 - 02:16 PM

I think that it takes only the last key pressed,only one :-) But Ive seen that someone has done it but I dont know how:P
Was This Post Helpful? 0
  • +
  • -

#4 RamonRobben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 600
  • Joined: 19-May 14

Re: Pygame - Moving and jumping at the same time

Posted 18 July 2015 - 02:17 PM

maybe those links may be of interest:

http://stackoverflow...esses-in-python

http://stackoverflow...o-move-a-canvas
Was This Post Helpful? 0
  • +
  • -

#5 sliziky   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 16-July 15

Re: Pygame - Moving and jumping at the same time

Posted 18 July 2015 - 02:19 PM

It's a Tkinter,but I will check it out :-))
Was This Post Helpful? 1
  • +
  • -

#6 witeboy724   User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 208
  • Joined: 21-June 12

Re: Pygame - Moving and jumping at the same time

Posted 18 July 2015 - 04:26 PM

+1 for going out and learning classes! The problem is that you get stuck in a loop that only moves you up then down when you press jump. You need to separate out the movements so that each tick of the clock will move just a bit in the x and just a bit in the y until the expected x and y are reached. In other words, when you press jump, you don't want it to stop any other animation/movements to go all the way to the top of the jump and then back down without moving at all in the x direction. You don't want a 2nd animation loop in the middle of your main animation loop.

Look at what each tick of the clock does. Maybe you want to move some x_change and y_change for each clock tick, with the x_change or y_change possibly being 0 when there is no movement in that direction

This post has been edited by witeboy724: 18 July 2015 - 05:37 PM

Was This Post Helpful? 0
  • +
  • -

#7 sliziky   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 16-July 15

Re: Pygame - Moving and jumping at the same time

Posted 19 July 2015 - 06:04 AM

Ok guys,new code:
import pygame
class App():
    def __init__(self):
        pygame.init()
        self.clock=pygame.time.Clock()

        self.window_height=480
        self.window_width=640
        self._y=400
        self._x=100
        self.ground_height=40
        self.ground_width=1000
        self.isOnGround=True
        pygame.key.set_repeat(50,50)
        self.rect_width=40
        self.rect_height=40
        self.running=True
        self.speed_x=0
        self.speed_y=0
        self.screen=pygame.display.set_mode((self.window_width,self.window_height))

    def checkBound(self,x,y):
        if x<0:
            self._x=0
        if y<0:
            self._y=0
        if x>self.window_width-self.rect_width:
            self._x=self.window_width-self.rect_width
        if y>self.window_height-self.rect_height-self.ground_height:
            self._y=self.window_height-self.rect_height-self.ground_height

    def event(self):
        for e in pygame.event.get():
            if e.type==pygame.QUIT:
                self.running=False
            if e.type==pygame.KEYDOWN:
                if e.key==pygame.K_RIGHT:
                    self.speed_x=10
                    self._x+=self.speed_x

                elif e.key==pygame.K_LEFT:
                    self.speed_x=-10
                    self._x+=self.speed_x

                elif e.key==pygame.K_SPACE and self.isOnGround:
                    self.speed_y=-10
                    self.isOnGround=False

        self._y+=self.speed_y
        self._x+=self.speed_x
        self.speed_y+=1
        if self._y>self.window_height-self.ground_height-self.rect_width:
            self._y=400
            self.isOnGround=True


    def update(self):
        self.screen.fill((255,255,255))
        self.checkBound(self._x,self._y)
        pygame.draw.rect(self.screen,(0,0,255),(0,self.window_height-self.ground_height,self.ground_width,self.ground_height))
        pygame.draw.rect(self.screen,(255,0,0),(self._x,self._y,self.rect_width,self.rect_height))
        pygame.display.update()

    def main(self):
        while self.running:
            self.clock.tick(30)
            self.event()
            self.update()



myApp=App()
myApp.main()
pygame.quit()
quit()


But there's still one problem,when I jump and fall down,I want to stop ,but my rect still moves ...this jump is a nightmare for me :stupid: I am solving it since yesterday,but no result ...thanks for help :-))
Was This Post Helpful? 0
  • +
  • -

#8 witeboy724   User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 208
  • Joined: 21-June 12

Re: Pygame - Moving and jumping at the same time

Posted 19 July 2015 - 03:56 PM

When you say that you want to stop after the jump, what do you mean? Stop the down movement after a jump? It might help if I tried running your code, but here are a few things I get from reading over it.

Your main problem might be this: when space is pressed, you set the speed_y to a negative number, which means it will move up on the screen, but you never set a min/max for how you want the jump to behave. You might want some extra variables to define what a jump does, the height, speed of fall, etc..

Also,

The key presses are changing the values of x and y movement speeds, but they are also changing the x and y positions right afterwards in the same keypress part. Later, on lines 49 and 50 you are changing the _x and _y positions based on speed_x/speed_y for possibly the 2nd time in the same frame (if the keypress itself already moved them some). I would change the positions only after one or the other; you're doubling up by putting the self._x+=speed_x in both places, for example. If you're limiting the movements by the key repeat, then just handle the movements in the keypress part. If you want to continue moving in an x direction after releasing a L/R key, then do it outside the keypress. It's all about how you want it to act. I bet what you want is the x position to change when you press L/R, but the Y position to be based on how long it has been since you pressed jump..


Couple of other notes:

Line 52 is relating to a bunch of y things, like the height of the window, the height of the ground, but then you include the rect width, which would be an x property. With a square or circle this will still work, but it probably isn't what you meant.

Line 51 makes no sense to me. You want to always increase the speed_y value no matter what? Actually this might be your main issue if I understand your code correctly.


Again, this is just from reading through the code and not actually trying it so idk if I'm missing something. But, I didn't know pygame had a pygame.key.set_repeat() which actually sounds really useful, so thanks for teaching me something here too!

Edit: Changed a couple mix-ups in my response..

This post has been edited by witeboy724: 19 July 2015 - 07:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1