You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sky Hop克隆游戏的多平台生成实现求助

问题:复刻《Sky Hop》平台生成逻辑的困境

近几周我一直在尝试复刻Pou小游戏《Sky Hop》的平台生成逻辑。目前我仅能实现每行生成单个平台的效果,但原游戏中每行有概率生成多个平台(可容纳4个平台的行最多生成3个,可容纳3个平台的行最多生成2个),这导致我的现有代码无法满足需求,代码如下:

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

上述代码运行结果

我曾尝试添加随机数值判断条件来生成额外平台,但这常导致平台孤立,且生成额外红色平台会破坏蓝色平台的生成逻辑。


内容的提问来源于stack exchange,提问作者SpookieTheRookie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 16:24:50