-1

I'm making a platformer game where the player has to go to the next level and he will face tasks and challenges. I made it so that when it collides with the gem it will print "collision", that all worked fine but the player rectangle was too big and I tried using pygame.Rect() instead of pygame.get_rect(). The reason is because of the transparent background and now I can't get the rectangle to be smaller.

The rectangle [1]: https://i.stack.imgur.com/oGkA5.png

Code:

import pygame
import sys

pygame.init()

WIDTH = 800
HEIGHT = 400
FPS = 60
game_active = True
start_time = 0

WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Fox - A platformer")

text_font = pygame.font.SysFont("arial", 30, bold=True)


def draw_text(text, font, text_colour, x, y):
    img = font.render(text, True, text_colour)
    WIN.blit(img, (x, y))


class Player:
    def __init__(self, x, y):
        player_img = pygame.image.load("player-idle-2.png")
        self.image_right = pygame.transform.scale(player_img, (100, 100))
        self.image_left = pygame.transform.flip(self.image_right, True, False)
        self.image = self.image_right
        self.rect = pygame.Rect(1, 1, 85, 101)
        self.rect.x = x
        self.rect.y = y
        self.gravity = 0
        self.is_jumping = False
        self.add_score = False
        self.level = 2

    def keyboard_input(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_w] and not self.is_jumping:
            self.gravity = -15
            self.is_jumping += 1

        if keys[pygame.K_d]:
            self.rect.x += 8
            self.image = self.image_right

        if keys[pygame.K_a]:
            self.rect.x -= 8
            self.image = self.image_left

    def apply_gravity(self):
        self.rect.y += self.gravity
        self.gravity += 0.7

        if self.rect.y >= HEIGHT - self.rect.height - 65:
            self.rect.y = HEIGHT - self.rect.height - 65
            self.gravity = 0
            self.is_jumping = False

    def boundaries(self):
        if self.rect.x <= -27:
            self.rect.x = -27

    def new_level(self):
        if self.rect.x >= 728:
            self.level += 1
            self.rect.x = 45

        # Going back to the last level
        if self.rect.x <= -27 and self.level > 1:
            self.level -= 1
            self.rect.x = 693
            self.rect.y = 235

    def update(self):
        WIN.blit(self.image, (self.rect.x, self.rect.y))
        pygame.draw.rect(WIN, (255, 255, 255), self.rect, 1)


class Bullet:
    def __init__(self):
        self.bullet_img = pygame.image.load("bullet.png")
        self.bullet_rotated = pygame.transform.rotate(self.bullet_img, -90)
        self.bullet = pygame.transform.scale(self.bullet_rotated, (100, 100))
        self.bullet_rect = self.bullet.get_rect()
        self.bullet_state = False

    def fire_bullet(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_SPACE]:
            self.bullet_state = True
            self.bullet_rect.x = player.rect.x
            self.bullet_rect.y = player.rect.y + 15

        if self.bullet_state:
            WIN.blit(self.bullet, self.bullet_rect)
            self.bullet_rect.x += 8

        if self.bullet_rect.x > WIDTH:
            self.bullet_state = False


# Background
class World:
    def __init__(self):
        self.background_img = pygame.image.load("back.png").convert_alpha()
        self.background = pygame.transform.scale(self.background_img, (800, 400))
        self.tile_img = pygame.image.load("tileset.png").convert_alpha()
        self.tile = pygame.transform.scale(self.tile_img, (64, 64))
        self.pine_img = pygame.image.load("pine.png")
        self.pine_tree = pygame.transform.scale(self.pine_img, (154, 260))
        self.lava_img = pygame.image.load("lava.png")
        self.lava = pygame.transform.scale(self.lava_img, (64, 64))
        self.lava_rect = pygame.Rect(339, 334, 170, 64)
        self.gem_img = pygame.image.load("gem.gif")
        self.gem = pygame.transform.scale(self.gem_img, (37.5, 32.5))
        self.gem_rect = pygame.Rect(595, 300, 37.5, 32.5)

    def update(self):
        WIN.blit(self.background, (0, 0))
        WIN.blit(self.pine_tree, (650, 76))

        for i in range(0, WIDTH, 64):
            WIN.blit(self.tile, (i, HEIGHT - 64))

        if player.level == 1:
            draw_text("Welcome to this platformer.", text_font, (0, 0, 0), WIDTH / 2 - 175, 40)
            draw_text("To go to the next level, you have to get to the other side.", text_font, (0, 0, 0),
                      WIDTH / 2 - 395, 75)
            draw_text("There will be tasks and challenges, good luck!", text_font, (0, 0, 0), WIDTH / 2 - 300, 110)
            self.lava_rect = pygame.Rect(9999, 240, 175, 64)

        if player.level == 2:
            draw_text("Jump over the lava!", text_font, (0, 0, 0), WIDTH / 2 - 125, 50)
            self.lava_rect = pygame.Rect(339, 334.5, 170, 64)
            for i in range(175):
                WIN.blit(self.lava, (i + 300, 336))

            WIN.blit(self.gem, (595, 300))

        if player.level > 2:
            self.lava_rect = pygame.Rect(9999, 240, 175, 64)

        if self.gem_rect.colliderect(player.rect):
            print("Collision")


player = Player(45, 234)
bullet = Bullet()

run = True
while run:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    if game_active:
        world = World()
        world.update()

        player.keyboard_input()
        player.boundaries()
        player.new_level()
        player.apply_gravity()

        bullet.fire_bullet()

    # if player.rect.colliderect(world.lava_rect):
    #     player.rect.x = 45
    #     player.rect.y = 235

    player.update()

    if not game_active:
        WIN.fill((0, 0, 0))
        game_over_font = pygame.font.SysFont("arial", 50, bold=True)
        press_k_to_restart = pygame.font.SysFont("arial", 40, bold=True)
        draw_text("GAME OVER", game_over_font, (255, 255, 255), WIDTH / 2 - 150, HEIGHT / 2 - 50)
        draw_text("Hold down K to play again", press_k_to_restart, (255, 255, 255), WIDTH / 2 - 250, HEIGHT / 2 + 50)

        key = pygame.key.get_pressed()

        if key[pygame.K_k]:
            game_active = True

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

    pygame.display.update()
hikari
  • 1
  • 2

0 Answers0