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

标签 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()

最佳答案

问题是您没有更新 TargetCircle 的 x 和 y 值。你设置

        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 (每帧调用):

class TargetCircle(object):
    def __init__(self, posX, posY):
        self.circlePositionX = posX
        self.circlePositionY = posY
        self.radius = 38
        self.theta = 0

    def draw(self, win):
        self.x = int((self.circlePositionX + (self.radius * math.cos(self.theta))))
        self.y = int((self.circlePositionY + (self.radius * math.sin(self.theta))))
        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)

关于python - 我在使用 pygame 显示表面变化时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60716380/

相关文章:

python - pygame.错误 : no video mode has been set

python - "Fatal Python error: (pygame parachute) Segmentation Fault"改变窗口时

python - 解决错误TypeError : __init__() takes 1 positional argument but 2 were given

Python SDK Azure 计算机视觉 : 'bytes' object has no attribute 'read'

python - 执行 pandas groupby 操作的更快替代方案

python - pygame 中的碰撞不起作用(对象发生碰撞但代码未触发)

python - 重置 time.clock()

Python pygame.transform.rotate

Python - 从不同的起始行读取同一文件

python - matplotlib:将小时字符串转换为 float