I am making a rudimentary platformer, and while debugging the game I noticed a problem, whenever I run the game, nothing happens (my fps is 0) until I press a key, move my mouse, or cause any type of event. And this isn't just my fps tanking, if I do something my fps usually shoots to 100 or 200.
I have honestly no idea what is going on because I have never encountered this problem ever. I've tried changing the driver, changing display tags, messing with the clock, checked the docs and tried to find a question like this, but to no avail.
here is my main:
class Main:
def __init__(self):
import pygame
from pygame.math import Vector2
import os
from Database.Player import Player
import platform
pygame.init()
# file path
file_path = os.path.dirname(os.path.abspath(__file__))
# changing driver
if platform.system() == "Windows":
os.environ["SDL_VIDEODRIVER"] = "directx"
# screen vars
screen = pygame.display.set_mode((800, 800), pygame.RESIZABLE)
screen_info = pygame.display.Info()
screen_size = Vector2(screen_info.current_w, screen_info.current_h)
game_resolution = Vector2(800, 800)
game_offset = Vector2(screen_size / 2 - game_resolution / 2)
# class initializations
player = Player((400, 0), file_path)
# screen caption
pygame.display.set_caption(f"reeeeeeeeeeeeeeeeeeeeeeeeeeeee")
# clock
clock = pygame.time.Clock()
leave = False
while not leave:
pygame.display.set_caption(str(clock.get_fps()))
# delta time
delta_time = clock.get_time() / 1000
# key presses
key = pygame.key.get_pressed()
# event
event = pygame.event.wait()
if event.type == pygame.QUIT or key[pygame.K_DELETE]:
leave = True
elif event.type == pygame.VIDEORESIZE:
game_offset = Vector2(Vector2(event.size) / 2 - game_resolution / 2)
screen = pygame.display.set_mode(event.size, pygame.RESIZABLE)
# update
player.update(key, delta_time)
player.draw(screen, game_offset)
pygame.display.flip()
screen.fill((255, 255, 255))
clock.tick(1000)
pygame.display.quit()
return
if __name__ == "__main__":
Main()
and here is the player class
import pygame
from pygame.math import Vector2
class Player(object):
def __init__(self, start_pos, file_path):
# visual
self.image = pygame.Surface((20, 20))
# positional
self.pos = start_pos
self.rect = pygame.Rect(start_pos, self.image.get_size())
self.speed = Vector2(0)
self.deceleration = 0.1
self.acceleration = Vector2(4000, 0)
self.gravity = Vector2(0, 800)
self.max_speed = Vector2(1000, 1000)
self.jump_speed = Vector2(0, 500)
# properties
self.jump = True
def draw(self, surface, game_offset):
surface.blit(self.image, self.pos + game_offset)
def update(self, key, delta_time):
if key[pygame.K_a]:
self.speed.x -= self.acceleration.x * delta_time
if key[pygame.K_d]:
self.speed.x += self.acceleration.x * delta_time
if key[pygame.K_w] or key[pygame.K_SPACE] and self.jump:
self.speed.y -= self.jump_speed.y
self.speed.y += self.gravity.y * delta_time
self.speed.x = max(-self.max_speed.x, min(self.speed.x, self.max_speed.x))
self.speed.y = max(-self.max_speed.y, min(self.speed.y, self.max_speed.y))
self.pos += self.speed * delta_time
Am I missing something obvious here? Is my problem even reproducible on other pc's??? I honestly have no idea. I would be grateful for any advice anyone could give. And thank you for spending the time to read my code and answer if you do.
The call to pygame.event.wait()
will actually wait for an event. What you need to do (to keep things running) is to call pygame.event.get()
.
However, get()
returns a list of events that have occurred since you last called it, so the proper way of doing it is something like:
for event in pygame.event.get():
if event.type == pygame.QUIT or key[pygame.K_DELETE]:
leave = True
break # No need to carry on if you're finishing up.
if event.type == pygame.VIDEORESIZE:
game_offset = Vector2(Vector2(event.size) / 2 - game_resolution / 2)
screen = pygame.display.set_mode(event.size, pygame.RESIZABLE)
# elif other events.
# Update stuff regardless of events.
player.update(key, delta_time)
screen.fill((255, 255, 255))
player.draw(screen, game_offset)
pygame.display.flip()
clock.tick(1000)