Hey I managed to give my player a picture as model, but now my jump isn't working. What did I do wrong?
It's Stuck on the floor
We have got this projekt pre coded and firt i changed that the player is not just a rectangle. Now it´s a picture but the jump is not working anymore. I think I only have to work on the jump def but I don't know how.
I would realy appreciate some help
main.py
import pgzrun
from src.model.game import Game
# create the game instance
game = Game()
# define width and height of the application. pgzero needs these to be set
WIDTH = game.screen_width
HEIGHT = game.screen_height
SKY_COLOR = (163, 232, 254)
FLOOR_COLOR = (88, 242, 152)
PLAYER_COLOR = (254, 185, 163)
def update():
game.player.update(keyboard.space)
game.player.update(keyboard.up)
game.update_decorations()
def draw():
# drawing the sky
screen.draw.filled_rect(
Rect(0, 0, WIDTH, game.ground_start),
SKY_COLOR
)
# drawing the background decorations like the clouds BEFORE drawing the floor (so that the floor is in front).
game.draw_background_decorations()
# drawing the floor
screen.draw.filled_rect(
Rect(0, game.ground_start, game.screen_width, game.screen_height - game.ground_start),
FLOOR_COLOR
)
# drawing the foreground decorations like the flowers AFTER drawing the floor (so that the decorations are in front).
game.draw_foreground_decorations()
game.draw_obstacles()
# drawing the player
game.player.image.draw()
# starting the application
pgzrun.go()
game.py
from src.model.player import Player
# The Game class.
# This class holds all information regarding the game and also contains all props like the player or the decorations.
class Game(object):
def __init__(self):
self.gravity = 5 # The gravity affects how fast the player falls to the ground
self.screen_height = 600
self.screen_width = 800
self.ground_start = 400 # The height at which the ground starts. Everything before this is sky.
self.player = Player(self)
def draw_player(self):
player.draw()
def update_player(self):
player.update
player.py
from pgzero.actor import Actor
import random
# The player class.
class Player(object):
def __init__(self, game):
self.image = Actor('player')
self.image.x = 100
self.image.y = game.ground_start
self.x = 100
self.y = game.ground_start
self.velocityX = 0
self.velocityY = 5
self.height = 100
self.width = self.height / 2
self.jump_height = -10 # The jump height is negative, as the top of the screen is 0 and the bottom is the height
self.game = game
self.isJump = False
self.jumpCount = 10
def update(self, jump_pressed):
if jump_pressed:
self.velocityY = self.jump_height
self.y += self.velocityY
self.velocityY += self.game.gravity
if self.y > self.game.ground_start - self.height:
self.velocityY = 2
self.y = self.game.ground_start - self.height
´´´