Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ensuring there is always a platform for player to jump on

I am making a game where the player has to jump on platforms. However i am having a problem ensuring that there is always a tile for the player to step on. I created platforms this way:

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) is the x position of the player and the numbers after comma is the y position of the player. How can i ensure that there is atleast 4 platforms on the screen withn 300 x-distance and 200 y-distance of each other (since my window size is 1200, 600) while also giving an impression that they are random and not not always in the same position. Thanks

Also here is my platform class for any references needed:

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

1 Answers

A simple workaround is to make a loop and take the last plateform position.

Something like:

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

You can make additional check to prevent your plateform being vertically out of screen by changing the range of the y random number generation.

like image 171
Noé Avatar answered Nov 27 '25 00:11

Noé



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!