当前位置: 首页 > 知识库问答 >
问题:

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

茅华灿
2023-03-14

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

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个答案

曾嘉福
2023-03-14

问题在于,在清除<code>屏幕</code>表面之后,在绘制白色矩形/像素之前,检查像素。此时绘图框区域仍为黑色。快速修复方法是在绘制矩形的代码下面调用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)

此外,程序也可以简化。您可能只需使用<code>json</code>或<code>pickle</code>模块序列化<code>rects</code>列表,或者为绘图区域创建一个单独的曲面,然后只需将其保存为<code>pygame.image.save</code>。

这里有一个例子,我在另一个表面而不是屏幕上画画。可以用< code>pygame.image.save保存,用< code>pygame.image.load加载(按< kbd>S和< kbd>O)。我还推荐创建< code>pygame。Rect对象,因为它们有有用的碰撞检测方法,如< code>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)
 类似资料:
  • 我们有一个关于MySQL中返回错误整数值的函数的问题。我们已经检查了“booked_passeters”是否包含正确的值0,并且当移除该变量时,它可以正常工作,也就是说只返回整数40。但是,当我们试图从它中减去“booked_passeters”(最终仍应返回40)时,它就不起作用了。 包括下面的代码。 提前道谢!:-)

  • 我很清楚关于这个话题有多个问题,但我就是弄不懂它的意思。问题似乎是没有将新值添加到@cacheable列表中。 调试完问题后,我发现问题似乎出在钥匙上。 下面是代码片段 所以当我调用save方法时,用于缓存的关键字是incrementing integer,或者1,2,3...但是当我尝试获取所有文档时,缓存使用SimpleKey[]作为键。如果我尝试为@Cacheable使用相同的键,我会得到S

  • 问题内容: 使用math.pow或**运算符哪个更有效?我什么时候应该使用另一个? 到目前为止,我知道可以返回一个或一个,如果您使用小数,该函数将返回一个浮点数 问题答案: 使用Power运算符将更快,因为它不会产生函数调用的开销。如果您反汇编Python代码,则可以看到以下内容: 请注意,我在这里使用变量作为指数,因为类似常数的表达式实际上是在编译时求值的。 现在,实际上,这种差异并不重要,正如

  • 问题内容: 我想知道为什么’Y’返回2019,而’y’返回2018在SimpleDateFormat: System.out.println(new SimpleDateFormat(“Y”).format(new Date())); // prints 2019 System.out.println(new SimpleDateFormat(“y”).format(new Date())); /

  • 我有一个测试类包含测试方法和两个服务类和。我正在为类中的方法编写JUnit测试,该类包括对类方法的调用。在我写的测试方法中 因此,当调用时,它应该返回大小为1,但不返回此列表,返回的是一个大小为0的列表。

  • 数组: 我想得到元素的索引。所以,我在循环它。 电流输出 我错在哪里?