0

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?

6
  • 2
    You only update the display when there are events that aren't MOUSEMOTION - why?! Remember that indentation is important in Python... Commented Aug 24, 2015 at 10:43
  • @jonrsharpe this is for a map in my game, when events are MOUSEMOTION i want to move the map, when the events are MOUSEBUTTONUP i want to start a level but to test I've used the draw() function Commented Aug 24, 2015 at 10:50
  • Yes, but the point is that you want to update the display whatever happens! Commented Aug 24, 2015 at 10:51
  • @svs Hint: Remove 16 spaces from the last line. Commented Aug 24, 2015 at 10:53
  • @muddyfish so? (see edit) Commented Aug 24, 2015 at 10:55

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.