For the past few weeks, I've been trying to figure out how to generate platforms just like how they're generated in the Pou minigame "Sky Hop"(Referring to this game). I've only succeeded in generating them when I assumed that only one platform can spawn in each row. However, in the minigame I'm trying to replicate, multiple platforms have a chance of spawning (maximum of 3 in the row that can hold 4 platforms, maximum of 2 in the row that can hold 3 platforms), which ruins the current code I have, which you can see below:
import pygame,random
# Initialize Pygame
pygame.init()
# Create a screen object
screen = pygame.display.set_mode((800, 600))
# Define the color of the rectangles
color = (255, 0, 0)
# Define the number of rows and columns
rows = 6
cols = 4
# Define the distance between the rectangles
dist = 10
running = True
# Define the width and height of each rectangle
width = 100
height = 50
platforms = []
platforms_alt = []
seed = []
rng = 0
#test_class
class Platform():
def __init__(self, x,y,width,height):
self.rect = pygame.Rect(x,y,width,height)
def draw(self,color):
pygame.draw.rect(screen,pygame.Color(color),self.rect)
# Generates the odd row platforms
for i in range(rows-1):
rng = 0
if i > 1 and rng % 2 == 0:
rng = 1
else:
rng = random.randint(0,cols-2)
for j in range(cols-1):
left = 105 + j * (width *2)
top = i * (height *2)
if i % 2 != 0 and j == rng:
platform = Platform(left,top,width,height)
platforms.append(platform)
seed.append(j)
seed.append(seed[1])
#Generates the even row platforms based on the index of the odd row platforms
for l in range(rows):
for m in range(cols):
left = m * (width *2)
top = l * (height * 2)
if l % 2 == 0:
if seed[0] - seed[1] < 0 and m == seed[int(l/2)]:
platform_alt = Platform(left,top,width,height)
platforms_alt.append(platform_alt)
if seed[0] - seed[1] >= 0 and m == seed[int(l/2)]+1 or random.randint(0,100) <= 5 and (m == seed[int(l/2)] or m > seed[int(l/2)]):
platform_alt = Platform(left,top,width,height)
platforms_alt.append(platform_alt)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
pass
# Odd row platforms painted red
for p in range(platforms.__len__()):
platforms[p].draw("red")
# Even row platforms painted blue
for q in range(platforms_alt.__len__()):
platforms_alt[q].draw("blue")
pygame.display.flip()
I've tried adding a condition that checked if a random number got to a specific value, and when it did, it would spawn an additional platform, however, that often resulted in the platform being isolated from all the other platforms, and generating an additional red platform broke the spawning of the blue platforms.
