jump logic in pygame

Solutions on MaxInterview for jump logic in pygame by the best coders in the world

showing results for - "jump logic in pygame"
Franco
01 Feb 2016
1import pygame
2pygame.init()
3win = pygame.display.set_mode((500, 500))
4pygame.display.set_caption("My First Game")
5clock = pygame.time.Clock()
6x = 0
7y = 300
8width = 40
9height = 60
10vel = 5
11j_vel = 10 ## The Velocity of the jump
12run = True
13isJump = False
14##main game loop
15while run:
16    clock.tick(60)
17    for event in pygame.event.get():
18        if event.type == pygame.QUIT:
19            run = False
20    ##moving the rectangle
21    key = pygame.key.get_pressed()
22    if key[pygame.K_LEFT] and x > 0:
23        x -= vel
24    if key[pygame.K_RIGHT] and x<500-width:
25        x += vel
26    if key[pygame.K_SPACE]:
27        isJump = True
28    if isJump:
29        y -= j_vel
30        j_vel -= 1
31        if j_vel < -10:
32            isJump = False
33            j_vel = 10
34    ##drawing the character
35    win.fill((0,0,0))
36    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
37    pygame.display.update()
38pygame.quit()