What this program is supposed to do is once the pygame window is opened with the image, coordinates from a mouse click on the window are grabbed and used into a loop that changes and sets the rgb values to a "negated effect" in a scaled region.
My question related to how these loops can be integrated to function to its intended purpose?
import pygame
import sys
from pygame.locals import *
simage = pygame.image.load(sys.argv[1]) #retrieve image from command arg
(w, h) = simage.get_size()
window = pygame.display.set_mode((w, h))
window.blit(simage, (0,0))
a = 0
b = 0
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_presses = pygame.mouse.get_pressed()
if mouse_presses[0]:
print("mouse")
(a, b) = pygame.mouse.get_pos()
for y in range(a, a*10):
for x in range(b, b*10):
(r, g, b, _) = simage.get_at((x, y))
simage.set_at((x, y), 255-r, 255-g, 255-b)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
CodePudding user response:
You must handle all the event in an event loop inside the application loop. The events are continuously queried in the application loop.
Get the position from the MOUSEBUTTONDOWN
event (event.pos
) and change the color of the image around this position.
Redraw the image in the application loop:
import pygame
from pygame.locals import *
simage = pygame.image.load(sys.argv[1]) #retrieve image from command arg
w, h = simage.get_size()
window = pygame.display.set_mode((w, h))
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
x0, y0 = event.pos
for y in range(y0-5, y0 5):
for x in range(x0-5, x0 5):
if 0 <= x < w and 0 <= y < h:
r, g, b, _ = simage.get_at((x, y))
simage.set_at((x, y), (255-r, 255-g, 255-b))
window.fill(0)
window.blit(simage, (0, 0))
pygame.display.update()
pygame.quit()