Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
B

Bryan Carvalho

@Bryan Carvalho
About
Posts
1
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Python - Pygame - Fill screen with triangles without gaps
    B Bryan Carvalho

    Generating a screen full of random triangles that completely fill the screen with no gaps can be quite challenging. One approach to solve this problem is to use a recursive algorithm that divides the screen into triangles of random sizes and positions. Here's one way you could implement such an algorithm using Pygame:

    import pygame
    import random

    Set up the Pygame window

    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("Random Triangles")

    Define the function to recursively draw triangles

    def draw_triangles(x, y, width, height, depth):
    if depth == 0:
    return

    # Generate three random points within the bounds of the rectangle
    x1 = random.randint(x, x + width)
    y1 = random.randint(y, y + height)
    x2 = random.randint(x, x + width)
    y2 = random.randint(y, y + height)
    x3 = random.randint(x, x + width)
    y3 = random.randint(y, y + height)
    
    # Draw the triangle
    color = (255, 255, 255)  # white
    pygame.draw.polygon(screen, color, \[(x1, y1), (x2, y2), (x3, y3)\])
    
    # Recursively draw triangles in the three sub-rectangles
    draw\_triangles(x, y, width // 2, height // 2, depth - 1)
    draw\_triangles(x + width // 2, y, width // 2, height // 2, depth - 1)
    draw\_triangles(x, y + height // 2, width // 2, height // 2, depth - 1)
    

    Draw the triangles

    draw_triangles(0, 0, 800, 600, 5)

    Update the display

    pygame.display.flip()

    Wait for the user to close the window

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

    In this example, we define a recursive function draw_triangles() that takes as input the position and dimensions of a rectangle, as well as a depth value that determines how many levels of recursion to use. The function generates three random points within the rectangle and uses pygame.draw.polygon() to draw a white triangle using those points. It then recursively calls itself on the three sub-rectangles that result from dividing the original rectangle in half horizontally and vertically.

    The depth parameter controls the level of detail in the final image. You can adjust this value to get more or fewer triangles in the final image. You can also modify the colors of the triangles or the size of the window to suit your needs.

    I hope this helps you get started on your project!

    Python
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups