[Solved] Trying to make a “solid” block in pygame but it doesnt work


Sorry about the fact that my code isn’t based off your’s, I literally just threw it together in the past 5 minutes so it isn’t very pretty, but here it is:

from pygame.locals import *

import pygame
import sys

GRAVITY = 1.2

class Player:
    def __init__(self, x, y):
        self.rect = pygame.Rect(x, y, 25, 25)
        self.yvel = 0

    def tick(self):
        self.yvel += GRAVITY

        self.rect.y += int(self.yvel)

        if self.rect.y >= 475:
            self.rect.y = 475
            self.yvel = 0

    def set(self, y):
        if y:
            self.yvel = y

class Block:
    def __init__(self, x, y, w, h):
        self.rect = pygame.Rect(x, y, w, h)

class Game:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((500, 500))
        self.player = Player(0, 0)
        self.block = Block(225, 400, 50, 50)

    def main(self):
        right = left = False
        while True:
            self.screen.fill((230, 230, 230))
            pygame.draw.rect(self.screen, (40, 40, 40), self.block.rect)
            pygame.draw.rect(self.screen, (100, 200, 100), self.player.rect)

            pygame.display.flip()
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit(0)
                elif event.type == KEYDOWN:
                    if event.key == K_SPACE:
                        self.player.set(-20)
                    elif event.key == K_RIGHT:
                        right = True
                    elif event.key == K_LEFT:
                        left = True
                elif event.type == KEYUP:
                    if event.key == K_RIGHT:
                        right = False
                    elif event.key == K_LEFT:
                        left = False

            if right:
                self.player.rect.x += 5
                while self.player.rect.colliderect(self.block.rect):
                    self.player.rect.x -= 1
            if left:
                self.player.rect.x -= 5
                while self.player.rect.colliderect(self.block.rect):
                    self.player.rect.x += 1
            self.player.tick()
            if self.player.yvel > 0:
                while self.player.rect.colliderect(self.block.rect):
                    self.player.rect.y -= 1
                    self.player.yvel = 0
            elif self.player.yvel < 0:
                while self.player.rect.colliderect(self.block.rect):
                    self.player.rect.y += 1
                    self.player.yvel = 0

game = Game()
game.main()

The player speed is quite fast, but that can be changed at the lines:

self.player.rect.x -= 5

and

self.player.rect.x += 5

near the bottom.

The way it works is whenever you go to move, it moves the player, then looks to see if if collided with the block. If it did, the player moves in the opposite direction until is is satisfied that it isn’t colliding any more.
Jumping is accomplished using velocity, hence Player.yvel and GRAVITY. GRAVITY can be changed but if it it higher, jumps will be shorter and vice-versa.

Have a play around with the code and experiment with things like the blocks position (self.block = Block(225, 400, 50, 50)) and the player’s staring position (self.player = Player(0, 0))

If you do find any errors in the code, just let me know and I’ll try to fix them.

1

solved Trying to make a “solid” block in pygame but it doesnt work