1
import pygame

pygame.init()

win = pygame.display.set_mode((800, 600))

在数组中列出 pygame 窗口中的所有像素

pts = pygame.PixelArray(win)

创建一些颜色常量

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREY = (128, 128, 128)

clicked = False

clock = pygame.time.Clock()

# GAME LOOP

while True:
    win.fill(BLACK)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

检查是否单击并按住鼠标左键

        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                clicked = True

检查鼠标按钮是否释放

        elif event.type == pygame.MOUSEBUTTONUP:
            clicked = False

不知道下面是不是有什么问题。

    if clicked:
        mouse_X, mouse_Y = pygame.mouse.get_pos()
        for a in range(mouse_X, mouse_X + 79):
            pts[a][mouse_Y:mouse_Y + 60] = GREEN

    pygame.display.update()
    clock.tick(250)
4

1 回答 1

0

这是你逻辑的问题。矩形不是永久绘制的。
pts[a][mouse_Y:mouse_Y + 60] = GREEN更改win Surface中的一个像素。然而win.fill(BLACK),将所有像素win变成黑色。

将矩形放置在permanant_win Surface上后立即复制“win”表面。Surface作为应用程序循环开始时窗口的背景blit

import pygame
pygame.init()
win = pygame.display.set_mode((800, 600))

BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
clicked = False
clock = pygame.time.Clock()
permanant_win = win.copy()

while True:
    win.blit(permanant_win, (0, 0))

    make_permanent = False
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                clicked = True
        elif event.type == pygame.MOUSEBUTTONUP:
            make_permanent = True

    if clicked:
        mouse_X, mouse_Y = pygame.mouse.get_pos()
        pts = pygame.PixelArray(win)
        for a in range(mouse_X, mouse_X + 79):
            pts[a][mouse_Y:mouse_Y + 60] = GREEN
        pts = None
        if make_permanent:
            permanant_win = win.copy()

    pygame.display.update()
    clock.tick(250)
于 2021-04-16T07:46:55.537 回答