I'm having an issue displaying changes to the surface using pygame

Viewed 36

My current goal is to make a smaller circle rotate around the center of a larger circle and have the rectangles follow the path of the smaller circle.

I have created a TargetCircle class because there will be a total of 18 circles when this visualization is finished. My issue is that after changing the theta value for each circle and trying to display the changes, nothing happens.

Currently, all that gets displayed is the 9 circles across the top and the horizontal and vertical lines, however, everything remains stationary. Any help would be much appreciated. Thank you.

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 Answers

The issue is that you're not updating the x and y values of the TargetCircle. You set

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

once in the __init__ (which is called once at the start), and then it is never set again. To remedy this, move those two lines into TargetCircle.draw (which is called every frame):

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)
Related