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?