Reputation: 82
I'm trying out a jumping code in my program but the characters aren't jumping. (Character 1s controls are 'a' 'd' and character 2s controls are 'left arrow' 'right arrow'. How do i get the character 1 to jump with w and character 2 to jump with up arrow. I'm not sure whats wrong with the code as this is my first time implementing a jumping mechanic.
import pygame
pygame.init()
win = pygame.display.set_mode((700, 480))
pygame.display.set_caption("First project")
run = True
red = (255, 0, 0)
green = (0, 255, 0)
def drawbg():
pygame.display.update()
win.fill((255, 255, 255))
class person(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.IsJump = False
self.jumpCount = 10
man = person(100, 400, 50, 60)
man2 = person(500, 400, 50, 60)
while run:
pygame.time.delay(25)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and man2.x > man2.vel:
man2.x -= man2.vel
if keys[pygame.K_RIGHT] and man2.x < 700 - man2.width - man2.vel:
man2.x += man2.vel
if keys[pygame.K_a] and man.x > man.vel:
man.x -= man.vel
if keys[pygame.K_d] and man.x < 700 - man.width - man.vel:
man.x += man.vel
if not man.IsJump and keys[pygame.K_SPACE]:
man.IsJump = True
man.JumpCount = 10
if man.IsJump:
if man.JumpCount >= -10:
neg = 1
if man.JumpCount < 0:
neg = -1
man.y -= (man.JumpCount ** 2) / 2 * neg
man.JumpCount -= 1
else:
man.IsJump = False
man.JumpCount = 10
pygame.draw.rect(win, red, (man.x, man.y, man.width, man.height))
pygame.draw.rect(win, green, (man2.x, man2.y, man2.width, man2.height))
drawbg()
pygame.quit()
Upvotes: 2
Views: 77
Reputation: 271
It seems you didn't write codes to react with keyboard input w (pygame.K_w
) and up (pygame.K_UP
) at first. However, in your code that you uploaded, player1 (man
) already can jump with space bar (K_SPACE
) properly. you can copy that part, and just change some variables for man2
.
Upvotes: 0
Reputation: 210909
Move the code which handles the jump, to an update
method of the class person
:
class person(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.IsJump = False
self.jumpCount = 10
def update(self):
if self.IsJump:
if self.JumpCount >= -10:
neg = 1 if self.JumpCount >= 0 else -1
self.y -= (self.JumpCount ** 2) / 2 * neg
self.JumpCount -= 1
else:
self.IsJump = False
self.JumpCount = 10
Activate jumping of player 1 (man
) on k and jumping of player 2 (man2
) on UP.
Call the update
method for the objects man
and man2
after the event handling:
while run:
pygame.time.delay(25)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and man2.x > man2.vel:
man2.x -= man2.vel
if keys[pygame.K_RIGHT] and man2.x < 700 - man2.width - man2.vel:
man2.x += man2.vel
if keys[pygame.K_a] and man.x > man.vel:
man.x -= man.vel
if keys[pygame.K_d] and man.x < 700 - man.width - man.vel:
man.x += man.vel
if not man.IsJump and keys[pygame.K_w]:
man.IsJump = True
man.JumpCount = 10
if not man2.IsJump and keys[pygame.K_UP]:
man2.IsJump = True
man2.JumpCount = 10
man.update()
man2.update()
pygame.draw.rect(win, red, (man.x, man.y, man.width, man.height))
pygame.draw.rect(win, green, (man2.x, man2.y, man2.width, man2.height))
drawbg()
pygame.quit()
Upvotes: 1