Zeperox
Zeperox

Reputation: 206

How do I make the character jump in Pygame?

I am trying to make a platformer game and I want the circle to jump, but whenever I try to make a jump, I get the following error, I found various methods of doing the jump, but none of them worked. I think that the error is whenever I jump, a float number is happening and the draw method can't contain floats, only integers

here is the code for the game:

import pygame
import os
import time
import random
pygame.font.init()

Width, Height = 1280, 720
win = pygame.display.set_mode((Width, Height))
pygame.display.set_caption("Parkour Ball")

BG = pygame.image.load(os.path.join("assets", "Background.jpg"))

class Ball:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def draw(self, window):
        pygame.draw.circle(window, (255,0,0), (self.x, self.y), 25)

def main():
    run = True
    FPS = 60
    clock = pygame.time.Clock()
    level = 0
    lives = 10
    main_font = pygame.font.SysFont("comicsans", 50)
    lost_font = pygame.font.SysFont("comicsans", 60)

    spikes = []
    holes = []
    hammers = []
    platforms = []
    enemies = []

    player_vel = 5
    jump = False
    jumpCount = 10
    player = Ball(300, 450)

    lost = False
    lost_popup = 0

    def redraw():
        win.blit(BG, (0,0))
        player.draw(win)

        pygame.display.update()

    while run:
        clock.tick(FPS)
        redraw()

        if lives == 0:
            lost = True
            lost_popup += 1

        if lost:
            if lost_popup > FPS * 3:
                run = False
            else:
                continue

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and player.x - player_vel - 25 > 0:
            player.x -= player_vel
        if keys[pygame.K_RIGHT] and player.x + player_vel + 25 < Width:
            player.x += player_vel
        if keys[pygame.K_w]and player.y - player_vel > 0: #up
            player.y -= player_vel
        if keys[pygame.K_s] and player.y + player_vel < Height: #down
            player.y += player_vel
        if not(jump):
            if keys[pygame.K_SPACE]:
                jump = True
        else:
            if jumpCount >= -10:
                neg = 1
                if jumpCount < 0:
                    neg = -1
                player.y -= (jumpCount ** 2) / 2 * neg
                jumpCount -= 1

main()

Upvotes: 1

Views: 454

Answers (1)

Rabbid76
Rabbid76

Reputation: 210909

The coordinates to pygame.draw.circle() have to be integral values. round() the floating point coordinates to integral coordinates:

pygame.draw.circle(window, (255,0,0), (self.x, self.y), 25)

pygame.draw.circle(window, (255,0,0), (round(self.x), round(self.y)), 25)

Upvotes: 1

Related Questions