Sky Hop 克隆平台生成

问题描述 投票:0回答:1

在过去的几周里,我一直在尝试弄清楚如何生成平台,就像在 Pou 迷你游戏“Sky Hop”中生成平台一样(参考这个游戏)。只有当我假设每一行中只能生成一个平台时,我才成功生成它们。然而,在我尝试复制的迷你游戏中,多个平台有机会生成(行中最多 3 个可以容纳 4 个平台,行中最多 2 个可以容纳 3 个平台),这会破坏当前代码我有,你可以在下面看到:

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()

我尝试添加一个条件来检查随机数是否达到特定值,当达到特定值时,它会生成一个额外的平台,但是,这通常会导致该平台与所有其他平台隔离,并生成额外的红色平台打破了蓝色平台的生成。

python python-3.x pygame
1个回答
0
投票

我建议将 0 到 3 的数字写在一个列表中,然后打乱列表:

platformIndices = list(range(4))
random.shuffle(platformIndices)

使用此列表作为平台索引。如果您只想连续使用 3 个平台,则仅使用列表中的前 3 个索引:

for j in platformIndices[:3]:

平台数量也可以是随机的:

noOFPlatforms = random.randint(2,4)
for j in platformIndices[:noOFPlatforms]:

您还可以定义一个规则,规定如果一行中只有 2 个平台,则下一行必须有 4 个平台:

minNoOf = 4 if i == 0 or lastNoOfPlatforms == 2 else 2  
noOFPlatforms = random.randint(minNoOf, 4)
for j in platformIndices[:noOFPlatforms]:

最小示例:

import pygame,random

pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

width = 100
height = 50
rows = 6
cols = 4
platforms_rows = []

class Platform():
    def __init__(self, x, y, width ,height, color):
            self.color = color
            self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
           pygame.draw.rect(screen, pygame.Color(self.color), self.rect)
         
for i in range(rows):
    platformIndices = list(range(4))
    random.shuffle(platformIndices)
    minNoOf = 4 if i == 0 or len(platforms_rows[-1]) == 2 else 2  
    noOFPlatforms = random.randint(minNoOf, 4)
    platforms = []
    for j in platformIndices[:noOFPlatforms]:
        even = i % 2 == 0
        top = 500 - i * (height * 2)
        left = j * (width * 2) + (105 if not even else 0)
        color = "red" if even else "blue"
        platform = Platform(left, top, width, height, color)
        platforms.append(platform)
    platforms_rows.append(platforms)

running = True                         
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill("black")
    for platforms in platforms_rows:
        for platform in platforms:
            platform.draw()             
    pygame.display.flip()
    clock.tick(100)

pygame.quit()
exit()
© www.soinside.com 2019 - 2024. All rights reserved.