2

I have just started using , and I'm stuck.

I'm not getting any syntax errors, but I'm sure there is some problem with the below code.

import pygame
import sys

pygame.init()
pygame.display.set_caption('Jumper Game')

display_width = 500
display_height = 500
the_game_is_on = True

ball_pos_x = 200
ball_pos_y = 500
ball_radius = 20
ball_color = [0,0,255]
speed = 1

is_jump = False
m = 1
v = 5

dis = pygame.display.set_mode((display_width,display_height)) #screen

while the_game_is_on:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and ball_pos_x > 20:
        ball_pos_x-= speed
    if keys[pygame.K_RIGHT] and ball_pos_x < (display_width - (ball_radius)):
        ball_pos_x+= speed
    if not (is_jump):
        if keys[pygame.K_UP] and ball_pos_y > 20:
            ball_pos_y-= speed
        if keys[pygame.K_DOWN] and ball_pos_y < (display_height - (ball_radius)):
            ball_pos_y+= speed
        if keys[pygame.K_SPACE]:
            is_jump = True
    else:
        f = (1/2)*m*(v**2)
        ball_pos_y-=f
        v-=1
        if v < 0:
            m = -1
        if v >= ((v+1)*-1): #to check the initial position
            is_jump = False
    pygame.time.delay(10)
    dis.fill((0,0,0))
    pygame.draw.circle(dis,ball_color,(ball_pos_x,int(ball_pos_y)),ball_radius)
    pygame.display.update()
pygame.display.quit()

For writing the code for jump, I referred to this website, the source code in this website works perfectly.

Adarsh Raj
  • 31
  • 4
  • some problem huh? care to elaborate? – Joran Beasley Jul 09 '20 at 19:32
  • "some problem" means nothing particular really and I would not expect too many users here interested in analyzing your code just to figure out what you wanted to ask about. – Marcin Orlowski Jul 09 '20 at 19:34
  • 1
    Ya, so the blue ball works perfectly when going sideways, up and down. But it suddenly disappears when I press the space bar. @JoranBeasley – Adarsh Raj Jul 09 '20 at 19:35
  • 3
    Try printing out the y position for the ball after you hit space. I suspect the ball is launching into crazy high or low positions not viewable in the camera very quickly. – code11 Jul 09 '20 at 19:40

2 Answers2

2

You have to compute m dependnet on v < 0:

m = -1 if v < 0 else 1

The jump has to end if v < -5 and when the jump ends, then v has to be reset (v = 5):

jump_h = 5 # try 10 for higher jumps
v = jump_h

while the_game_is_on:
    # [...]

    if not is_jump:
        # [...]

    else:
        if v >= -jump_h:
            m = -1 if v < 0 else 1
            f = (1/2)*m*(v**2)
            v -=1
            ball_pos_y -= f
        else:
            is_jump = False
            v = jump_h

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

I think you are hitting Python2 vs. Python3 difference:

$ python3
Python 3.7.7 (default, Mar 13 2020, 21:39:43) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> m = 1
>>> v = 5
>>> (1/2)*m*(v**2)
12.5

In Python2, 1 / 2 equals 0, but when you use float: 1.0 / 2 is 0.5:

$ python2
Python 2.7.18 (default, Apr 21 2020, 18:49:31) 
[GCC 9.3.1 20200408 (Red Hat 9.3.1-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> m = 1
>>> v = 5
>>> (1/2)*m*(v**2)
0
>>> (float(1)/2)*m*(v**2)
12.5
jhutar
  • 1,369
  • 2
  • 17
  • 32