循环集成,循环可以三重嵌套吗?

Loop integration, Can loops be triple nested?

这个程序应该做的是一旦用图像打开 pygame window,抓取鼠标点击 window 的坐标并将其用于循环这会更改 rgb 值并将其设置为缩放区域中的“否定效果”。

我的问题是关于如何整合这些循环以实现其预期目的?

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()

您必须在应用程序循环内的事件循环中处理所有事件。在应用程序循环中不断查询事件。
MOUSEBUTTONDOWN 事件 (event.pos) 中获取位置并更改此位置周围图像的颜色。
在应用程序循环中重绘图像:

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()