A problem with not seeable cube in pygame

Question:

I can not see a cube made from rhombuses. I opened this script after a few months and It is not working. I do not remember that in the past after the last chance, my code id had worked or not. it is a complex script. So sorry for the whole script. I have not any details about the error, because I do not have any type of error message.

my code:

import pygame as pg
import random
import time
pg.init()

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')
BALL_COOR_X = 240

screen = pg.display.set_mode((500, 800))
clock = pg.time.Clock()


bg = pg.image.load("bg.png")


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 colors():
    collist = []
    old_col = None
    for x in range(0,10):
        col = random.choice([RED,ORANGE,RED,BLUE,WHITE])
        if col != old_col:

            collist.append(col)
            old_col = col
        else:
            collist.append(PURPLE)
            old_col = col


    return collist

def cube(collist, plus):
    index = -1
    for x in range(0,10):
        rhumbus(screen,collist[x],plus)
        if ball_coor_y >= 500-plus and ball_coor_y <= 550-plus and BALL_COOR_X >= 120+plus and BALL_COOR_X <= 300+plus:
            index = x
            break
    return index
            
def ball(color,x,y):
    pg.draw.circle(screen, color, (x,y),15,)

def plus(y):
    y = y +1
    return y

def minus(y):
    y = y -1
    return y

def move_rhumbus(plus, keys):
    if keys[pg.K_LEFT]:
        if plus > 0:
            plus -= 5
    if keys[pg.K_RIGHT]:
        if plus < 500:
            plus += 5
    if event.type == pg.FINGERDOWN:
        start_x = event.x
    if event.type == pg.FINGERUP:
        if event.x > start_x:
            if plus < 500:
                plus += 5
        else:
            if plus > 0:
                plus -= 5
    return plus


def check_collision(plus, index):
    end = False
    if plus > 0 and plus < 500:
        if colorlist[ballcol] != colorlist[index]:
            end = True 
    return end

def ball_color(colorlist,):
    li = [0,1,2,3,4]
    ballcol = random.choice(li)
    return ballcol



# this is a just a 1 level 


done = False
colorlist = colors()
ballcol = ball_color(colorlist)
ball_coor_y = 300
constant_of_px = 516
end = False
restart = False
plus_x = 0
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        
    keys = pg.key.get_pressed()
    #plus_x = move_rhumbus(plus_x , keys)
    print(f"This is a plus_x {plus_x}")
    index = cube(colorlist, plus_x)
    end = check_collision(plus_x , index)
    screen.fill(BG_COLOR)
    ball(colorlist[ballcol],BALL_COOR_X,ball_coor_y)


    
    # ball movement 
    if ball_coor_y < constant_of_px:
        ball_coor_y = plus(ball_coor_y)
        

    else:

        ball_coor_y = minus(ball_coor_y)
        constant_of_px = 0
        if colorlist[ballcol] != colorlist[index]:
            end = True 

        if ball_coor_y == 300:
            constant_of_px = 516
    
    
    #if end == True:
        #screen.blit(bg,(0,0))
        
    
    pg.display.flip()
    clock.tick(60)

my error is not just I can not see the cube

Asked By: Test000

||

Answers:

You must clear the background before drawing the cube, but not after drawing the cube:

while not done:
    # handle the events
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        
    # update the objects
    keys = pg.key.get_pressed()

    # [...]

    
    # clear the display
    screen.fill(BG_COLOR)
    
    # draw the scene
    index = cube(colorlist, plus_x)
    end = check_collision(plus_x , index)
    ball(colorlist[ballcol],BALL_COOR_X,ball_coor_y)

    # update the display 
    pg.display.flip()
    # limit the frames per second 
    clock.tick(60)

The typical PyGame application loop has to:


Also the position of all Rhomboids is the same. You need to change
the position in the loop where you draw them. e.g.:

for x in range(0,10):
    rhumbus(screen, collist[x], plus + x * 5)

However, if you want to create a three-dimensional cube, I suggest you read the answer to the following question:

How to create a rhomboid in pygame

How to create a rhomboid in pygame

Answered By: Rabbid76

The issue is that the background need to be cleared:

while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        
    keys = pg.key.get_pressed()
    
    screen.fill(BG_COLOR)
    
    index = cube(colorlist, plus_x)
    end = check_collision(plus_x , index)
    
    ball(colorlist[ballcol],BALL_COOR_X,ball_coor_y)
    
    pg.display.flip()
    clock.tick(60)

To ensure that the ball is always visible on top of the cube draw the ball before updating the display.
And the clock.tick(60) call limits the frame rate to 60 FPS to reduce CPU usage.

Answered By: 0xPacman
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.