2014-11-24 59 views
2

我想在終點上畫幾個帶正方形的弧線,而我大部分都是這樣,但有些東西並不完全正確。在我認爲弧應該結束的地方和它被繪製的地方之間總會有幾個像素的間隙。示例代碼如下:Pygame.draw.arc()完成bug還是隻是我?

import pygame 
from math import pi 

pygame.init() 
screen = pygame.display.set_mode([1000,1000]) 
clock = pygame.time.Clock() 
done = False 

while not done: 
    clock.tick(10) 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
    screen.fill((0,0,0)) 

    # draw first arc just past pi/2 to make sure arc is not cut off by rectangle bounds 
    pygame.draw.arc(screen, (255,255,255),[100,100,800,800], 0, 9 * pi/16, 1) 

    # normal, easy, simple, arc does not touch vertical red line as expected 
    pygame.draw.arc(screen, (255,255,255),[200,200,600,600], 0, 2 * pi, 1) 

    # shorter arc because in my actual code it seems that smaller arcs have larger gaps 
    # but I can't seem to tell here besides it doesn't connect either 
    pygame.draw.arc(screen, (255,255,255),[300,300,400,400], 4 * pi/16, pi/2, 1) 

    # Horizontal and vertical lines for comparison 
    pygame.draw.line(screen, (255,0,0), [500,500], [500,0]) 
    pygame.draw.line(screen, (255,0,0), [500,500], [900,500]) 

    pygame.display.flip() 

pygame.quit() 

[示例輸出] [1]注意鼠標光標左側的空白處。 http://i.stack.imgur.com/0zxkG.jpg

OS X和Linux Mint上的結果相同。首先繪製線條沒有什麼區別,它總是終點而不是起點。謝謝閱讀!

+0

綜觀API,該'arc'函數接受一個起始角度和停止角度,但它看起來像你只指定的一個二。這可能是問題嗎? – jme 2014-11-24 15:58:26

+0

對不起,這是一個錯字。我已經編輯了代碼,將它們的起始點添加爲0。除了從pi/4到pi/2的最後一個圓弧外 – 2014-11-24 16:44:41

回答

0

解決多虧/ U/Mekire在this reddit post

import pygame 
from pygame import gfxdraw 

RED = pygame.Color("red") 
WHITE = pygame.Color("white") 

def draw_arc(surface, center, radius, start_angle, stop_angle, color): 
    x,y = center 
    start_angle = int(start_angle%360) 
    stop_angle = int(stop_angle%360) 
    if start_angle == stop_angle: 
     gfxdraw.circle(surface, x, y, radius, color) 
    else: 
     gfxdraw.arc(surface, x, y, radius, start_angle, stop_angle, color) 


pygame.init() 
screen = pygame.display.set_mode([500,500]) 
screen_rect = screen.get_rect() 
x,y = screen_rect.center 
clock = pygame.time.Clock() 
done = False 

while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
    screen.fill((0,0,0)) 
    #Arcs 
    draw_arc(screen, screen_rect.center, 200, 45, 360, WHITE) 
    draw_arc(screen, screen_rect.center, 220, 0, 360, WHITE) 
    draw_arc(screen, screen_rect.center, 100, -90, -135, WHITE) 
    draw_arc(screen, screen_rect.center, 120, -135, -90, WHITE) 
    # Horizontal and vertical lines for comparison 
    pygame.draw.line(screen, RED, screen_rect.center, screen_rect.midright) 
    pygame.draw.line(screen, RED, screen_rect.center, screen_rect.midtop) 
    pygame.draw.line(screen, RED, screen_rect.topleft, screen_rect.bottomright) 
    pygame.display.flip() 
    clock.tick(30) 

pygame.quit() 
相關問題