moving circles together in pygame

323 views Asked by At

I have been working on this project for too long. I am clueless at this point. I am a complete noobie at python. i just started learning python, and im supposed to make a drawing. I just want to give up at this point, since basically nobody wants to help me. This is what the project is actually supposed to look like:

from turtle import Screen, Turtle

listCircleColors = ['red', 'blue', 'green', 'orange', 'yellow', 'purple', 'white']

intGroup = 5
angleLeft = 360 / (intGroup * len(listCircleColors))

def moveAndDraw(turtle):
    turtle.left(90)
    turtle.penup()
    turtle.forward(15)
    turtle.pendown()
    turtle.right(90)
    turtle.circle(100)

def moveSameColorCircle():
    for color in listCircleColors:
        for _ in range(12):
            for turtle in listCircleObjects:
                if turtle.pencolor() == color:
                    turtle.undo()
                    moveAndDraw(turtle)

            screen.update()

screen = Screen()
screen.setup(900, 900)
screen.bgcolor('black')
screen.tracer(False)

headAngle = 0
listCircleObjects = list()

for _ in range(intGroup):
    for color in listCircleColors:
         turtle = Turtle()
        turtle.hideturtle()
        turtle.setheading(headAngle)
        turtle.color(color)
        turtle.pensize(2)
        turtle.circle(100)
     headAngle += angleLeft

    listCircleObjects.append(turtle)

screen.update()

screen.ontimer(moveSameColorCircle, 500)
screen.exitonclick()

basically, im supposed to draw a spirograph and then move them towards the sides of the screen by its color, but in a group, beginning by moving all the red circles to the sides of the screen, then green, then blue, etc. I want to do the exact same thing, but using pygame. So far, i am able to draw a the spirograph and move it, just not one color group at a time, but the whole spirograph moving to the right. I've read so many stack overflow posts about moving multiple objects and creating different screens, but i still cant seem to figure out how to move the circles by its color group. This is my current code:

import pygame
import math
import sys
import time
#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)
circles = []
circles_rect = []
#draw

#draw
alpha = turnangle
for i in range(intGroup):
    for cl in listCircleColor:
        surfacetemp = pygame.Surface((width, height))
        surfacetemp = surfacetemp.convert_alpha()
        surfacetemp.fill((0, 0, 0, 0))

        ##circlerect = pygame.rect
    if alpha > 0 and alpha < 90:
        circlerect = pygame.draw.circle(surfacetemp, 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:
        circlerect = pygame.draw.circle(surfacetemp, 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:
        circlerect = pygame.draw.circle(surfacetemp, 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:
        circlerect = pygame.draw.circle(surfacetemp, cl, (300 + radius * math.cos(math.radians(360 - alpha)), 300 - radius * math.sin(math.radians(360 - alpha))), radius, width=2)

    alpha = alpha + turnangle
    ##circles.append(circlerect)
    circles.append(surfacetemp)
    circles_rect.append(surfacetemp.get_rect())

# move"

#exit only when user clicks on exit button
clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


    screen.fill((0, 0, 0))
    for crect, ret in zip(circles, circles_rect):
        ret.right += 5
        ret.left += 5
        screen.blit(crect, ret)


pygame.display.update()
clock.tick(20)

pygame.quit()
exit()

also, for some reason, when i run this code, nothing appears, only a dark screen. I dont know what i did wrong, but before writing this i made sure that everything was working. Thanks for any help.

2

There are 2 answers

1
Rabbid76 On BEST ANSWER

It is a matter of Indentation. The display needs to be updated in the application loop:

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


    screen.fill((0, 0, 0))
    for crect, ret in zip(circles, circles_rect):
        ret.right += 5
        ret.left += 5
        screen.blit(crect, ret)

# INDENTATION
#-->|

    pygame.display.update()
    clock.tick(20)

pygame.quit()
exit()

If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visibel, when the display is updated with either pygame.display.update() or pygame.display.flip().


Complete and tested code:

import pygame
import math
import sys
import time
#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)
circles = []
circles_rect = []
#draw

#draw
alpha = turnangle
for i in range(intGroup):
    for cl in listCircleColor:
        surfacetemp = pygame.Surface((width, height))
        surfacetemp = surfacetemp.convert_alpha()
        surfacetemp.fill((0, 0, 0, 0))

        ##circlerect = pygame.rect
    if alpha > 0 and alpha < 90:
        circlerect = pygame.draw.circle(surfacetemp, 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:
        circlerect = pygame.draw.circle(surfacetemp, 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:
        circlerect = pygame.draw.circle(surfacetemp, 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:
        circlerect = pygame.draw.circle(surfacetemp, cl, (300 + radius * math.cos(math.radians(360 - alpha)), 300 - radius * math.sin(math.radians(360 - alpha))), radius, width=2)

    alpha = alpha + turnangle
    ##circles.append(circlerect)
    circles.append(surfacetemp)
    circles_rect.append(surfacetemp.get_rect())

# move"

#exit only when user clicks on exit button
clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


    screen.fill((0, 0, 0))
    for crect, ret in zip(circles, circles_rect):
        ret.right += 5
        ret.left += 5
        screen.blit(crect, ret)


    pygame.display.update()
    clock.tick(20)

pygame.quit()
exit()
1
Adid On

In order to display the changes made to the screen, add pygame.display.flip() to the end of your game loop.
Changes are only displayed after you flip.
Further documentation here