为什么 Pygame 中的像素模糊?
Why are pixels fuzzy in Pygame?
我刚刚开始使用 Pygame,我做了一些测试,只在随机点打印像素。但是,我注意到像素似乎根本不是单个像素,而是 'fuzzy' 几个像素的斑点,如图所示。这是我用来绘制像素的代码:
有没有办法只显示单个像素?
编辑:这是我使用的全部代码:
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
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)
尝试以这种方式进行测试,我将 window 大小设置为适合我的显示器分辨率,因此它可以全屏显示。 x_full
和 y
应该给你水平线。如果你减去,例如,10,你会得到稍微短一点的线,反之亦然 y_full
和一些随机的 x
。同样使用 (width//2, height//2)
将正好覆盖屏幕的四分之一。我认为它是准确的,pygame.gfxdraw.pixel(screen, i, j, pygame.Color("red"))
只显示它应该显示的单个像素。
在你的代码中,你使用 random
来显示像素,它每秒添加 240 个像素,所以你很快就会在随机位置得到一堆像素,导致像素彼此靠近,看起来像 "bigger one".我认为这就是这里发生的事情。如果我错了,请有人纠正我。
也使 window 变小,例如(100, 100)
并在(50, 50)
画一个像素,这样更容易看出来。如果你在 windows 使用放大镜来测试它。
重要:
在使用大量像素进行测试时,请在循环外进行,因为显示它们会消耗大量处理器功率。
希望这能回答您的问题
我刚刚开始使用 Pygame,我做了一些测试,只在随机点打印像素。但是,我注意到像素似乎根本不是单个像素,而是 'fuzzy' 几个像素的斑点,如图所示。这是我用来绘制像素的代码: 有没有办法只显示单个像素?
编辑:这是我使用的全部代码:
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
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)
尝试以这种方式进行测试,我将 window 大小设置为适合我的显示器分辨率,因此它可以全屏显示。 x_full
和 y
应该给你水平线。如果你减去,例如,10,你会得到稍微短一点的线,反之亦然 y_full
和一些随机的 x
。同样使用 (width//2, height//2)
将正好覆盖屏幕的四分之一。我认为它是准确的,pygame.gfxdraw.pixel(screen, i, j, pygame.Color("red"))
只显示它应该显示的单个像素。
在你的代码中,你使用 random
来显示像素,它每秒添加 240 个像素,所以你很快就会在随机位置得到一堆像素,导致像素彼此靠近,看起来像 "bigger one".我认为这就是这里发生的事情。如果我错了,请有人纠正我。
也使 window 变小,例如(100, 100)
并在(50, 50)
画一个像素,这样更容易看出来。如果你在 windows 使用放大镜来测试它。
重要: 在使用大量像素进行测试时,请在循环外进行,因为显示它们会消耗大量处理器功率。 希望这能回答您的问题