[英]having problem with drawing lines thickness in pygame
這是我使用 pygame 繪制的板: https://i.stack.imgur.com/Hne6A.png
當我在圖像上標記它們時,我遇到了最后兩行粗細的錯誤。 我相信我的代碼中的 if 語句有問題,但我不太明白,它不會生效。
這是上面繪制板的代碼:
import pygame, sys
pygame.init()
width, height = 750, 750
rows, cols = 9, 9
BLACK = (0,0,0)
screen = pygame.display.set_mode((width, height))
screen.fill((255, 255, 255, 255))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sys.exit()
surf = pygame.Surface((600, 600))
surf.fill((255,255,255))
padding = surf.get_width()/9
for i in range(rows+1):
if i % 3 == 0:
thick = 4
else:
thick = 1
pygame.draw.line(surf, BLACK, (0, i*padding), (width, i*padding), width=thick)
pygame.draw.line(surf, BLACK, (i*padding, 0), (i*padding, height), width=thick)
surf_center = (
(width-surf.get_width())/2,
(height-surf.get_height())/2
)
screen.blit(surf, surf_center)
pygame.display.update()
pygame.display.flip()
要繪制粗線,將一半的粗細應用到線的兩側。 最簡單的解決方案是使目標表面稍微大一點,並向線條的坐標添加一個小的偏移量。
出於性能原因,我還建議在應用程序循環之前創建表面並在應用程序循環中連續blit
它:
import pygame, sys
pygame.init()
width, height = 750, 750
rows, cols = 9, 9
BLACK = (0,0,0)
screen = pygame.display.set_mode((width, height))
surf_size = 600
surf = pygame.Surface((surf_size+4, surf_size+4))
surf.fill((255,255,255))
padding = surf_size/9
for i in range(rows+1):
thick = 4 if i % 3 == 0 else 1
offset = i * padding + 1
pygame.draw.line(surf, BLACK, (0, offset), (width, offset), width=thick)
pygame.draw.line(surf, BLACK, (offset, 0), (offset, height), width=thick)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((255, 255, 255, 255))
screen.blit(surf, surf.get_rect(center = screen.get_rect().center))
pygame.display.update()
pygame.display.flip()
pygame.quit()
sys.exit()
下面的代碼應該可以解決問題。 問題是pygame.draw.line
中的width
繪制在線的中間。 因此,邊界線實際上被剪成兩半,因為沒有考慮一側的一半和另一側的另一半。 但要注意的是,如果厚度均勻,則邊界上的偏移量為 1,因為其中一側的寬度較大。
即使厚度 ( bold_think
) 升高或降低,下面的算法也能完美運行,網格外沒有任何間隙。
import pygame
import sys
pygame.init()
width, height = 750, 750
rows, cols = 9, 9
BLACK = (0, 0, 0)
screen = pygame.display.set_mode((width, height))
screen.fill((255, 255, 255, 255))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sys.exit()
surf = pygame.Surface((600, 600))
surf.fill((255, 255, 255))
bold_thick = 4
# Calculates the offset of 1 for the even widths
thick_offset = (bold_thick - 1) % 2
offset = (bold_thick / 2) - thick_offset
# The padding should take into account the borders
padding = (surf.get_width() - bold_thick) / 9
for i in range(rows+1):
if i % 3 == 0:
thick = bold_thick
else:
thick = 1
pygame.draw.line(surf, BLACK, (0, i*padding + offset),
(width, i*padding + offset), width=thick)
pygame.draw.line(surf, BLACK, (i*padding + offset, 0),
(i*padding + offset, height), width=thick)
surf_center = (
(width-surf.get_width())/2,
(height-surf.get_height())/2
)
screen.blit(surf, surf_center)
pygame.display.update()
pygame.display.flip()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.