Here is the code that tries to make my player stand on terrain -
class Player(pygame.sprite.Sprite):
def __init__(self):
...
self.image = self.images_run_list[self.hero_index]
self.rect = self.image.get_rect(center=(200, 200))
...
def vertical_collision(self):
import playground
for terrain in playground.terrain_group:
if self.rect.colliderect(terrain.rect):
print('collision')
if self.rect.bottom < terrain.rect.top:
# self.rect.bottom = terrain.rect.top
print('rest on the terrain')
I was able to print "collision", but unable to print "rest on the terrain". That means that I cannot reach the second if
statement, if self.rect.bottom < terrain.rect.top:
. Could someone recognize the problem here?
Here is the implementation of terrain
from playground.py
-
terrain_group = pygame.sprite.Group()
# Timer
timer = pygame.USEREVENT + randint(0, 1)
pygame.time.set_timer(timer, 1500)
while True:
# this loop ques a user's command and execute according to the code written in the loop
for event in pygame.event.get():
if event.type == timer:
terrain_group.add(Terrains(choice([1, 2, 3])))
terrain_group.add(Terrains(choice([1, 2, 3, 3])))
terrain_group.add(Terrains(choice([2, 3, 3])))
# calling terrain
terrain_group.draw(screen)
terrain_group.update() #