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

时间:2018-11-29 17:17:05

标签: pygame pixel pygame-surface

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

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)

1 个答案:

答案 0 :(得分:0)

问题是您要清除[('homemade', 'NOUN'), ('green', 'ADJ'), ('tea', 'NOUN'), ('powder', 'NOUN')]表面之后且绘制白色矩形/像素之前检查像素。此时绘图框区域仍为黑色。一种快速的解决方法是在绘制矩形的代码下面调用screen

GetPixelDataOfDrawScreen

此外,该程序可以简化。您可能只是使用json# 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) 模块序列化rects列表,或者为绘图区域创建一个单独的表面,然后仅用pygame.image.save保存它。

在此示例中,我绘制到另一个表面而不是屏幕上。您可以使用pickle保存它,并使用pygame.image.save加载它(按 S O )。我还建议为框和按钮创建pygame.Rect对象,因为它们具有pygame.image.load这样的有用的冲突检测方法:

collidepoint