I am pretty new to pygame and this is my first basic game code. I am making a platformer game and I have everything rendered, tiles, background, sprite and sprite walking animations. Now I need the physics and I am having a pretty hard time understanding how to go about this. I know you can use rect but from what I see, most youtubers are telling me how to use rect with one sprite image, not multiple. Maybe I could try a different route, what do you guys suggest I do?
import pygame
pygame.init()
class Player(object):
def __init__(self,x,y):
self.velocity=4
self.x = x
self.y = y
self.jumping=False
self.right=False
self.left=False
self.jumptotal=10
self.walkcount=0
self.player_hitbox=(self.x+1,self.y,60,60)
def hitbox(self,window):
self.player_hitbox = (self.x, self.y, 47, 83)
pygame.draw.rect(window,(255,0,0),self.player_hitbox,2)
running=True
player=Player(20,600)
def draw_game():
global player
window.blit(background, (0, 0))
if player.walkcount + 1 > 45:
player.walkcount = 0
if player.right:
window.blit(walk_right[player.walkcount // 3], (player.x, player.y))
player.walkcount += 1
elif player.left:
window.blit(walk_left[player.walkcount // 3], (player.x-50, player.y))
player.walkcount += 1
else:
window.blit(idle, (player.x, player.y))
player.hitbox(window)
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
key_press=pygame.key.get_pressed()
if key_press[pygame.K_LEFT] and player.x>player.velocity:
player.x-=player.velocity
player.right=False
player.left=True
elif key_press[pygame.K_RIGHT] and player.x<2300-player.x-player.velocity:
player.x+=player.velocity
player.right = True
player.left = False
else:
player.right=False
player.left=False
player.walkcount=0
if key_press[pygame.K_UP]:
player.jumping=True
if player.jumping:
if player.jumptotal>=-10:
jumpside=1
if player.jumptotal<0:
jumpside=-1
player.y-=(player.jumptotal**2)*0.3*jumpside
player.jumptotal-=1
else:
player.jumping=False
player.jumptotal=10
pygame.display.update()
pygame.quit()