0

I have a pycharm project where i use pygame. I am trying to make a 2d platformer and currently i have 3 scripts one for player one for block and one for main game loop. I have tried looking at another post and could not figure it out. I have tried messing around with the collision code but to no help.

Main Script

import pygame
import player
import environment


def run_game():
    pygame.init()

    winW, winH = 900, 650
    screen = pygame.display.set_mode((winW, winH))
    pygame.display.set_caption("")
    icon = pygame.Surface((32, 32))
    icon.fill((255, 255, 255))
    pygame.display.set_icon(icon)

    sprite_list, sprite_rect_list = environment.load_sprites()
    character = player.Player(sprite_list, sprite_rect_list)

    clock = pygame.time.Clock()
    FPS = 25

    running = True
    while running:
        clock.tick(FPS)
        FPS = 25
        screen.fill((50, 100, 100))
        screen.blit(character.surf, character.rect)

        character.check_collision()



        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
        # Get Pressed Keys
        keys = pygame.key.get_pressed()
        # Check if the player should be moving
        if keys[pygame.K_LEFT]:
            character.rect.move_ip(-12, 0)
        if keys[pygame.K_RIGHT]:
            character.rect.move_ip(12, 0)
        if keys[pygame.K_UP] and character.on_ground:
            character.jumping = True
        if keys[pygame.K_DOWN] and character.on_ground:
            character.rect.y += 1
        else:
            character.jumping = False

        character.jump()

        for i in range(len(sprite_list)):
            screen.blit(sprite_list[i], sprite_rect_list[i])

        pygame.display.flip()


if __name__ == "__main__":
    run_game()

Player Script

import pygame


class Player(pygame.sprite.Sprite):
    def __init__(self, sprite_list, sprite_rect_list, w=32, h=32):
        super(Player, self).__init__()
        self.sprite_list = sprite_list
        self.sprite_rect_list = sprite_rect_list
        self.surf = pygame.Surface((w, h))
        self.rect = self.surf.get_rect()
        self.surf.fill((255, 0, 0))
        self.jumping = False
        self.on_ground = False
        self.fall_speed = 3
        self.air_time = 0
        self.jump_height = 30
        self.rect.move_ip(100, 400)
        self.collision = [[False] * 9] * len(self.sprite_rect_list)

    def check_collision(self):
        for i in range(len(self.sprite_rect_list)):
        # for rect in self.sprite_rect_list:
            rect = self.sprite_rect_list[i]
            self.collision[i][0] = self.rect.collidepoint(rect.topleft)
            self.collision[i][1] = self.rect.collidepoint(rect.topright)
            self.collision[i][2] = self.rect.collidepoint(rect.bottomleft)
            self.collision[i][3] = self.rect.collidepoint(rect.bottomright)
                           
            self.collision[i][4] = self.rect.collidepoint(rect.midleft)
            self.collision[i][5] = self.rect.collidepoint(rect.midright)
            self.collision[i][6] = self.rect.collidepoint(rect.midtop)
            self.collision[i][7] = self.rect.collidepoint(rect.midbottom)

            #self.collision[8] = self.rect.collidepoint(rect.center)

    def jump(self):
        if self.jumping and self.air_time <= 5:
            self.rect.y -= self.jump_height
            self.fall_speed = 1
            self.air_time += 1
        else:
            if 1 in self.sprite_rect_list: exit(22)
            if 1 in self.collision[0] or self.collision[1] or self.collision[6]:
                self.on_ground = True
                self.air_time = 0
                print(self.collision)
            else:
                self.on_ground = False


            '''if self.rect.collidelist(self.sprite_rect_list) >= 0:
                self.on_ground = True
                self.air_time = 0
            else:
                self.on_ground = False'''

            if not self.on_ground:
                self.rect.y += self.fall_speed
                self.fall_speed += 0

Block Scipt

import string
import pygame

'''
sprite_list = []
sprite_rect_list = []
'''


def make_sprite(sprite_list: list, sprite_rect_list: list, width: int, height: int, x_pos: int, y_pos: int, sprite: string):
    block_image = pygame.image.load(sprite)
    block_image = pygame.transform.scale(block_image, (width, height))
    block_image_rect = block_image.get_rect()
    block_image_rect.move_ip(x_pos, y_pos)

    sprite_list.append(block_image)
    sprite_rect_list.append(block_image_rect)


def load_sprites():
    sprite_list = []
    sprite_rect_list = []
    make_sprite(sprite_list, sprite_rect_list, 64, 64, 100, 500, "Grass Block 16x.png")
    make_sprite(sprite_list, sprite_rect_list, 64, 64, 164, 500, "Grass Block 16x.png")
    make_sprite(sprite_list, sprite_rect_list, 64, 64, 164 + 64, 500 - 50, "Grass Block 16x.png")
    make_sprite(sprite_list, sprite_rect_list, 64, 64, 164 + 64 + 64, 500 - 50, "Grass Block 16x.png")
    make_sprite(sprite_list, sprite_rect_list, 64, 64, 164 + 64 + 64 + 64, 500 - 50, "Grass Block 16x.png")
    make_sprite(sprite_list, sprite_rect_list, 64, 64, 164 + 64 + 64 + 64 + 64, 500 - 50, "Grass Block 16x.png")
    return sprite_list, sprite_rect_list
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
Luca-Fly
  • 1
  • 2
  • Where have you tried so far? Where do you struggle? Where exactly are you trying to handle the collision with the sides of a block? – Rabbid76 Nov 30 '21 at 17:26
  • Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Rabbid76 Nov 30 '21 at 17:30

0 Answers0