Search code examples
pythonpygame

Problem with coordinates of rhombus in pygame


I am working on a small game in Pygame. I am aware that Kivy might be a better option for game development in Python, but I am still in the process of learning object-oriented programming (OOP).

This game will feature a ball and a stack of rhombuses piled on top of each other to create the illusion of a cuboid. The topmost plate can only move along the x-axis, controlled by arrow keys. I manually adjusted the y-coordinates of the top plate to make it appear as if it's on the top, but I'm having difficulty making it look like it's in the foreground rather than the background.

[game}1

I will give you a output of coordinates of top plate mooving with keys press:

This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(175, 364), (355, 364), (305, 414), (125, 414)\]
This is are a points of firts rhombus\[(180, 364), (360, 364), (310, 414), (130, 414)\]
This is are a points of firts rhombus\[(185, 364), (365, 364), (315, 414), (135, 414)\]
This is are a points of firts rhombus\[(190, 364), (370, 364), (320, 414), (140, 414)\]
This is are a points of firts rhombus\[(195, 364), (375, 364), (325, 414), (145, 414)\]
This is are a points of firts rhombus\[(200, 364), (380, 364), (330, 414), (150, 414)\]
This is are a points of firts rhombus\[(205, 364), (385, 364), (335, 414), (155, 414)\]
This is are a points of firts rhombus\[(210, 364), (390, 364), (340, 414), (160, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(220, 364), (400, 364), (350, 414), (170, 414)\]
This is are a points of firts rhombus\[(225, 364), (405, 364), (355, 414), (175, 414)\]
This is are a points of firts rhombus\[(230, 364), (410, 364), (360, 414), (180, 414)\]
This is are a points of firts rhombus\[(235, 364), (415, 364), (365, 414), (185, 414)\]
This is are a points of firts rhombus\[(240, 364), (420, 364), (370, 414), (190, 414)\]
This is are a points of firts rhombus\[(245, 364), (425, 364), (375, 414), (195, 414)\]
This is are a points of firts rhombus\[(250, 364), (430, 364), (380, 414), (200, 414)\]
This is are a points of firts rhombus\[(255, 364), (435, 364), (385, 414), (205, 414)\]
This is are a points of firts rhombus\[(260, 364), (440, 364), (390, 414), (210, 414)\]
This is are a points of firts rhombus\[(260, 364), (440, 364), (390, 414), (210, 414)\]
This is are a points of firts rhombus\[(260, 364), (440, 364), (390, 414), (210, 414)\]
This is are a points of firts rhombus\[(255, 364), (435, 364), (385, 414), (205, 414)\]
This is are a points of firts rhombus\[(250, 364), (430, 364), (380, 414), (200, 414)\]
This is are a points of firts rhombus\[(245, 364), (425, 364), (375, 414), (195, 414)\]
This is are a points of firts rhombus\[(240, 364), (420, 364), (370, 414), (190, 414)\]
This is are a points of firts rhombus\[(235, 364), (415, 364), (365, 414), (185, 414)\]
This is are a points of firts rhombus\[(230, 364), (410, 364), (360, 414), (180, 414)\]
This is are a points of firts rhombus\[(225, 364), (405, 364), (355, 414), (175, 414)\]
This is are a points of firts rhombus\[(220, 364), (400, 364), (350, 414), (170, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(210, 364), (390, 364), (340, 414), (160, 414)\]
This is are a points of firts rhombus\[(205, 364), (385, 364), (335, 414), (155, 414)\]
This is are a points of firts rhombus\[(200, 364), (380, 364), (330, 414), (150, 414)\]
This is are a points of firts rhombus\[(195, 364), (375, 364), (325, 414), (145, 414)\]
This is are a points of firts rhombus\[(190, 364), (370, 364), (320, 414), (140, 414)\]
This is are a points of firts rhombus\[(185, 364), (365, 364), (315, 414), (135, 414)\]
This is are a points of firts rhombus\[(180, 364), (360, 364), (310, 414), (130, 414)\]
This is are a points of firts rhombus\[(175, 364), (355, 364), (305, 414), (125, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(165, 364), (345, 364), (295, 414), (115, 414)\]
This is are a points of firts rhombus\[(160, 364), (340, 364), (290, 414), (110, 414)\]
This is are a points of firts rhombus\[(155, 364), (335, 364), (285, 414), (105, 414)\]
This is are a points of firts rhombus\[(150, 364), (330, 364), (280, 414), (100, 414)\]
This is are a points of firts rhombus\[(145, 364), (325, 364), (275, 414), (95, 414)\]
This is are a points of firts rhombus\[(140, 364), (320, 364), (270, 414), (90, 414)\]
This is are a points of firts rhombus\[(135, 364), (315, 364), (265, 414), (85, 414)\]
This is are a points of firts rhombus\[(130, 364), (310, 364), (260, 414), (80, 414)\]
This is are a points of firts rhombus\[(125, 364), (305, 364), (255, 414), (75, 414)\]
This is are a points of firts rhombus\[(125, 364), (305, 364), (255, 414), (75, 414)\]
This is are a points of firts rhombus\[(125, 364), (305, 364), (255, 414), (75, 414)\]
This is are a points of firts rhombus\[(125, 364), (305, 364), (255, 414), (75, 414)\]
This is are a points of firts rhombus\[(130, 364), (310, 364), (260, 414), (80, 414)\]
This is are a points of firts rhombus\[(135, 364), (315, 364), (265, 414), (85, 414)\]
This is are a points of firts rhombus\[(140, 364), (320, 364), (270, 414), (90, 414)\]
This is are a points of firts rhombus\[(145, 364), (325, 364), (275, 414), (95, 414)\]
This is are a points of firts rhombus\[(150, 364), (330, 364), (280, 414), (100, 414)\]
This is are a points of firts rhombus\[(155, 364), (335, 364), (285, 414), (105, 414)\]
This is are a points of firts rhombus\[(160, 364), (340, 364), (290, 414), (110, 414)\]
This is are a points of firts rhombus\[(165, 364), (345, 364), (295, 414), (115, 414)\]
This is are a points of firts rhombus\[(170, 364), (350, 364), (300, 414), (120, 414)\]
This is are a points of firts rhombus\[(175, 364), (355, 364), (305, 414), (125, 414)\]
This is are a points of firts rhombus\[(180, 364), (360, 364), (310, 414), (130, 414)\]
This is are a points of firts rhombus\[(185, 364), (365, 364), (315, 414), (135, 414)\]
This is are a points of firts rhombus\[(190, 364), (370, 364), (320, 414), (140, 414)\]
This is are a points of firts rhombus\[(195, 364), (375, 364), (325, 414), (145, 414)\]
This is are a points of firts rhombus\[(200, 364), (380, 364), (330, 414), (150, 414)\]
This is are a points of firts rhombus\[(205, 364), (385, 364), (335, 414), (155, 414)\]
This is are a points of firts rhombus\[(210, 364), (390, 364), (340, 414), (160, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]
This is are a points of firts rhombus\[(215, 364), (395, 364), (345, 414), (165, 414)\]

My code here:

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 rhombus(screen, color, space):
    point1 = (170, 760 - space)
    point2 = (350, 760 - space)
    point3 = (300, 810 - space)
    point4 = (120, 810 - space)
    points = [point1, point2, point3, point4]
    #print(points)
    return pg.draw.polygon(screen, color, points)
def first_rhombus(screen, color, x_coor):
    point1 = (170-x_coor, 364 )
    point2 = (350-x_coor, 364 )
    point3 = (300-x_coor, 414 )
    point4 = (120-x_coor, 414 )
    points = [point1, point2, point3, point4]
    print(f"This is are a points of firts rhombus{points}")
    return pg.draw.polygon(screen, color, points)

def colors():
    collist = []
    old_col = None
    for trash in range(0, 43):
        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, space,x_coor):
    positions = []
    for x in range(0, 43):
        if x == 0:
            position = first_rhombus(screen, collist[x], x_coor)
        else:
            position = rhombus(screen, collist[x], space)
        positions.append(position)
        space += 8
    return positions

def move_rhumbus(x_coor, keys):
    if keys[pg.K_LEFT]:
        if x_coor > -500:
            x_coor -= 5
    if keys[pg.K_RIGHT]:
        if x_coor < 500:
            x_coor += 5
    return x_coor

#Ball functions
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 check_collision(space, index):
    end = False
    #print(f"This is a plus in check_collision: {space}")
    #print(f"This is a index in check_collision: {index} ")
    if space > 0 and space< 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

# Main game loop
done = False
colorlist = colors()
ballcol = ball_color(colorlist)
ball_coor_y = 300
constant_of_px = 440
end = False
restart = False
x_coor = 0
space = 0

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

    keys = pg.key.get_pressed()
    
    x_coor = move_rhumbus(x_coor, keys)
    screen.fill(BG_COLOR)
    
    # Draw rhombus and store their positions
    rhombus_positions = cube(colorlist, space,x_coor)

    # Check for collisions
    for i, position in enumerate(rhombus_positions):
        end = check_collision(space, i)
        if end:
            break

    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[i]:
            end = True 

        if ball_coor_y == 300:
            constant_of_px = 440
    #This is a bg after game over but in dev it is banned so please remove """
    """
    if end:
        screen.blit(bg, (0, 0))
    """
    pg.display.flip()
    clock.tick(30)

Solution

  • Do not use a separate draw function for the first Rhombus, but use a function that can draw all the Rhomboids:

    def rhombus(screen, color, x_coor, space):
        point1 = (170 + x_coor, 740 - space)
        point2 = (350 + x_coor, 740 - space)
        point3 = (300 + x_coor, 790 - space)
        point4 = (120 + x_coor, 790 - space)
        points = [point1, point2, point3, point4]
        return pg.draw.polygon(screen, color, points)
    

    Draw the Rhomboids from bottom to top:

    def cube(collist, space, x_coor):
        positions = []
        for i, color in enumerate(collist):
            x = x_coor if i == len(collist)-1 else 0 
            position = rhombus(screen, color, x, space + i * 8)
            positions.append(position)
        return positions