pythonpygamepygame2

Pygame moving circles


im new to python and im working on moving circles in pygame. Im trying to put the circles in a group and move them. I am stuck. Right now, im trying to put the circles into a list, but i keep getting a error. I've been working on this project for too long, and Im about to give up. Any help would be appreciated

import pygame
import math
import sys
#setting colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255,  0)
RED = (255, 0, 0)
ORANGE = (255, 127, 0)
YELLOW = (255, 255, 0)
PURPLE = (160, 32, 240)
#setting what order the colors go in
listCircleColor = (RED, BLUE, GREEN, ORANGE, YELLOW, PURPLE, WHITE)
#how many circles per color
 intGroup = 5
#the space between each circle
turnangle = 360/35
#width of screen
width = 600
#height of screen
height = 600
#radius of circles
radius = 100
#making the screen
screen = pygame.display.set_mode((width, height))
#if the code is running, then continue
running = True


##.draw.circle(screen, BLUE, (0, 0), radius, width=2)
alpha = turnangle
circles = []

#draw
alpha = turnangle
for i in range(intGroup):
    for cl in listCircleColor:
        if alpha > 0 and alpha < 90:
            pygame.draw.circle(screen, cl, (300 + radius * math.cos(math.radians(alpha)), 300 + radius * math.sin(math.radians(alpha))), radius, width=2)
            # second quarter of circles
        if alpha > 90 and alpha < 180:
            pygame.draw.circle(screen, cl, (300 - radius * math.cos(math.radians(180 - alpha)), 300 + radius * math.sin(math.radians(180 - alpha))), radius, width=2)
            # third quarter of circles
        if alpha > 180 and alpha < 270:
            pygame.draw.circle(screen, cl, (300 - radius * math.cos(math.radians(270 - alpha)), 300 - radius * math.sin(math.radians(270 - alpha))), radius, width=2)
            # last quarter of circles
        if alpha > 270 and alpha < 360:
            pygame.draw.circle(screen, cl, (300 + radius * math.cos(math.radians(360 - alpha)), 300 - radius * math.sin(math.radians(360 - alpha))), radius, width=2)
        alpha = alpha + turnangle
        circle = [pygame.draw.circle(screen, cl, (300 + radius * math.cos(math.radians(alpha)), 300 + radius * math.sin(math.radians(alpha))), radius, width=2)]
        circles={'circles': circle.get_rect()}


#move"
"""
circlesToMove = []

finishedMovingAllFive = False
for circle in circlesToMove:
                circle[1] += circle[3][0] * speed
                circle[2] += circle[3][1] * speed
    


"""
for circle in circles:
    circles[circle].right+=1
pygame.time.Clock().tick(40)
#updating display
pygame.display.flip()
#exit only when user clicks on exit button
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

Solution

  • pygame.draw.circle doesn't create an object. pygame.draw.circle draws a circle on the screen (on a surface) and recturns a pygame.Rect object with the bounding box of the circle.

    You must create a list with the data of the circles. Each element in the list has to contain the center point and the color of the circle

    circles = []
    alpha = turnangle
    for i in range(intGroup):
        for cl in listCircleColor:
            x = 300 + radius * math.cos(math.radians(alpha))
            y = 300 + radius * math.sin(math.radians(alpha))
            circles.append(([x, y], cl))
            alpha = alpha + turnangle
    

    Move and draw the circles in the application loop with:

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
    
        for circle in circles:
            circle[0][0] += 1
       
        screen.fill(0)
        for center, color in circles:
            pygame.draw.circle(screen, color, center, radius, 2)
        pygame.display.flip()
        pygame.time.Clock().tick(40)