svs
svs

Reputation: 441

pygame.display.update() doesn't work in a loop

I'm working on a simple game. When i draw something in a loop it draws it for less than a second. This is my code:

import pygame, sys
from pygame.locals import*

pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()

DISPLAYSURF = pygame.display.set_mode((1000, 750))
BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
pygame.display.set_caption('Game')

while True:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEMOTION:
                "do something"

            else:
                posx, posy = pygame.mouse.get_pos()
                if event.type == pygame.MOUSEBUTTONUP:
                    if posx == 500 and posy == 500:
                        DISPLAYSURF.fill((40, 40, 40))
                        pygame.draw.rect(DISPLAYSURF, (40, 40, 40), (posx, posy, 50, 20))

        pygame.display.update()

What am i doing wrong?

Upvotes: 0

Views: 3115

Answers (1)

Fahim Shahriar
Fahim Shahriar

Reputation: 36

You are only updating the display if the mouse is at 500,500, and the event MOUSEBUTTONUP is triggered. I would change it to:

import pygame, sys
from pygame.locals import*

pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()

DISPLAYSURF = pygame.display.set_mode((1000, 750))
BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
pygame.display.set_caption('Game')

while True:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEMOTION:
            "do something"

        else:
            posx, posy = pygame.mouse.get_pos()
            if event.type == pygame.MOUSEBUTTONUP:
                if posx == 500 and posy == 500:
                    DISPLAYSURF.fill((40, 40, 40))
                    pygame.draw.rect(DISPLAYSURF, (40, 40, 40), (posx, posy, 50, 20))

    pygame.display.update()

Upvotes: 1

Related Questions