Reputation: 870
I'm trying to make a paint brush game in pygame. Ive done the painting, but i want the user to click on an image and that will change the brush of the paint brush. If user clicks on blue image, the paint brush becomes blue. So far, I have manged to display the blue image and i have a default red paint brush. The paint brush colour only changes when the position of the blue image is touched. i want the paint brush color to remain when the user clicks on any other area of the screen.
Any suggestions?
Here is my code:
import sys, pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1000,600))
screen.fill((255,255,255))
brush = pygame.image.load("redbrush.png")
brush = pygame.transform.scale(brush,(45,45))
brush2 = pygame.image.load("bluebrush.png")
brush2 = pygame.transform.scale(brush2,(45,45))
pygame.display.update()
clock = pygame.time.Clock()
z = 0
while 1:
screen.blit(brush2, (0,10))
pygame.display.update()
clock.tick(60)
x,y = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type ==pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type ==MOUSEBUTTONDOWN:
z=1
elif event.type ==MOUSEBUTTONUP:
z=0
if z==1:
screen.blit(brush, (x -23.5,y-23.5))
pygame.display.update()
if brush2.get_rect().collidepoint(pygame.mouse.get_pos()):
screen.blit(brush2, (x -23.5,y-23.5))
pygame.display.update()
Upvotes: 0
Views: 1314
Reputation: 14731
Replace your lines:
if brush2.get_rect().collidepoint(pygame.mouse.get_pos()):
screen.blit(brush2, (x -23.5,y-23.5))
pygame.display.update()
with:
if brush2.get_rect().collidepoint(pygame.mouse.get_pos()):
(brush, brush2) = (brush2, brush) # swaps brush and brush2
pygame.display.update()
EDITED: I saw your comments and here's an edited version of your code:
import sys, pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1000,600))
screen.fill((255,255,255))
brush1 = pygame.image.load("redbrush.png")
brush1 = pygame.transform.scale(brush1,(45,45))
pos1 = (0, 32)
brush2 = pygame.image.load("bluebrush.png")
brush2 = pygame.transform.scale(brush2,(45,45))
brush2 = pygame.transform.scale(brush2,(45,45))
pos2 = (0, 64)
brush = brush1
pygame.display.update()
clock = pygame.time.Clock()
z = 0
while 1:
screen.blit(brush1, pos1)
screen.blit(brush2, pos2)
pygame.display.update()
clock.tick(60)
x,y = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type ==pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type ==MOUSEBUTTONDOWN:
z=1
elif event.type ==MOUSEBUTTONUP:
z=0
if z==1:
if brush1.get_rect(center=pos1).collidepoint(pygame.mouse.get_pos()):
brush = brush1
if brush2.get_rect(center=pos2).collidepoint(pygame.mouse.get_pos()):
brush = brush2
screen.blit(brush, (x -23.5,y-23.5))
pygame.display.update()
Upvotes: 1