[Python] How to program angled movement (for a Sonic game)? [duplicate]

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I've been trying to get a Sonic The Hedgehog game working in Pygame and it works for the most part. Sonic can run fast, jump across platforms like a normal platformer, gets affected by gravity, collects rings, etc. (check attached video)

    https://imgur.com/a/q1YrAXO

    However, I cannot for the life of me get Sonic to run at different angles and go up slopes and loops like in the real games. I am aware such a project doesn't exist in Pygame (for the public to access anyway) and making the precise, mathematically accurate scripts can be hard to do.

    Problematic code:
    (from levels.py)

    def update(self):
    self.sonic.update()
    self.camera.update(self.sonic) # Follow Sonic

    # Keep Sonic grounded
    self.sonic.grounded = False
    for tile in self.tile_group:
    if self.sonic.mask.overlap(tile.mask, (tile.rect.x - self.sonic.hitbox.x, tile.rect.y - self.sonic.hitbox.y)):
    self.sonic.Yvel = 0
    self.sonic.grounded = True
    self.sonic.jumped = False
    self.sonic.angle = tile.angle
    break


    (from characters.py)

    if not self.grounded:
    self.Yvel += self.gravityforce # Apply gravity
    self.Yvel = min(self.Yvel, 10) # Cap max fall speed
    self.Xvel = self.groundSpeed
    else:
    # Adjusting speed with some trigonometry
    self.Yvel = self.groundSpeed * math.sin(self.angle * -1)
    self.Xvel = self.groundSpeed * math.cos(self.angle)

    self.x += self.Xvel
    self.rect.x = self.x # Update self.rect with the new x coordinate
    self.y += self.Yvel
    self.rect.y = self.y
    self.hitbox.x = self.x
    self.hitbox.y = self.y


    I am using Tiled for mapmaking by the way, I'm not sure if that helps in my situation. I've manually added an angle attribute to the tiles currently in use and given them unique angles such as 20, 45, 90, etc. (which is why Sonic suddenly changes angle and jumps/phases through floor at times).

    With all that being said, can anyone help or offer some advice? Literally anything will be appreciated.

    Continue reading...

Compartilhe esta Página