我在使用pygame显示表面变化时遇到问题

时间:2020-03-17 04:47:11

标签: python pygame

我当前的目标是使一个较小的圆围绕一个较大圆的中心旋转,并使矩形遵循较小圆的路径。

我创建了一个TargetCircle类,因为完成此可视化时总共将有18个圆圈。 我的问题是,在更改每个圆的theta值并尝试显示更改后,什么都没有发生。

当前,显示的只是顶部的9个圆圈以及水平线和垂直线,但是,所有内容均保持不变。任何帮助将非常感激。谢谢。

import pygame as pg
import pygame.gfxdraw
import math

pg.init()
windowWidth = 800
windowHeight = 800
surface = pg.display.set_mode((windowWidth, windowHeight))
pg.display.set_caption("Circle")
clock = pg.time.Clock()
black = (0, 0, 0)
white = (255, 255, 255)
gray = (50, 50, 50)
red = (255, 0, 0)


class TargetCircle(object):
    def __init__(self, posX, posY):
        self.circlePositionX = posX
        self.circlePositionY = posY
        self.radius = 38
        self.theta = 0
        self.x = int((self.circlePositionX + (self.radius * math.cos(self.theta))))
        self.y = int((self.circlePositionY + (self.radius * math.sin(self.theta))))

    def draw(self, win):
        pg.draw.rect(win, gray, (0, self.y, 800, 1))
        pg.draw.rect(win, gray, (self.x, 0, 1, 800))
        pygame.gfxdraw.aacircle(win, self.circlePositionX, self.circlePositionY, self.radius, white)
        pygame.gfxdraw.filled_circle(win, self.x, self.y, 2, white)


circleList = []
x = 120
for i in range(1, 10):
    circle = TargetCircle(x, 40)
    circle.draw(surface)
    circleList.append(circle)
    x += 80
pg.display.update()
loop = 0
run = True
while run:

    clock.tick(160)
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False

    if loop == len(circleList):
        loop = 0

    surface.fill(0)
    for i in range(len(circleList)):
        circleList[i].theta += .10
        circleList[i].draw(surface)
    pg.display.flip()
    loop += 1

pg.quit()

1 个答案:

答案 0 :(得分:2)

问题是您没有更新<RouterLink to="/"> <img alt="Logo" src="/images/logos/logo--white.svg" /> <Typography variant="h5" component="h2" style={{display: 'inline'}}> Hello World </Typography> </RouterLink> 的x和y值。您设置了

TargetCircle

一次进入 self.x = int((self.circlePositionX + (self.radius * math.cos(self.theta)))) self.y = int((self.circlePositionY + (self.radius * math.sin(self.theta)))) (在开始时被调用一次),然后再也不会设置它。为了解决这个问题,请将这两行移至__init__(每帧都称为):

TargetCircle.draw