Advait M
Advait M

Reputation: 23

My pygame code is running horribly slow, how can I fix it?

So I'm trying to make tetris in pygame, and it was running really slow. I isolated the function that draws everything and even that is running really slow, how can I fix this? (And by running I mean I get a black screen and the desired images just load slowly from the bottom over the course of several minutes.)

P.S. There are some unused variables because I was too lazy to comment them out, but I don't imagine a couple variables make a big difference

import random
import time
import pygame
pygame.init()
win=pygame.display.set_mode((420, 840))
clock= pygame.time.Clock()

score= 0
level= 0
lines= 0
field= [[1]*10 for _ in range(22)]


run= True
play= True
setState= False
t= 48
frameCounter= 0
font= pygame.font.SysFont('arial', 30)

def drawWin():
        tempY= 40
        for y in range(2 ,22):
            tempX= 10
            for x in field[y]:
                if(x==1):
                    win.fill((255, 255, 255), (tempX, tempY, 40, 40))
                    pygame.draw.rect(win, (0, 0, 255), (tempX, tempY, 40, 40), 5)
                tempX+=40
            tempY+=40
        pygame.draw.rect(win, (255, 255, 255), (10, 40, 400, 800), 2)
        text= font.render('Score: '+ str(score)+ '\nLevel: '+ str(level), 1, (255, 255, 255))

while True:
    drawWin()
    for event in pygame.event.get():
        if event.type== pygame.QUIT:
            break
    time.sleep(1)
    clock.tick(t)

pygame.quit()

Upvotes: 0

Views: 197

Answers (2)

Andy
Andy

Reputation: 263

Try adding pygame.display.update() to the end of your while loop

I've had a similar issue running Pygame before :))

Upvotes: 2

Advait M
Advait M

Reputation: 23

Simple fix, forgot the update display command for pygame.

Upvotes: 1

Related Questions