How would I code player jump physics, similar to "soccer physics", and make players collide with a ball in pygame?

34 views Asked by At

I recently chose to remake the popular game called "soccer physics" but in pygame instead of its original engine, unity. However, I have a quite limited understanding of pygame and implementing game physics, so I've only managed to create this so far with characters and sprites I found online (I know it's quite bad but it's my first time trying to code an actual 2d game). I aimed to make it a bit more fun and unique with the city background and the platform.

I decided to only make one player for now as I'll be using object oriented programming, so I'm thinking I can just create the other 3 players once I've made the code for one of them in a class. Please do correct me if I'm mistaken on that.

The main problem right now is the jumping and collisions, which are going to be a big obstacle for me. I managed to make the characters a very simple jump function, with moving and tilting in random directions, however it looks sort of robotic and there's no ragdoll or realistic physics to the jumping, as seen in the original soccer physics. I also have no idea on how I'll make the ball collide with the players and the ground and move accordingly. I've looked up countless tutorials but all of them use simple circles, whereas I'm trying to use an actual image of a football as a sprite along with football players, and I'm finding find this very hard to implement.

Here's what I've got so far:

if keys[pygame.K_a] and not jumping: # checks to see if up arrow key is pressed and player isn't jumping
            jumping = True # sets jumping to true - activates the jump algorithm
            possibilities = [(x - xvelocity), (x + xvelocity)] # list of the two possible directions - right and left
            angles = [15,20,25,30,35] # list of random possible angles
            direction = random.choice(possibilities) # picks a random direction for movement
            angle = random.choice(angles) # picks a random angle from the given list
if jumping: # checks if jumping is set to true (in other words if a has been pressed)
            y -= yvelocity # moves the player's y-position upwards
            yvelocity -= gravity # induces gravity to the jumping velocity, resulting in a deceleration and the player moving back down
            if y <= 300: # checks to see if plauer is above a certain height in the jump, and isn't colliding with the goalpost
                if direction == possibilities[1] and not goalright.rect.colliderect(hitbox): # checks if direction is chosen to be forward or backward
                    x = x + xvelocity # moves player forward/left 
                else: # checks if direction is chosen to be forward or backward
                    x = x - xvelocity # moves player backward/right
                xvelocity = 1 # returns velocity back to normal
            if yvelocity < -15: # checks to see if max velocity/height is reached
                jumping = False # stops the jumping function
                yvelocity = 15 # returns velocity to original value / moves the player back down
            if direction == possibilities[1] and x < 665: # if direction is to the right (x + the velocity)
                display.blit((pygame.transform.rotate(player1kick, -1*angle)),(x,y)) # draws player rotated at a clockwise angle, depending on the direction they're moving
            elif direction == possibilities[0] and x < 665: # if direction is to the left (x - the velocity)
                display.blit((pygame.transform.rotate(player1kick, angle)),(x,y)) # if direction is to the left, player rotates anti-clockwise; if to the right, player rotates clockwise.
            else:
                display.blit(player1,(x,y)) # draws normal state if too close to the goal
        else:
            display.blit(player1,(x,y)) # draws normal state on the ground

I know it's a big ask, but if someone could give me advice on how to implement the football and jumping, similar to soccer physics, that would be great. Otherwise, please tell me if I've just made a bad choice trying to do this in pygame.

0

There are 0 answers