2

I'm making a little game in order to learn python. It's a game where you control a red point who wants to get out of a dungeon. I'm using pygame for the graphic interface.

I'm creating a 2D map generator compose of rectangles but i think i'm using the bad method: i create rectangles after rectangles and i need to set all caracteristics "one by one".

Even if this method teach me a lot of basic stuffs, it's way too long and the odds of making an error increase with the length of the code.

So I thought this will be faster using algorithm but i don't know which kind of procedural content generator i need to use.

For informations, here are the conditions i want to put on this algorithm:

  • Rectangles can't collide

  • Rectangles must be spaced by a distance of 30

How can i do that ?

Here is the current method i'm using (i made a little example for the question):

import pygame
import random

screen = pygame.display.set_mode((750,750))
done = False
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
BLUE = (0,0,255)

def main():
    global BLACK, done, WHITE, RED, BLUE, X, Y

    X1 = random.randint(30,300)
    Y1 = random.randint(30,300)
    W1 = random.randint(30,300)
    H1 = random.randint(30,300)

    X2 = random.randint(30,300)
    Y2 = random.randint(30,300)
    W2 = random.randint(30,300)
    H2 = random.randint(30,300)

    X3 = random.randint(30,300)
    Y3 = random.randint(30,300)
    W3 = random.randint(30,300)
    H3 = random.randint(30,300)

    screen.fill(BLACK)
    pygame.draw.rect(screen, WHITE, (X1, Y1, W1, H1))
    pygame.draw.rect(screen, RED, (X2, Y2, W2, H2))
    pygame.draw.rect(screen, BLUE, (X3, Y3, W3, H3))
    pygame.display.update()

    while not done:

        for event in pygame.event.get():

            if event.type == pygame.KEYDOWN and event.key == pygame.K_1:
                main()

            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                pygame.quit()
                quit()


main()
pygame.quit()
quit()
Oboliu
  • 45
  • 3
  • 1
    There are a lot of different ways to generate random maps, but you might be interested in **Noise Functions** and especially **Perlin Noise**. It is very usefull if you plan to have an efficient random map generator. – 000102 Aug 07 '19 at 14:59
  • Thank you for your suggestion. I will check that and come back later. – Oboliu Aug 07 '19 at 15:06

0 Answers0