加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 消除锯齿的Arc Pygame

发布时间:2020-12-20 13:12:48 所属栏目:Python 来源:网络整理
导读:我正在尝试使用Pygame在 Python中编写一个简单的循环计时器. 目前它看起来像这样: 如您所见,蓝线非常波浪状,并带有白点.我通过使用pygame.draw.arc()函数实现了这条蓝线,但它没有消除锯齿并且看起来很糟糕.我希望它是消除锯齿的,但gfxdraw模块应该让我实现
我正在尝试使用Pygame在 Python中编写一个简单的循环计时器.
目前它看起来像这样:

Timer

如您所见,蓝线非常波浪状,并带有白点.我通过使用pygame.draw.arc()函数实现了这条蓝线,但它没有消除锯齿并且看起来很糟糕.我希望它是消除锯齿的,但gfxdraw模块应该让我实现这一点,不支持弧宽选择.这是代码片段:

pygame.draw.arc(screen,blue,[center[0] - 120,center[1] - 120,240,240],pi/2,pi/2+pi*i*koef,15)
pygame.gfxdraw.aacircle(screen,center[0],center[1],105,black)
pygame.gfxdraw.aacircle(screen,120,black)

解决方法

我不知道任何可以解决这个问题的pygame函数,这意味着你基本上必须自己编写一个解决方案(或者使用除pygame之外的其他东西),因为你已经注意到了draw并且gfxdraw不会给你厚度.

一个非常丑陋但简单的解决方案是在弧段上绘制多次,总是稍微移动以“填充”缺失的间隙.这仍然会在计时器弧的最前面留下一些锯齿,但其余部分将被填入.

import pygame
from pygame.locals import *
import pygame.gfxdraw
import math

# Screen size
SCREEN_HEIGHT = 350
SCREEN_WIDTH = 500

# Colors
BLACK = (0,0)
WHITE = (255,255,255)
GREY = (150,150,150)
RED = (255,0)


 # initialisation
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
done = False
clock = pygame.time.Clock()

# We need this if we want to be able to specify our
#  arc in degrees instead of radians
def degreesToRadians(deg):
    return deg/180.0 * math.pi

# Draw an arc that is a portion of a circle.
# We pass in screen and color,# followed by a tuple (x,y) that is the center of the circle,and the radius.
# Next comes the start and ending angle on the "unit circle" (0 to 360)
#  of the circle we want to draw,and finally the thickness in pixels
def drawCircleArc(screen,color,center,radius,startDeg,endDeg,thickness):
    (x,y) = center
    rect = (x-radius,y-radius,radius*2,radius*2)
    startRad = degreesToRadians(startDeg)
    endRad = degreesToRadians(endDeg)

    pygame.draw.arc(screen,rect,startRad,endRad,thickness)


# fill screen with background
screen.fill(WHITE)
center = [150,200]
pygame.gfxdraw.aacircle(screen,BLACK)
pygame.gfxdraw.aacircle(screen,BLACK)

pygame.display.update()

step = 10
maxdeg = 0

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    maxdeg = maxdeg + step
    for i in range(min(0,maxdeg-30),maxdeg):
        drawCircleArc(screen,RED,(150,200),119,i+90,max(i+10,maxdeg)+90,14)  
        #+90 will shift it from starting at the right to starting (roughly) at the top
    pygame.display.flip()

    clock.tick(2)  # ensures a maximum of 60 frames per second

pygame.quit()

请注意,我已从https://www.cs.ucsb.edu/~pconrad/cs5nm/08F/ex/ex09/drawCircleArcExample.py复制degreesToRadians和drawCircleArc

我一般不推荐这种解决方案,但它可能会在紧要关头.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读