gpt4 book ai didi

python - 如何在pygame中显示由点组成的圆圈?

转载 作者:行者123 更新时间:2023-12-04 07:42:05 24 4
gpt4 key购买 nike

我有一个像这样的像素对象:

class Pixel:
def __init__(self, screen, color, pos, size=8):
self.screen = screen
self.color = color
self.pos = pos
self.size = size

def update(self):
pygame.draw.circle(self.screen, self.color, self.pos, self.size)
我想创建一个输出,看起来像一个由这些 Pixel 对象组成的圆圈。到目前为止,我已经尝试过这样的程序:
# PyMate

import pygame
from pygame.colordict import THECOLORS
from drawing import Pixel, Frame

frame = Frame((800, 650))
frames = [frame]
pixels = []

x, y = 50, 50
li = []

for i in range(1000):
li.append(Pixel(frames[0], THECOLORS["orange"], (x, y)))

if i % 500 == 0:
x -= 0.002
y -= 0.006
else:
y += 0.006
x += 0.002

pixels.extend(li)
frames.append(frame)

running = True
while running:
frame = Frame((800, 650))
frames.append(frame)

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

for i in frames:
i.fill(THECOLORS["white"])

for i in pixels:
i.update()

del frame, frames[0]

pygame.display.update()
这种作品,但它画了一条直线(不是我想要的)。我真正关注的部分是:
for i in range(1000):
li.append(Pixel(frames[0], THECOLORS["orange"], (x, y)))

if i % 500 == 0:
x -= 0.002
y -= 0.006
else:
y += 0.006
x += 0.002
x 和 y 变化似乎不正确,模数似乎也不正确。他们可能是什么数字?

最佳答案

一种方法是使用长度不变但角度变化的向量向前推进:

x, y = 300, 200
step = 20
angle = 0
for i in range(36):
x -= math.sin(angle*math.pi/180) * step
y += math.cos(angle*math.pi/180) * step
angle += 10
pixels.append(Pixel(screen, "orange", (x, y)))

最小的例子:

import pygame
import math

class Pixel:
def __init__(self, screen, color, pos, size=8):
self.screen = screen
self.color = color
self.pos = pos
self.size = size
def update(self):
pygame.draw.circle(self.screen, self.color, self.pos, self.size)

screen = pygame.display.set_mode((800, 650))

pixels = []
x, y = 300, 200
step = 20
angle = 0
for i in range(36):
x -= math.sin(angle*math.pi/180) * step
y += math.cos(angle*math.pi/180) * step
angle += 10
pixels.append(Pixel(screen, "orange", (x, y)))

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

screen.fill(0)
for p in pixels:
p.update()
pygame.display.update()

关于python - 如何在pygame中显示由点组成的圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67408873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com