Creating a platformer in pygame and im using vectors for the gravity and movement. Right now im stuck on the collisions with the platforms because the plat will hit the platform and sink almost entirely in the platform like this:
Player sunk into platform block
(EDIT) People wanted more code info so...
Main Class:
import pygame as pg
import sys
from sprites import *
class Game:
def __init__(self):
pg.init()
pg.mixer.init()
pg.display.set_caption(TITLE)
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
self.clock = pg.time.Clock()
self.running = True
self.load_data()
self.new_game()
def load_data(self):
# Eventually will load data
pass
def new_game(self):
# Creates new instances of all of the sprites
# groups and re-renders sprites at starting locations
self.all_sprites = pg.sprite.Group()
self.platform_sprites = pg.sprite.Group()
self.player = Player(self, 60, 60)
# Creates some test platforms
for x in range(10):
Platform(self, x, 6)
def run(self):
# Basic game loop
while self.running:
self.dt = self.clock.tick(FPS) # Get time passed since last update / Set FPS
self.events()
self.render()
self.update()
def events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.running = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
self.running = False
# Check for quit event and close window
def render(self):
self.screen.fill(WHITE)
self.all_sprites.draw(self.screen) # Draws all sprites to screen
pg.display.flip() # updates screen
def update(self):
self.all_sprites.update() # calls the update function of all of the sprites
if __name__ == '__main__':
g = Game()
while g.running:
g.run()
pg.quit()
sys.exit()
.... Player and platform sprite code:
import pygame as pg
from constants import *
vec = pg.math.Vector2
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
self.game = game
super().__init__(self.groups)
self.image = pg.Surface((40, 70))
self.image.fill(BLUE)
self.rect = self.image.get_rect()
pg.draw.rect(self.image, BLACK, (0, 0, self.rect.width, self.rect.height), 3)
self.pos = vec(x, y)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
def move(self):
self.acc = vec(0, 0)
keypress = pg.key.get_pressed()
if keypress[pg.K_a]:
self.acc.x += -PLAYER_MOVE_SPEED
if keypress[pg.K_d]:
self.acc.x += PLAYER_MOVE_SPEED
self.acc += self.vel * PLAYER_FRICTION
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
self.rect.midbottom = self.pos
def collisions(self, axis):
if axis == 'y':
for plat in self.game.platform_sprites:
if self.acc.y > 0:
if pg.Rect.colliderect(self.rect, plat.rect):
self.acc.y = 0
self.pos.y = plat.rect.top
def update(self):
self.move()
self.collisions('y')
class Platform(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites, game.platform_sprites
self.game = game
super().__init__(self.groups)
self.image = pg.Surface((TILESIZE, TILESIZE))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.x = x * TILESIZE
self.y = y * TILESIZE
def update(self):
self.rect.x = self.x
self.rect.y = self.y
The "Constants" file only has stuff like the screen size, colors, and player speed defined. IF any of that's needed I'll add it.
move()andcollisions()in. If the player is sinking in but stopping after a certain point, it may be occurring because you are displaying the player before collisions are handled. - Larry McMuffin