【问题标题】:Ensuring there is always a platform for player to jump on确保总有一个平台供玩家跳跃
【发布时间】:2020-05-03 21:06:20
【问题描述】:

我正在制作一个玩家必须在平台上跳跃的游戏。但是,我在确保始终有一块瓷砖可供玩家踩踏时遇到问题。我是这样创建平台的:

p1 = Platforms(random.randint(0, 200), -100)
p2 = Platforms(random.randint(200, 400),-250)
p3 = Platforms(random.randint(400, 600),-600)
p4 = Platforms(random.randint(600, 800),-400)
p5 = Platforms(random.randint(800, 1000),-500)
p6 = Platforms(random.randint(800, 1000),-300)

random.randint(n, n) 是玩家的x 位置,逗号后面的数字是玩家的y 位置。我如何确保屏幕上至少有 4 个平台,它们之间的 x 距离和 200 y 距离(因为我的窗口大小是 1200、600),同时还给人一种它们是随机的而不总是在其中的印象相同的位置。谢谢

这也是我需要的任何参考的平台类:

class Platforms:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 100
        self.height = 20
        self.speed_list = [0.5, 0.8, 1]


    def draw(self):
        pygame.draw.rect(g.d, (3, 124, 23), (self.x, self.y, self.width, self.height))

    def move(self):
        self.y += random.choice(self.speed_list)

    def reset(self):
        if p1.y > 600:
            p1.y = -100 
            p1.x =  random.randint(0, 200)
        if p2.y > 600:
            p2.y = -250
            p2.x = random.randint(200, 400)
        if p3.y > 600:
            p3.y = -600
            p3.x = random.randint(400, 600)
        if p4.y > 600:
            p4.y = -400
            p4.x = random.randint(600, 800)
        if p5.y > 600:
            p5.y = -500
            p5.x = random.randint(800, 1000)
        if p6.y > 600:
            p6.y = -300
            p6.x = random.randint(800, 1000)


    def on_plat(self):
        for plat in g.platlist:
            if (b.x > plat.x) and (b.x < plat.x + plat.width) or (b.x + b.side > plat.x) and (b.x + b.side < plat.x + plat.width):
                if (b.y > plat.y) and (b.y < plat.y + plat.height) or (b.y + b.side > plat.y) and (b.y + b.side < plat.y + plat.height):
                    b.is_jumping = False
                    b.y =  plat.y - b.side

p1 = Platforms(random.randint(0, 200), -100)
p2 = Platforms(random.randint(200, 400),-250)
p3 = Platforms(random.randint(400, 600),-600)
p4 = Platforms(random.randint(600, 800),-400)
p5 = Platforms(random.randint(800, 1000),-500)
p6 = Platforms(random.randint(800, 1000),-300)

g.platlist = [p1, p2, p3, p4, p5, p6]

while True: 
        for plats in g.platlist:
            plats.draw()
            plats.move()
            plats.reset()
            plats.on_plat()

【问题讨论】:

  • 使用Platform 类而不是Platforms 类不是更好吗?目前,如果我们决定总共需要 15 个平台会怎样?
  • @AMC 虽然我将它命名为 Platfroms,但它具有平台的所有属性。我知道问题已经得到解答,但如果我们决定需要 15 个平台,我们可以使用 for 循环在列表中附加 15 个平台,这就是我将 x 和 y 传递给 init 的原因函数,以便它们可以成为平台属性中唯一的变量

标签: python random pygame


【解决方案1】:

一个简单的解决方法是制作一个循环并占据最后一个平台位置。

类似:

platforms = []
for i in range(6):
  x, y = 0, 0
  if (i > 0):
    lastplatform = platforms[i - 1]
    x, y = platforms[i - 1].x, platforms[i - 1].y
  x += random.randint(0, 200)
  y += random.randint(-100, 100)
  platforms.append(Platforms(x, y))

您可以通过更改 y 随机数生成的范围来进行额外检查,以防止您的平台垂直超出屏幕。

【讨论】:

  • 如果有帮助,您能否验证我的回答:)
  • 嗨,我无法理解代码。你介意解释一下第 4、5 和 6 行是做什么的吗?这会很有帮助:)
  • 根据我的理解,for的范围从1开始一直增加到6,那么在第4行“i”不会总是大于0吗?我很困惑
  • 范围从 0 迭代到 x - 1。所以它给你:0,1,2,3,4,5
  • 第 4 行检查它是否是第一次迭代。第 5 行获取最后一个平台,它位于索引 i - 1 处,因为当您将元素附加到数组时,该元素将添加到数组的末尾。第6行,你占据最后一个平台的最后一个位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多