Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [PyGame] Problems with jump code in pygame
I' ve started learning pygame after I learned some python. But than I got to the problem with jumping code. When I press space it goes up and doesn' t go down.
There is also another problem; when I have the same value in if and while loop it doesn't jump at all. Here is the code:
import pygame
import math
x = 600
y = 600
h = 3
W = 9
v = 3
t = 2 * v
a = 0
run = True 
skok = False
screen= pygame.display.set_mode((800, 700))
screen.fill((255, 0, 0))
pygame.display.set_caption("Grafika 101")
while run:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
   run = False
 screen.fill((255, 0, 0)), (0, 0, 0), (x, y), 30)
 if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT and x > h:
  x -= h
 if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT and x < 800 - 30 - h:
  x += h
 if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  skok = True
  while skok:
   y -= v
   //This is  while loop I was talking about
   while a < 601:
    a = a+1 
	// And this is if loop
   if a == 600:
    while y < 600:
     y += v
     a = 0
   skok = False
I hope someone helps me
Thanks for replying
First, I notice that you have the if event.type ==... outside of the for loop. The for loop creates the variable event for use inside of the for loop. The variable cannot be used outside of that for loop. So the if statements need to be moved inside the for loop. I also notice that you must have come from a another language that uses slashes as comments. Python uses "#" to make a comment. Now, in the beginning you made a lot of variables for use in the loop. It would probably be more efficient to just make one dictionary and carry all the values in that dictionary. Also, for making pygames, there are lots of ways to mess up towards spaghetti code. I would recommend checking out the tutorials of pygame on this forum for tips on minimizing code and maximizing its efficiency - If you still have problems after fixing up your code, just post your new code and the problem you're having on this same thread.
Age Doesn't Matter, Your Passion Does
- Shreya Jain
OK thanks. But I'm courious as to why left and right do work? Anyway I will ceartanly remember what you told me.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [pygame] Improvement with code SheeppOSU 1 243 Jul-24-2019, 11:09 AM
Last Post: metulburr
  [Pygame] Problems with my textbox SheeppOSU 1 433 May-27-2019, 12:03 AM
Last Post: metulburr
  [PyGame] Having 4 players(Sprites) all being able to jump ElijahCastle 5 559 May-07-2019, 05:04 PM
Last Post: SheeppOSU
  [pygame] Inventory problems. Weapons equipped to wrong slot SheeppOSU 6 705 May-07-2019, 02:46 AM
Last Post: SheeppOSU
  drawing, moving, and collision problems (pygame) SheeppOSU 26 2,840 Apr-22-2019, 03:09 AM
Last Post: SheeppOSU
  Problems with loading buttons (pygame) SheeppOSU 2 636 Apr-12-2019, 08:04 PM
Last Post: SheeppOSU
  pygame installation problems Gheryk 2 1,873 Dec-12-2018, 07:05 AM
Last Post: Aikanwha
  Python Pygame code help Trajme 1 1,316 Dec-07-2017, 04:55 PM
Last Post: nilamo
  Appropriately delay this PyGame code kleynah22 2 1,478 Nov-09-2017, 02:00 PM
Last Post: Windspar
  Doodle Jump themed game! georgecoopers 1 2,041 Apr-11-2017, 08:47 PM
Last Post: metulburr

Forum Jump:

Users browsing this thread: 1 Guest(s)