You might think this is because of the gravity, but it's not. I tried setting gravity to 0, but no go. When the "man" sprite lands on the platform I've placed beneath him and have made it so it catches him, the sprite then stays perfectly still on the platform, but when I move him left and right he sinks into the platform with each press of the key like it's quicksand. Keep in mind this happens in mid-air too, it's just easier to observe when he's on the platform. What could be the cause of this?
import pygame, sys, time, random
"""
def create_mov_plat():
new_plat = platform.get_rect(midtop = (250, 450))
return new_plat
"""
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 800))
pygame.display.set_caption('Climbing Man')
#Game elements
gravity = 0.15
mov_of_man = 0
right_mov = 2
left_mov = 2
#background
bg_image = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/bckwall.jpg").convert()
bg_image = pygame.transform.scale(bg_image,(500, 900))
#the ladder
ladder = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/ladder.png").convert_alpha()
ladder = pygame.transform.scale(ladder, (43,206))
ladder_rec = ladder.get_rect(center = (250,600))
#the player
man = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/man.png").convert_alpha()
man = pygame.transform.scale(man, (51, 70))
man_rec = man.get_rect(center = (250,300))
#the starting platform
first_platform = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/platform.png").convert_alpha()
first_platform = pygame.transform.scale(first_platform,(300,60))
first_platform_rec = first_platform.get_rect(center = (250,730))
def check_collison(first_platform):
if man_rec.colliderect(first_platform_rec):
global mov_of_man
mov_of_man = 0.1
return
"""
#the platforms player moves on
platform = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/platform.jpg").convert_alpha()
platform = pygame.transform.scale(platform,(280,80))
platform_pos = 700
"""
#moving platforms
plat_list = []
PLATMOV = pygame.USEREVENT
pygame.time.set_timer(PLATMOV, 1200)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
mov_of_man += right_mov
man_rec.centerx += mov_of_man
if event.key == pygame.K_LEFT:
mov_of_man += left_mov
man_rec.centerx -= mov_of_man
#if event.type == PLATMOV:
#plat_list.append(create_plat())
#surfaces
screen.blit(bg_image,(0,0))
screen.blit(man, man_rec)
#screen.blit(ladder, ladder_rec)
screen.blit(first_platform, (first_platform_rec))
#screen.blit(platform,(25,platform_pos))
#platform_pos += 1
mov_of_man += gravity
man_rec.centery += mov_of_man
check_collison(first_platform)
pygame.display.update()
clock.tick(120)
mov_of_man
is set to 0.1
at the end of the mainloop. When you press LEFT or RIGHT, left_mov
or right_mov
is added, so it's now 2.1
.
Then the line
man_rec.centery += mov_of_man
moves man_rect
down by that amount (rounded to 2 pixels).
You should use either two variables to keep track of the velocity of your player, or use a vector instead.