Estoy teniendo un problema que muestra los cambios en el uso de la superficie de pygame

NickP:

Mi objetivo actual es hacer una pequeña rotación círculo alrededor del centro de un círculo más grandes y tienen los rectángulos siguen el camino del círculo más pequeño.

He creado una TargetCircleclase, ya que habrá un total de 18 círculos cuando se termine esta visualización. Mi problema es que después de cambiar el thetavalor de cada círculo y tratar de mostrar los cambios, no pasa nada.

Actualmente, lo único que consigue es representada los 9 círculos en la parte superior y las líneas horizontales y verticales, sin embargo, todo permanece inmóvil. Cualquier ayuda sería muy apreciada. Gracias.

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()
Ken Hilton:

La cuestión es que no se va a actualizar los valores de x e y de la TargetCircle. Configura

        self.x = int((self.circlePositionX + (self.radius * math.cos(self.theta))))
        self.y = int((self.circlePositionY + (self.radius * math.sin(self.theta))))

una vez en el __init__(que se llama una vez al comienzo), y entonces nunca se ajusta de nuevo. Para remediar esto, mover esas dos líneas en TargetCircle.draw(que se llama cada trama):

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)

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=351516&siteId=1
Recomendado
Clasificación