Search code examples
pythonspyder

pygame playerrect dont moves


enter image description hereI build a ground map is about 1280x720p there is a rectangle in center of map(x=640 y=360) player character have 500 health, and gets damage whenever collide with rectangle in center of map. If health is 0 i make a respawn code, That code sets player rectangle to (x=100,y=100)

Here is code:

import pygame
import keyboard
pygame.init()

speed_player=5.0

number = '10'

game_font =pygame.font.Font('font\THEBOLDFONT.ttf',50)
window = pygame.display.set_mode((1280,720))
pygame.display.set_caption('test')
clock = pygame.time.Clock()
#health_bar
current_health = 500
max_health = 1200
health_bar_length = 250
health_ratio = max_health / health_bar_length

def get_damage(amount):#damage funct
    global current_health
    if current_health > 0:
        current_health -= amount
    if current_health <= 0:
        current_health = 0
        
def get_health(amount):#heal funct
    global current_health
    if current_health < max_health:
        current_health += amount
    if current_health >= max_health:
        current_health = max_health

ground_surface = pygame.image.load('sand.png').convert_alpha()

stone_surface = pygame.image.load('stone.png').convert_alpha()
stone_rectangle = stone_surface.get_rect(center = (640,360))

player_surface = pygame.image.load('player.png').convert_alpha()
player_rectangle = player_surface.get_rect(topleft = (0,0))



text_surface = game_font.render('WELCOME',True,'Black')
text_rectangle = text_surface.get_rect(center = (640,90))


number_surface = game_font.render(number,True,'White')
number_rectangle = number_surface.get_rect(center = (800,90))
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            
    
    window.blit(ground_surface,(0,0)) 
    
    if keyboard.is_pressed('w'):player_rectangle.y -= speed_player
    if keyboard.is_pressed('s'):player_rectangle.y += speed_player
    if keyboard.is_pressed('a'):player_rectangle.x -= speed_player
    if keyboard.is_pressed('d'):player_rectangle.x += speed_player
    
    window.blit(stone_surface,stone_rectangle)
    window.blit(player_surface,player_rectangle)
    #write health
    if player_rectangle.colliderect(stone_rectangle):
        get_damage(10)
        print(current_health)
    
    #respawn
    if current_health == 0: 
        player_rectangle.x = 500
        player_rectangle.y = 500
    
    if player_rectangle.colliderect(stone_rectangle): print("collision")
    
    window.blit(text_surface,(text_rectangle))
    window.blit(number_surface,number_rectangle)
    pygame.display.update()    
    clock.tick(60)

pygame.quit()

problem is when i respawn player rectangle(set x and y both to 500) player rectangle cannot move more than about 10-20 px to every where


Solution

  • I tried out your code and ran into the same issue where the player gets stuck when respawned. At issue is the "if" test for health. Since the program does not reset the health, the "if" test gets repeated and continues to reset the player to the "500, 500" position. I added a line of code to give the player some "health".

        #respawn
        if current_health == 0: 
            player_rectangle.x = 500
            player_rectangle.y = 500
            current_health = 100        # Otherwise the "if" statement stays true and keeps placing the player back at "500, 500"
    

    That then allowed me to resume moving the player around.

    Give that a try.