I am currently trying to make a Mario-type platformer using pygame and the MVC design pattern. My progress as of right now is getting a character on screen and having it move left and right.
My goal is to have this character have the ability to jump when spacebar is pressed. It is definitely recognizing that spacebar is pressed, but the character just stops in place instead of jumping. Here is the code for the character that I currently have:
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self, name, x, y, img_file):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(img_file).convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.falling = True
self.onGround = False
self.v = 0
self.speed = 2
def moveLeft(self):
self.rect.x -= self.speed
def moveRight(self):
self.rect.x += self.speed
def jump(self):
if self.onGround == False:
return
self.velocity = 8
self.onGround = False
and here is the controller part corresponding to this:
if event.type == pygame.KEYDOWN:
if(event.key == pygame.K_SPACE):
self.player.jump()
Thanks