python – 消除锯齿的Arc Pygame

python – 消除锯齿的Arc Pygame,第1张

概述我正在尝试使用Pygame在 Python中编写一个简单的循环计时器. 目前它看起来像这样: 如您所见,蓝线非常波浪状,并带有白点.我通过使用pygame.draw.arc()函数实现了这条蓝线,但它没有消除锯齿并且看起来很糟糕.我希望它是消除锯齿的,但gfxdraw模块应该让我实现这一点,不支持弧宽选择.这是代码片段: pygame.draw.arc(screen, blue, [center[ 我正在尝试使用Pygame在 Python中编写一个简单的循环计时器.
目前它看起来像这样:

如您所见,蓝线非常波浪状,并带有白点.我通过使用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 pygamefrom pygame.locals import *import pygame.gfxdrawimport math# Screen sizeSCREEN_HEIGHT = 350SCREEN_WIDTH = 500# colorsBLACK = (0,0)WHITE = (255,255,255)GREY = (150,150,150)RED = (255,0) # initialisationpygame.init()screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))done = Falseclock = pygame.time.Clock()# We need this if we want to be able to specify our#  arc in degrees instead of radiansdef 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 pixelsdef 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 backgroundscreen.fill(WHITE)center = [150,200]pygame.gfxdraw.aacircle(screen,BLACK)pygame.gfxdraw.aacircle(screen,BLACK)pygame.display.update()step = 10maxdeg = 0while 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 secondpygame.quit()

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

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

总结

以上是内存溢出为你收集整理的python – 消除锯齿的Arc Pygame全部内容,希望文章能够帮你解决python – 消除锯齿的Arc Pygame所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1196156.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存