Python Forum
[PyGame] Players not falling from the platform, and some other errors.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Players not falling from the platform, and some other errors.
#2
Jumping is fun because it involves physics.

The player is always being pulled downward by gravity. When your player jumps, you give the player an upward velocity. Gravity slows the upward velocity over time. When the velocity is zero, the player is at the apex of the jump. Gravity is still pulling, so the player begins moving downward after reaching the apex. The player's downward fall gets faster and faster until it reaches terminal velocity or collides with something that prevents falling. Colliding with a platform stops the player's vertical motion and sets the players vertical speed to zero.

I modified you code to focus on jumping and falling. No deaths or health or even a second player. When posting code to the forum you should only post enough code to demonstrate the problem. My example only requires one image file to run. I removed the background image and special attack images because I don't have those, and they aren't needed to demonstrate jumping or falling. Same goes for the icom. Try to post examples that other people can run.
import pygame

width = 1080
height = 720
player_image = "games/dice0.png"
platform_image = "games/dice0.png"
screen = pygame.display.set_mode((width, height))
gravity = 0.025  # Acceleration due to gravity


class Player:
    def __init__(self, x, y, image):
        self.image = pygame.image.load(image)
        w, h = self.image.get_size()
        self.rect = pygame.Rect(x, y, w, h)
        self.velocity = 1  # How fast I can run
        self.jump_velocity = 4  # How fast I move when I jump
        self.terminal_velocity = 2  # Fastest I fall
        self.dx = 0  # X velocity
        self.dy = 0  # Y velocity

    def move(self, direction):
        """Move horizontally"""
        self.dx = direction * self.velocity

    def jump(self):
        """Move vertically.  Just be standing on something to jump"""
        if self.dy == 0:
            self.dy = -self.jump_velocity

    def update(self, platforms):
        """Update player position"""
        # Move player.  Cannot exceed terminal velocity while falling
        self.dy = min(self.terminal_velocity, self.dy + gravity)
        self.rect.x += self.dx
        self.rect.y += self.dy

        # Check if player going off edge of screen
        if self.rect.y > height:
            self.rect.y = 0  # For demo purposes jump up to top of screen
        if self.rect.x < 0:
            self.rect.x = 0
        elif self.rect.right > width:
            self.rect.right = width

        # Check for collisions with platforms
        for platform in platforms:
            if self.rect.colliderect(platform.rect):
                if self.dy > 0:
                    self.dy = 0
                    self.rect.bottom = platform.rect.top
                elif self.dy < 0:
                    self.dy = 0
                    self.rect.top = platform.rect.bottom

    def draw(self, screen):
        screen.blit(self.image, (self.rect.x, self.rect.y))


class Platform:
    def __init__(self, x, y, width, height, image):
        self.image = pygame.image.load(image)
        self.image = pygame.transform.scale(self.image, (width, height))
        self.rect = pygame.Rect(x, y, width, height)

    def draw(self, screen):
        screen.blit(self.image, (self.rect.x, self.rect.y))


platforms = (
    Platform(0, 300, 600, 20, platform_image),
    Platform(500, 600, 600, 20, platform_image),
)
player1 = Player(300, -100, player_image)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        player1.move(-1)
    elif keys[pygame.K_d]:
        player1.move(1)
    else:
        player1.move(0)  # Stop running
    if keys[pygame.K_w]:
        player1.jump()

    player1.update(platforms)

    screen.fill("black")
    player1.draw(screen)
    for platform in platforms:
        platform.draw(screen)
    pygame.display.update()

pygame.quit
Reply


Messages In This Thread
RE: Players not falling from the platform, and some other errors. - by deanhystad - Jan-23-2023, 10:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Having 4 players(Sprites) all being able to jump ElijahCastle 5 4,110 May-07-2019, 05:04 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020