-1

I have been making a basic runner game. This is only the first part. I added delay, so that when the character jumps up, it doesn't go back down automatically, but it works kind of wrong. This is the code:

import pygame

screen = pygame.display.set_mode((600, 550))
run = True

positionx = 100
positiony = 300

def update_screen():
  screen.fill((0, 100, 255)) #backgorund
  player = pygame.draw.rect(screen, (255, 255, 255), (positionx, positiony, 50, 80))
  ground = pygame.draw.rect(screen, (0, 255, 0), (0, 380, 600, 550))

def jump():
  global positiony
  positiony = positiony - 60
  update_screen()
  
  pygame.time.delay(1000)
  
  positiony = positiony + 60
  update_screen()

while run:
  update_screen(0)

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit
      
    if event.type == pygame.KEYDOWN and event.key == 32:
      jump()

  pygame.display.update()

The jump message displays, only after the 1 second delay. If I change the jump() function to something like, for example:

def jump():
  global positiony
  positiony = positiony - 60
  update_screen()
  
  pygame.time.delay(1000)
  
  positiony = positiony - 60
  update_screen()

Then the character will react only after the delay, even if the first update was before

Is this a glitch?

  • No, this isn't a glitch. `delay()` pauses the entire program for the given amount of time. Nothing else can happen while the program is paused. Messages aren't shown, characters don't move. – John Gordon Sep 02 '23 at 22:59

1 Answers1

-1

here's an example of jump in pygame

import pygame
pygame.init()
screen = pygame.display.set_mode((600, 550))
pygame.display.set_caption("Jump Game")
# position of player
positionx = 100
positiony = 300
# geometry of player
width = 30
height = 40
# states and variables
isjump = False
v = 10
m = 1
run = True
def update_screen():
  screen.fill((0, 100, 255))
  player = pygame.draw.rect(screen, (255, 255, 255), (positionx, positiony, 50, 80))
  ground = pygame.draw.rect(screen, (0, 255, 0), (0, 380, 600, 550))

def jump():
  global positiony,v,m,isjump
  F =(1 / 2)*m*(v**2)
  positiony-= F
  v = v-1
  if v<0:
    m =-1
  if v ==-11:
    isjump = False
    v = 10
    m = 1

while run:
  update_screen()

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run=False
  keys = pygame.key.get_pressed()
  if isjump == False:
    if keys[pygame.K_SPACE]:
      isjump = True

  pygame.display.update()
  if isjump :
    jump()
  pygame.time.delay(10)
  pygame.display.update()
pygame.quit()

Also as said in the comments,this isn't a bug in pygame.

  • Also delay() will pause the program for an amount of time,since the program is single threaded it may sometime be not responsive
  • but since you are making a runner game the player only moves in the y direction and the platforms makes the illusion of running.So delaying while jumping probably pause the entire program,
  • for the current use case a jump program can be written to solve this,The above program works on the basis of F =(1 / 2)*m*(v**2)(equation from GeeksForGeeks website).
cool-guy
  • 3
  • 1