pygame.surface.get_at((x, y)) 没有返回正确的像素数据

pygame.surface.get_at((x, y)) not returning correct pixel data

我有一个简单的程序,允许用户在黑框中绘制白色像素。当用户单击蓝色按钮时,我希望我的程序遍历绘图框中的所有像素并将像素数据添加到数组中以供以后在另一个项目中使用。但是,即使用户在那里绘制了图像,每个像素也只有 returns 黑色。

import pygame
from pygame.locals import *
import sys

# set up the screen
pygame.init()
screen = pygame.display.set_mode((200, 200))
pygame.display.set_caption("drawTest")
clock = pygame.time.Clock()

# goes through the screen data and reads the pixel data
def GetPixelDataOfDrawScreen():
    for i in range(200):
        for j in range(200):
            if(i > 49 and i < 150):
                if(j > 49 and j < 150):

                    # gets the red, green, blue and alpha (RGBA) components of the pixel
                    pixelColor = screen.get_at((i, j))
                    print(i, pixelColor[0])

                    if(pixelColor[0] == 255):
                        pixelDataOfDrawBox.append(255)
                    else:
                        pixelDataOfDrawBox.append(0)

                    # dosen't work
    print(pixelDataOfDrawBox)

pixelDataOfDrawBox = []
rects = []

while True:

    for event in pygame.event.get(): 
            if event.type == pygame.QUIT: # adds close button to screen
                sys.exit()

    screen.fill((255, 255, 255))                                                    

    # gets the mouse position
    mouseX, mouseY = pygame.mouse.get_pos()

    # draw drawing box
    boxX = 50
    boxY = 50
    pygame.draw.rect(screen, (0, 0, 0), (boxX, boxY, 100, 100))

    if(mouseX >= boxX and mouseX <= boxX + 100 - 2): # check if drawing
        if(mouseY >= boxY and mouseY <= boxY + 100 - 2):
            if(pygame.mouse.get_pressed()[0]):
                rects.append([mouseX, mouseY, 2, 2])

    # blue button
    bButtonX = 100
    bButtonY = 150
    pygame.draw.rect(screen, (0, 0, 255), (bButtonX, bButtonY, 50, 20))

    if(mouseX >= bButtonX and mouseX <= bButtonX + 50): # check if pressed
        if(mouseY >= bButtonY and mouseY <= bButtonY + 20):
            pygame.draw.rect(screen, (0, 0, 180), (bButtonX, bButtonY, 50, 20))
            if(pygame.mouse.get_pressed()[0]):
                GetPixelDataOfDrawScreen()

    # red button
    rButtonX = 50
    rButtonY = 150
    pygame.draw.rect(screen, (255, 0, 0), (rButtonX, rButtonY, 50, 20))

    if(mouseX >= rButtonX and mouseX <= rButtonX + 50):
        if(mouseY >= rButtonY and mouseY <= rButtonY + 20):
            pygame.draw.rect(screen, (180, 0, 0), (rButtonX, rButtonY, 50, 20))

    # draw the rects
    for r in rects:
        pygame.draw.rect(screen, (255, 255, 255), (r[0], r[1], r[2], r[3]))

    pygame.display.flip()
    clock.tick(60)

问题是您在清除 screen 表面之后和绘制白色 rects/pixels 之前检查像素。绘图框区域此时还是黑色的。快速解决方法是在绘制矩形的代码下方调用 GetPixelDataOfDrawScreen

# draw the rects
for r in rects:
    pygame.draw.rect(screen, (255, 255, 255), (r[0], r[1], r[2], r[3]))

if(mouseX >= bButtonX and mouseX <= bButtonX + 50): # check if pressed
    if(mouseY >= bButtonY and mouseY <= bButtonY + 20):
        pygame.draw.rect(screen, (0, 0, 180), (bButtonX, bButtonY, 50, 20))
        if(pygame.mouse.get_pressed()[0]):
            GetPixelDataOfDrawScreen()

pygame.display.flip()
clock.tick(60)

另外,程序可以简化。您可能只需使用 json or pickle modules, or create a separate surface for the drawing area and then just save it with pygame.image.save.

序列化 rects 列表

这是我在另一个表面而不是屏幕上绘图的示例。您可以使用 pygame.image.save 保存它并使用 pygame.image.load 加载它(按 SO)。我还建议为框和按钮创建 pygame.Rect 对象,因为它们具有有用的碰撞检测方法,例如 collidepoint:

import sys
import pygame


pygame.init()
screen = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()

box_surface = pygame.Surface((100, 100))
box = box_surface.get_rect(topleft=(50, 50))
blue_button = pygame.Rect(100, 150, 50, 20)
red_button = pygame.Rect(50, 150, 50, 20)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_s:  # Press s to save the image.
                pygame.image.save(box_surface, 'box.png')
            elif event.key == pygame.K_o:  # Press o to load the image.
                box_surface = pygame.image.load('box.png').convert()

    mouse_pos = pygame.mouse.get_pos()
    if pygame.mouse.get_pressed()[0]:
        # Adjust the mouse positions because the box is positioned at (100, 100).
        mouse_x, mouse_y = mouse_pos[0]-box.x, mouse_pos[1]-box.y
        pygame.draw.rect(box_surface, (255, 255, 255), [mouse_x, mouse_y, 2, 2])

    # Draw everything.
    screen.fill((255, 255, 255))
    screen.blit(box_surface, box)
    pygame.draw.rect(screen, (0, 0, 255), blue_button)
    pygame.draw.rect(screen, (255, 0, 0), red_button)
    if red_button.collidepoint(mouse_pos):
        pygame.draw.rect(screen, (180, 0, 0), red_button)
    if blue_button.collidepoint(mouse_pos):
        pygame.draw.rect(screen, (0, 0, 180), blue_button)

    pygame.display.flip()
    clock.tick(60)