How to fix pygame blinking gemetric shape

Question:

Hi I want to make a game with rhombus what I want to fix is that I set the clock to XY and in while true cycles I have function with one built me a cube
image

I want to create a game with levels on each level a cube will be made again with another random variation of colors. I have not levels method created yet so I want to create a cube with some colors and after a restarting code, It should be another color variation. But I can not fix a blinking.

my code:

import pygame as pg
import random
pg.init()
screen = pg.display.set_mode((500, 800))
clock = pg.time.Clock()
BG_COLOR = pg.Color('black')
WHITE = pg.Color('white')
ORANGE = pg.Color('orange')
BLUE = pg.Color('blue')
RED = pg.Color('red')
PURPLE = pg.Color('purple')
m = 8
def rhumbus(screen,color,plus):
    point1=(170, 500-plus)
    point2=(350, 500-plus)
    point3=(300, 550-plus)
    point4=(120, 550-plus)
    points=[point1, point2, point3, point4]
    return pg.draw.polygon(screen,color, points)

def cube(rando):
    plus = -72
    old_col = None
    for x in range(0,10):
        rando = random.choice([RED,ORANGE,RED,BLUE,WHITE])
        if rando != old_col:

            rhumbus(screen,rando,plus)
            plus += 8
            old_col = rando
        else:
            rhumbus(screen,PURPLE,plus)
            plus += 8
            old_col = rando

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    
    screen.fill(BG_COLOR)
    cube(m)
    pg.display.flip()
    clock.tick(10)
 
Asked By: ALex Break

||

Answers:

Instead of choosing a random sequence of colors on each frame, pick them beforehand.

  • The new make_random_sequence function returns a list chosen randomly from the given sequence, making sure there are no consecutive repeats of the same item (which seemed to be your logic with old_col too?)
  • You were not actually using the rando parameter given to cube.
  • Using enumerate to loop over the colors and their indices is shorter than dealing with a plus variable.
import pygame as pg
import random

pg.init()
screen = pg.display.set_mode((500, 800))
clock = pg.time.Clock()
BG_COLOR = pg.Color('black')
WHITE = pg.Color('white')
ORANGE = pg.Color('orange')
BLUE = pg.Color('blue')
RED = pg.Color('red')
PURPLE = pg.Color('purple')


def rhumbus(screen, color, plus):
    point1 = (170, 500 - plus)
    point2 = (350, 500 - plus)
    point3 = (300, 550 - plus)
    point4 = (120, 550 - plus)
    points = [point1, point2, point3, point4]
    return pg.draw.polygon(screen, color, points)


def make_random_sequence(options, n):
    seq = []
    while len(seq) < n:
        choice = random.choice(options)
        if seq and choice == seq[-1]:  # skip if same as last
            continue
        seq.append(choice)
    return seq


def cube(colors):
    for i, color in enumerate(colors):
        rhumbus(screen, color, -72 + i * 8)


colors = make_random_sequence([RED, ORANGE, BLUE, WHITE], 8)

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    screen.fill(BG_COLOR)
    cube(colors)
    pg.display.flip()
    clock.tick(10)
Answered By: AKX
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.