Im trying to code a simple game, the game has a player object, and a floor using loops to create it. Im not sure why its losing frames soo quickly though. I have clock to tick 60 fps but quickly drops to 15 fps. Thanks in advance!
main.py
black = (0, 0, 0)
blue = (50, 60, 200)
white = (255, 255, 255)
font = pygame.font.Font(None, 30)
window = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Platformer")
gravity = -0.5
player = Player(400, 0)
level1 = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
]
for y in range(0, len(level1)):
for x in range(0, len(level1[y])):
if level1[y][x] == 1:
blockList.append(Block(x*32, y*32))
window.fill(blue)
for block in blockList:
block.render(window)
player.x += moveX
player.update(gravity, blockList)
player.render(window)
clock.tick(60)
for x in range(0, len(level1[y])):
if level1[y][x] == 1:
blockList.append(Block(x*32, y*32))``` Im pretty sure thats where the error is...
There is a good learning point in here. One of the first steps in optimizing code is taking the unnecessary math out of the loops. As suggested in comment, if you are running that double loop inside of each frame computation, it is probably slowing you down a lot. Perhaps other things are too.
Let's assume that your "level 1" blocks are not changing frame-to-frame. So, we can compute blacklist before you start your loop.
# make blocklist
level1= [[0,0, ... , 0],
...
[1,1, ... , 1]]
blocklist = []
for y ...
for x ...
blocklist.append(...)
# now start your frame loop
while True:
window.fill()
for block in blocklist:
...
clock.tick(60)