【问题标题】:Why are pixels fuzzy in Pygame?为什么 Pygame 中的像素会模糊?
【发布时间】:2020-05-06 17:46:19
【问题描述】:

我刚刚开始使用 Pygame,我做了一些测试,只是在随机点打印像素。但是,我注意到像素似乎根本不是单个像素,而是几个像素的“模糊”斑点,如图所示。这是我用来绘制像素的代码: 有没有办法只显示单个像素?

编辑:这是我使用的全部代码:

import pygame.gfxdraw
import pygame
import random

width = 1000
height = 1000

screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
running = True

while running:
    x = random.randint(0,1000)
    y = random.randint(0,1000)

    pygame.gfxdraw.pixel(screen, x, y, (225,225,225))

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

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

more fuzzy pixels

【问题讨论】:

  • 您能否提供一个最小可重现的产品,其中包含生成窗口的内容?
  • 截图很好看,很清晰。您的显示器是否以它能够提供的精确分辨率运行?例如,如果您在 2560x1400 显示器上使用 1080x1024 分辨率,则必须重新缩放图像以适应物理显示器。这使它变得模糊。也许检查您的系统设置。或者......您上次清洁显示器是什么时候? ;)
  • 哈哈(:感谢您的提示,但事实证明我使用的缩放系统设置搞砸了一切。现在它是像素完美的!

标签: python pygame pixel resolution


【解决方案1】:

pygame.gfxdraw.pixel(surface, x, y, color) 将在给定的表面上绘制一个像素。 您还需要添加import pygame.gfxdraw

编辑:完整代码:

import pygame.gfxdraw
import pygame
import random

width = 1680
height = 1050

screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
running = True

x = [i for i in range(width - 10)]
x_full = [i for i in range(width)]
y = 100
y_full = [i for i in range(height // 2)]

while running:

    for i in x:
        for j in y_full:
            pygame.gfxdraw.pixel(screen, i, j, pygame.Color("red"))

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

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

尝试以这种方式对其进行测试,我将窗口大小设置为适合我的显示器分辨率,使其全屏显示。 x_fully 应该给你水平线。如果你减去,例如,10 你会得到稍短的线,反之亦然y_full 和一些随机的x。同样使用(width//2, height//2) 将恰好覆盖屏幕的四分之一。我认为这是准确的,pygame.gfxdraw.pixel(screen, i, j, pygame.Color("red")) 只显示应有的单个像素。 在您的代码中,您使用 random 来显示像素,它每秒添加 240 个像素,因此您很快就会在随机位置出现一堆像素,从而使像素彼此接近,看起来像一个“更大的像素”。我认为这就是这里发生的事情。如果我错了,请有人纠正我。 还可以制作小窗口,例如(100, 100) 并在(50, 50) 处绘制一个像素,这样可以更容易地看到它。如果您在 Windows 上,请使用放大镜进行测试。

重要提示: 在使用大量像素进行测试时,请在循环之外进行,因为它会消耗大量处理器功率来显示它们。 希望这能回答你的问题

【讨论】:

  • 我试过这个,但我最终遇到了同样的问题。会不会是显示问题?
  • 非常感谢你们,事实证明我很傻,使用 125% 缩放分辨率设置,所以在 100% 时一切都是像素完美的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2022-12-17
  • 2020-08-25
  • 2013-02-09
相关资源
最近更新 更多