How do I make my player run on a floating platform?

Question:

I am struggling to make my player run on a floating platform that hovers horizontally from right to left side of the screen. I am thinking to get a list of valid (x, y) coordinates where my player could run on, if it is on a platform. Here is the code for it –

def vertical_collision(self):
    import playground
    can_jump = True
    keys = pygame.key.get_pressed()
    for platform in playground.platform_group:

        if self.rect.colliderect(platform.rect) and can_jump and self.image == self.player_jump:
            # if player stands on one of the terrains
            if self.rect.bottom >= platform.rect.top:
                # allows player to stand on the platform
                self.rect.bottom = platform.rect.top

                # player's x cor is changed according to platform's x cor
                where_to_run = [range(platform.rect.topleft, platform.rect.topright)]
                while self.rect.bottom in where_to_run:
                    self.rect.top = where_to_run

                if keys[pygame.K_w] or keys[pygame.K_UP]:
                    can_jump = False
                else:
                    can_jump = True

(Coding for this issue below the comment, # player's x cor is changed according to platform's x cor.)

But the problem is that my game crashes whenever it jumps and collide with one of the platforms. Here is the stack trace for it –

Traceback (most recent call last):
  File "/Users/xyz/Documents/Extracurricular/Side Projects/pyGameTutorial/playground.py", line 69, in <module>
    player.update()  #
  File "/Users/xyz/Documents/Extracurricular/Side Projects/pyGameTutorial/Player.py", line 140, in update
    self.vertical_collision()
  File "/Users/xyz/Documents/Extracurricular/Side Projects/pyGameTutorial/Player.py", line 78, in vertical_collision
    import playground
  File "/Users/xyz/Documents/Extracurricular/Side Projects/pyGameTutorial/playground.py", line 69, in <module>
    player.update()  #
  File "/Users/xyz/Documents/Extracurricular/Side Projects/pyGameTutorial/Player.py", line 140, in update
    self.vertical_collision()
  File "/Users/xyz/Documents/Extracurricular/Side Projects/pyGameTutorial/Player.py", line 90, in vertical_collision
    where_to_run = [range(platform.rect.topleft, platform.rect.topright)]
TypeError: 'tuple' object cannot be interpreted as an integer

I don’t know how to get a list of all x coordinates of a floating platform, and allow the player to run on those coordinates only. Could someone suggest a new way or rectify my error so that my player could stand on the platform?

Asked By: Jezues

||

Answers:

The problem seems to be your coordinates are stored in tuples like (10,15) to indicate x-coordinate of 10 and y-coordinate of 15. However, where_to_run should contain a range of x coordinates only so you should only give the 1st element of the tuple with [0]. Also, you need to iterate over the range to get the list with something like [x for x in range()]:

# contains the platform's x cors
where_to_run = [x for x in range(platform.rect.topleft[0], platform.rect.topright[0]+1)]

I am not sure what you are trying to do in the while loop but it seems incorrect. self.rect.top = where_to_run is trying to assign all the x coordinates of platform to the player which does not make sense. I believe you can replace that line with any specific action you want the player to do while on the platform.

# while player's x cor is on the plaform's x cor do whatever
while self.rect.bottom[0] in where_to_run:
    # do whatever you want on the platform

# coming outside the while loop means player is no longer on the platform
Answered By: viggnah
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.