[Python] Pygame collision

Discussão em 'Python' iniciado por Stack, Junho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I trying to learn the basic of pygame and im starting with a simple platform game. Ive run into some trouble trying to make collisions however. I can make the player (red square) collide with the sides of the obstacle (blue shape) but some reason when it tries landing on top, its starts jittering instead of cleaning just sitting above the obstacle like normal. How do i fix this so it just rests on top?

    import pygame
    import random
    pygame.init()
    clock = pygame.time.Clock()

    class obstacle():
    def __init__(self, width, height):
    self.width = width
    self.height = height

    def make_obstacle(self, xpos, ypos):
    ypos = ypos - self.height
    pygame.draw.rect(win, (0, 0, 225), (xpos, ypos, self.width, self.height)) # Draw obstacle

    def collision(self, player_x, player_y, player_width, player_height, obs_xpos, obs_ypos):
    obs_top = obs_ypos - self.height
    obs_bottom = obs_ypos
    obs_left = obs_xpos
    obs_right = obs_xpos + self.width

    player_top = player_y
    player_bottom = player_y + player_height
    player_left = player_x
    player_right = player_x + player_width

    if player_bottom > obs_top and player_top < obs_top and player_right > obs_left and player_left < obs_right:
    return "top"
    elif player_right > obs_left and player_left < obs_left and player_bottom > obs_top and player_top < obs_bottom:
    return "right"
    elif player_left < obs_right and player_right > obs_right and player_bottom > obs_top and player_top < obs_bottom:
    return "left"
    return None

    win = pygame.display.set_mode((500, 500))
    pygame.display.set_caption("Game")

    x = 50
    y = 400
    width = 50
    height = 50
    vel = 10

    isJump = False
    jumpCount = 10

    xpos = random.randint(0, 430)
    ypos = 450

    floor_y = 450

    run = True

    while run:
    clock.tick(60)

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > vel:
    x -= vel
    if keys[pygame.K_RIGHT] and x < 500 - vel - width:
    x += vel

    if not isJump:
    if keys[pygame.K_SPACE]:
    isJump = True
    else:
    if jumpCount >= -10:
    y -= (jumpCount * abs(jumpCount)) * 0.5
    jumpCount -= 1
    else:
    jumpCount = 10
    isJump = False

    obstacle1 = obstacle(70, 70)
    collision_result = obstacle1.collision(x, y, width, height, xpos, ypos)
    on_obstacle = False

    if collision_result == "top":
    y = ypos - obstacle1.height - height
    isJump = False
    jumpCount = 10
    on_obstacle = True
    elif collision_result == "right":
    x = xpos - width
    elif collision_result == "left":
    x = xpos + obstacle1.width

    if not on_obstacle and y + height < floor_y:
    y += vel

    if y + height > floor_y:
    y = floor_y - height
    isJump = False
    jumpCount = 10

    win.fill((255, 255, 255))
    obstacle1.make_obstacle(xpos, ypos)
    pygame.draw.rect(win, (0, 0, 0), (0, floor_y, 1000, 1000)) # Floor
    pygame.draw.rect(win, (255, 0, 0), (x, y, width, height)) # Player
    pygame.display.update()

    pygame.quit()



    this here is my code

    Continue reading...

Compartilhe esta Página