0

I am trying to make a 2D Platformer with Pygame and it starts out fine running at 25 fps that i capped it at with clock.tick(25), and then after 10 seconds or so it starts slowing down. I have tried to add a deltatime function that didnt work and i have tried switching out pygame.display.flip() with pygame.display.update() but to no help. I am not sure what the problem is since there are no error messages so i will show all 3 python files.

This is my main file called main.py

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)

    character = player.Player()

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

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

        character.jump()

        # Make The Environment
        environment.make_block(1000, 32, 50, 500, (255, 255, 255))
        environment.make_sprite(64, 64, 200, 100, "Grass Block 16x.png")

        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(-6, 0)
        if keys[pygame.K_RIGHT]:
            character.rect.move_ip(6, 0)
        if keys[pygame.K_UP] and character.on_ground:
            character.jumping = True
        else:
            character.jumping = False

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

        pygame.display.flip()


if __name__ == "__main__":
    run_game()

This is my player file called player.py

import pygame
import environment
from main import *

class Player(pygame.sprite.Sprite):
    def __init__(self, w=32, h=32):
        super(Player, self).__init__()
        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

    def jump(self):
        if self.jumping and self.air_time <= 5:
            self.rect.y -= self.jump_height
            self.fall_speed = 0
            self.air_time += 1
        else:
            for i in range(len(environment.block_rect_list)):
                if self.rect.colliderect(environment.block_rect_list[i]):
                    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 += 2

This is my environment file called environment.py

import pygame

block_surf_list = []
block_rect_list = []


def make_block(width, height, x_pos, y_pos, block_color):
    block_surf = pygame.Surface((width, height))
    block_surf.fill(block_color)
    block_rect = block_surf.get_rect()
    block_rect.move_ip(x_pos, y_pos)

    block_surf_list.append(block_surf)
    block_rect_list.append(block_rect)


sprite_list = []
sprite_rect_list = []

test_size_1 = 32


def make_sprite(width, height, x_pos, y_pos, sprite):
    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)
Luca-Fly
  • 15
  • 5
  • 2
    You are making a new block and a new sprite for every single iteration. After 10 seconds, you'll have 250 copies of each. Don't do that. Make ONE, outside the loop, and alter their positions. – Tim Roberts Nov 18 '21 at 20:15
  • 1
    You call `pygame.image.load(sprite)` in every frame. (`make_sprite`) – Rabbid76 Nov 18 '21 at 20:16
  • 1
    Thanks for the help i just moved the make_block() functions to the outside of the loop and it worked. – Luca-Fly Nov 19 '21 at 07:12

1 Answers1

0

You appear to be making a new block every frame. Instead of making a new block and drawing it every frame, you could make one block and a seperate function to draw it.

lol no
  • 31
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 19 '21 at 00:12