0
\$\begingroup\$

Sorry for the bad title, if you think of a better one please tell me.

So I was asked to make a button that says a line from a video game. I attempted to have the while loop add 1 to a variable every time it looped through, then display the variable in the caption of the window. I did this so that I could see if it was looping, and really, what was going on. It showed a rapidly increasing number, then froze the computer (I had to restart the computer). I tried putting a short wait in the loop, still would not respond. I am also having this exact same problem in a much longer and more complex program that I'm working on, so if I figure it out, it will solve both issues. Don't just give me code, please tell me why I need to use it or what it does. Here's the program:

from pygame import *
import pygame
pygame.init()

pygame.display.set_caption('Potato')
screen = pygame.display.set_mode((224, 224))
button = pygame.image.load("C:/Python34/redbutton.jpg")
screen.blit(button,(0,0))
pygame.display.flip()

potato = pygame.mixer.Sound("C:/Python34/potato.wav")

test = 0
pressed = False

while pressed == False:
    if pygame.mouse.get_pressed()[1]:
        pressed = True

    if pygame.mouse.get_pressed()[0]:
        potato.play()

sys.exit()
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

From the documentation for pygame.mouse.get_pressed():

Note, remember to call pygame.event.get() before this function. Otherwise it will not work.

\$\endgroup\$
3
  • \$\begingroup\$ And while I'm on the topic, it also says "it is better to use either pygame.event.wait() or pygame.event.get() and check all of (the accumulated) events" instead of just testing whether the mouse button happens to be down at this particular moment. \$\endgroup\$ Commented May 14, 2015 at 0:37
  • \$\begingroup\$ No worries! If this works for you, please upvote and/or accept the answer, so the site knows that the question's been answered. :) \$\endgroup\$ Commented May 14, 2015 at 0:39
  • \$\begingroup\$ Sorry I didn't upvote earlier, it said I had to wait. Thanks! \$\endgroup\$
    – Nate.Olson
    Commented May 14, 2015 at 0:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .