lundi 29 novembre 2021

How to put circles on multiple screens to move them

I'm working on a code that draws a spirograph and moves circles depending on its color towards the sides of the screen. What I mean is, for example, there are 7 colors starting from white, blue, green, etc. I want to move all the circles that are white to the sides of the screen together, every white circle moving at once. Then every blue circle to the sides of the screen together, then, etc. This is what I mean:

import turtle
import random
import math
listCircleColor = ("red", "blue", "green", "orange", "yellow", "purple", "white")
listCircleObjects = list()

intGroup = 5
angleleft = 360/(intGroup*len(listCircleColor))
# make screen object
# and set size
sc = turtle.Screen()
sc.bgcolor("black")
sc.tracer(5)
# make turlte object
turtle.hideturtle()
turtle.speed(0)

def moveanddraw(oneturtle):
   oneturtle.left(90)
   oneturtle.penup()
   oneturtle.forward(20)
   oneturtle.right(90)
   oneturtle.pendown()
   oneturtle.circle(100)

def movesamecolorcircle():
   for cls in listCircleColor:
      for tur in listCircleObjects:
         objcrlcolor = tur.color()
         if objcrlcolor == (cls, cls):
            tur.undo()
            for i in range(1, 6):
               moveanddraw(tur)
               if i < 5:
                  tur.undo()

headangle = 0
for i in range(intGroup):
   for cl in listCircleColor:
      tur = turtle.Turtle()
      tur.hideturtle()
      tur.setheading(headangle)
      tur.color(cl)
      tur.pencolor(cl)
      tur.speed(0)
      tur.pensize(2)
      tur.circle(100)
      headangle = headangle + angleleft
      listCircleObjects.append(tur)

sc.ontimer(movesamecolorcircle, 50)


 print(len(listCircleObjects))
 sc.exitonclick()

This is what I want, except that all the circles are moving together, as you can see that they don't move together in the code. So instead of using turtle, I used pygame to try to make the effect of circles moving together better. 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
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        screen.fill((0, 0, 0))
        for crect, ret in zip(circles, circles_rect):
            ret.right += 5
            ret.left += 5
            screen.blit(crect, ret)

        screen.blit(crect, ret)




##screen.blit(crect,crect)
pygame.time.Clock().tick(20)
pygame.display.update()

##for center, color in circles:
##    pygame.draw.circle(screen, color, center, radius, 2)
##pygame.display.flip()

I've been told to put each circle into multiple screens and then move them, but I have no idea how to move them simultaneously. Please help me.




Aucun commentaire:

Enregistrer un commentaire