M
M
Mikhail Sulzhenko2019-04-15 22:33:49
Game development
Mikhail Sulzhenko, 2019-04-15 22:33:49

Creating a paygame engine, how to implement a collision between objects?

There was a problem with the creation of physics and collision of the character and objects. I cannot make it so that the character does not jump through the blocks and stand on them freely, and can generally interact with other objects. There is an assumption that all blocks should be inserted into class 1, and then somehow interact with the character, but I don’t know how to do it. Please help, because I chose the creation of the game as the topic of the project (Grade 10), but did not realize my strength, I began to write the engine myself.
5cb4dbb731c27695886521.jpeg5cb4dbbf63457512380631.png5cb4dbc680e93863525004.png5cb4dbd101807339230203.png5cb4dbe0f09b7130428654.png5cb4dbe7c806c177805079.png

import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))

pygame.display.set_caption("Sulzhenko Project")

walkRight = pygame.image.load('right.png')

walkLeft = pygame.image.load('left.png')

bg = pygame.image.load('pygame_bg.jpg')
playerStand = pygame.image.load('stay.png')
block = pygame.image.load('Block.png')
cherry = pygame.image.load('cherry4.png')

clock = pygame.time.Clock()

x = 50
y = 445
width = 50
height = 50
speed = 5

isJump = False
jumpCount = 10

left = False
right = False
animCount = 0

def drawWindow():
    global animCount
    global animTrigger
    win.blit(bg, (0, 0))
    win.blit(block, (150, 350))
    win.blit(block, (175, 350))
    win.blit(block, (200, 350))
    win.blit(block, (225, 350))
    win.blit(block, (250, 350))
    win.blit(block, (375, 350))
    win.blit(block, (275, 350))
    win.blit(block, (300, 350))
    win.blit(block, (325, 350))
    win.blit(block, (350, 350))
    win.blit(block, (125, 350))
    win.blit(block, (100, 350))
    win.blit(block, (75, 350))
    win.blit(block, (50, 350))
    win.blit(block, (25, 350))
    win.blit(block, (0, 350))
    win.blit(block, (375, 350))
    win.blit(block, (400, 350))
    win.blit(block, (150, 200))
    win.blit(block, (175, 200))
    win.blit(block, (200, 200))
    win.blit(block, (225, 200))
    win.blit(block, (250, 200))
    win.blit(block, (375, 200))
    win.blit(block, (275, 200))
    win.blit(block, (300, 200))
    win.blit(block, (325, 200))
    win.blit(block, (350, 200))
    win.blit(block, (125, 200))
    win.blit(block, (100, 200))
    win.blit(block, (75, 200))
    win.blit(block, (475, 200))
    win.blit(block, (450, 200))
    win.blit(block, (425, 200))
    win.blit(block, (375, 200))
    win.blit(block, (400, 200))
    win.blit(cherry, (450, 150))

    if left:
        win.blit(walkLeft, (x, y))
    elif right:
        win.blit(walkRight, (x, y))
    else:
        win.blit(playerStand, (x, y))

    pygame.display.update()

run = True
while run:
    clock.tick(30)

    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 > 5):
        x -= speed
        left = True
        right = False
    elif keys[pygame.K_RIGHT] and x < 500 - width - 5:
        x += speed
        left = False
        right = True
    else:
        left = False
        right = False
        animCount = 0
    if not (isJump):
        if keys[pygame.K_SPACE]:
            isJump = True
    else:
        if jumpCount >= -10:
            if jumpCount < 0:
                y += (jumpCount ** 2) / 2.5
            else:
                y -= (jumpCount ** 2) / 2.5
            jumpCount -= 1
        else:
            isJump = False
            jumpCount = 10

    drawWindow()

pygame.quit()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
M0LCHALIVIY, 2019-04-15
@M0LCHALIVIY

I'm not an expert in game development, but I'm also learning.
I think you need to do something like gravity that reduces the player's Y if the player's bottom isn't in collision with anything.
And you can check the collision by adding the height of the Player to the Y coordinate and comparing it with the Y coordinate of the blocks / floor.
Something like this, I hope you understand the logic, if I'm wrong, correct me in the comments, I'll find out my mistakes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question