Reputation: 20841
So I am trying to do some very basic things in pygame. It is my first few days using it so I am a beginner. I am trying to change the color of something whenever I press it with the mouse. I know how to change the colors by timing which is what I have my code down below. I am trying to change the color of the cloud in my code below, if you run it you will see the cloud is the top left and I have it changing between white and black every three seconds but I want it to change based on mousebuttondown. Thanks
import pygame, time, sys
from pygame.locals import *
def drawItem(windowSurface, x, y):
pygame.draw.polygon(windowSurface, RED, ((0+x, 100+y),(100+x, 100+y), (50+x, 50+y)))
pygame.draw.polygon(windowSurface, GREEN, ((0+x,100+y),(100+x,100+y),(100+x,200+y),(0+x,200+y)))
pygame.init()
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("Lab 9")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
GRASS = (26, 82, 26)
SKY = (179,237,255)
color = SKY
flag = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
windowSurface.fill(SKY)
drawItem(windowSurface,200,120)
pygame.draw.rect(windowSurface, GRASS, (0,300,500,500),0)
house = ((0+50, 100+50),(100+50, 100+50), (50+50, 50+50), (50+100, 50+100))
for i in range(3):
pygame.draw.circle(windowSurface,color, house[i], 80)
if flag == False:
color = WHITE
flag = True
elif flag == True:
color = BLACK
flag = False
pygame.display.update()
time.sleep(3)
Upvotes: 1
Views: 1614
Reputation: 59733
You've already discovered how to test for the type of an event (checking whether event.type == QUIT
).
You can extend this to check whether it's a mouse button click. Stick this in your for event in pygame.event.get()
loop:
if event.type == MOUSEBUTTONDOWN:
flag = not flag # This will swap the value of the flag
Then get rid of the flag = True
and flag = False
lines below, since you don't need them anymore. Also get rid of the time.sleep() call; or at least change it to a reasonable frame rate (like time.sleep(0.2)
= 50 frames per second).
Upvotes: 1