0

So im new to python, and im working on a code. The code is supposed to make a spirograph move and im almost finished and at the last step, but when i ran it, the screen doesnt refresh even though im sure that the circles are moving by printing out the coordinates. I really dont know whats going on because im sure i spelt everything right. Any help?

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 = []

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

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

#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()


    for crect in circles:
        ret = crect.get_rect()
        ret.right += 5
        ret.left += 5

        screen.blit(screen, 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()
Ven
  • 31
  • 5
  • Please fix up the code first, the indentation levels are wrong. For example, the line with pygame.quit() is at the same level as the preceding if. At any rate, it looks as if you aren't drawing anything. – Passerby Oct 30 '21 at 04:27

2 Answers2

0

There is so much code having wrong indentations:

if alpha > 0 and alpha < 90:
            circlerect = pygame.draw.circle(screen, cl, (300 + radius * 
math.cos(math.radians(alpha)), 300 + radius * math.sin(math.radians(alpha))), radius, width=2)

and

if alpha > 90 and alpha < 180:
            circlerect = pygame.draw.circle(screen, cl, (300 - radius * 
math.cos(math.radians(180 - alpha)), 300 + radius * math.sin(math.radians(180 - alpha))), 
radius, width=2)

and

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

and

if event.type == pygame.QUIT:
    pygame.quit()
    exit()

Fix the indentations in these parts so that we can see where the problem is.

0

There are many problems within the code, primarily as indentations, as mentioned by Dhrumil Dave, but to fix the display not updating, here is what you need to do. Instead of this

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

"for circle in circles:"
"print(circle[0][0])"
"print(circle[0][1])"
"time.sleep(50)"
"circle[0][0] += 1"

for crect in circles:
    crect.right += 20

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

You need to move the pygame.display.update() to within the while running loop, like this

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
        pygame.quit()
        exit()
    pygame.display.update()

I would also look in to implementing a clock to standardize frame rate, like this...

FPS = 60
clock = pygame.time.Clock()
running = True
while running:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
        pygame.quit()
        exit()
    pygame.display.update()

If you do this, the game will only update 60 times per second(the while loop will only run through 60 times per second). This will standardize speed of many actions and help significantly once the game is more complete. You can also look into something called delta time, which you can see how to do here.

  • Thanks! I tried it but the screen still doesnt refresh. What should i do? – Ven Oct 30 '21 at 21:11
  • When you run the program, do the initial circles draw onto the screen? –  Oct 31 '21 at 13:44
  • Yes, but they dont move – Ven Nov 01 '21 at 00:40
  • try moving the draw rect commands to within the while running loop, so it will draw each time –  Nov 01 '21 at 10:43
  • Okay. Also, I made changes to the code. When I tried moving the rect commands to the while running, it just gives me a blank screen – Ven Nov 03 '21 at 02:21
  • can you please post your updated code at the bottom of your question? –  Nov 04 '21 at 16:23