Search code examples
pythonpygamepygame2

How to create a rhomboid in pygame


I want to create a rhomboid like this. I can not do this I found the solution just for the diamond.

image

my code:

import pygame as pg
pg.init()
screen = pg.display.set_mode((500, 700))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
BLUE = pg.Color('dodgerblue')
points = [(200, 200), (250, 250), (200, 300), (150, 250)]
done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    screen.fill(BG_COLOR)
    pg.draw.polygon(screen, BLUE, points)
    pg.display.flip()
    clock.tick()

Solution

  • Write a function that draws a rhomboid with a with, height and offset:

    import pygame as pg
    
    def drawRhomboid(surf, color, x, y, width, height, offset, thickness=0):
        points = [
            (x + offset, y), 
            (x + width + offset, y), 
            (x + width, y + height), 
            (x, y + height)]
        pg.draw.polygon(surf, color, points, thickness)
    
    pg.init()
    screen = pg.display.set_mode((500, 700))
    clock = pg.time.Clock()
    BG_COLOR = pg.Color('gray12')
    BLUE = pg.Color('dodgerblue')
    
    done = False
    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
        screen.fill(BG_COLOR)
        drawRhomboid(screen, BLUE, 50, 50, 300, 200, 100, 3)
        pg.display.flip()
        clock.tick()
    


    If you want to draw an isometric cube, you just need to calculate the correct points and stick the cube from Rhomboids:

    import pygame, math
    
    def drawCube(surf, center, size, angle):
        v = pygame.math.Vector2(1, 0)
        v.rotate_ip(angle + 45)
        lx = round(v.x * size * math.sqrt(2) / 2)
        ly = round(v.y * size * math.sqrt(2) / 2)
        x, y = center
        s = size/2
        f1 = [(x+lx, y-s+ly//2), (x-ly, y-s+lx//2), (x-lx, y-s-ly//2), (x+ly, y-s-lx//2)]
        pts = [(lx, ly//2), (-ly, lx//2), (-lx, -ly//2), (ly, -lx//2)]
        faces = []
        for i in range(4):
            p0, p1 = pts[i], pts[(i+1) % 4]
            f = [(p0[0]+x, p0[1]+y-s), (p1[0]+x, p1[1]+y-s), (p1[0]+x, p1[1]+y+s), (p0[0]+x, p0[1]+y+s)]
            faces.append(f)
        colors = ["red", "yellow", "green", "blue"]
        for face, color in zip(faces, colors):
            if (face[0][1] + face[1][1]) / 2 > y-s:
                pygame.draw.polygon(surf, color, face, 3)
        pygame.draw.polygon(surf, "white", f1, 3)
    
    pygame.init()
    window = pygame.display.set_mode((300, 300))
    clock = pygame.time.Clock()
    
    angle = 0
    run = True
    while run:
        clock.tick(100)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False 
    
        window_center = window.get_rect().center
    
        window.fill(0)
        drawCube(window, window_center, 150, angle)
        pygame.display.flip()
    
        angle += 1
    
    pygame.quit()
    exit()