python绘制填充颜色的饼形状

2024-04-20 04:11:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着画一个馅饼形状。我试着用不同的方法来做这件事。代码如下:

ball = pygame.draw.circle(self.screen, self.pink, self.pos, self.r, 0)
pygame.gfxdraw.pie(self.screen, 60,60, 40, 0, 90,(0,255,0))
pygame.gfxdraw.arc(self.screen, 60,60, 40, 180, 270,(0,255,255))
pygame.draw.arc(self.screen, (255,0,255),ball,0, math.pi/4, ball.width/2)

输出图像如下:
enter image description here

我想要馅饼形状充满颜色,就像洋红的形状一样。我使用了arc函数并用=半径来设置这条线(代码中的第四行)。然而,颜色并不是均匀填充的。我试着画出一种颜色来填充第二种形状。。。在

非常感谢你的帮助!在


Tags: 方法代码posself颜色screenpygame形状
1条回答
网友
1楼 · 发布于 2024-04-20 04:11:47

您只需绘制一个足够细的多边形(例如以一度为间隔):

import math
import pygame

# Center and radius of pie chart
cx, cy, r = 100, 320, 75

# Background circle
pygame.draw.circle(screen, (17, 153, 255), (cx, cy), r)

# Calculate the angle in degrees
angle = val*360/total

# Start list of polygon points
p = [(cx, cy)]

# Get points on arc
for n in range(0,angle):
    x = cx + int(r*math.cos(n*math.pi/180))
    y = cy+int(r*math.sin(n*math.pi/180))
    p.append((x, y))
p.append((cx, cy))

# Draw pie segment
if len(p) > 2:
    pygame.draw.polygon(screen, (0, 0, 0), p)

相关问题 更多 >