Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

My key does not work eventhough I pressed it already Python Pygame

    import pygame, sys
    
    pygame.init()
    pygame.display.set_caption("test 1")
    
    #main Variables
    clock = pygame.time.Clock()
    window_size = (700, 700)
    screen = pygame.display.set_mode(window_size, 0, 32)
    
    #player variables
    playerx = 150
    playery = -250
    player_location = [playerx, playery]
    player_image = pygame.image.load("player/player.png").convert_alpha()
    player_rect = player_image.get_rect(center = (80,50))
    
    #button variables
    move_right = False
    move_left = False
    move_up = False
    move_down = False
    
    while True:
        screen.fill((4, 124, 32))
        screen.blit(player_image, player_location, player_rect)
    
        if move_right == True:
            playerx += 4
        if move_left == True:
            playerx -= 4
        if move_up == True:
            playery -= 4
        if move_down == True:
             playery += 4
    
        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_d:
                    move_right = True
                if event.key == pygame.K_a:
                    move_left = True
                if event.key == pygame.K_w:
                    move_up = True
                if event.key == pygame.K_s:
                    move_down = True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d:
                    move_right = False
                if event.key == pygame.K_a:
                    move_left = False
                if event.key == pygame.K_w:
                    move_up = False
                if event.key == pygame.K_s:
                    move_down = False
    
        pygame.display.update()
        clock.tick(120)

I cant get it to work. I pressed the button but it wont go up or down. It worked well when i used no rectangle for the player. I wanter so i can move the character up and down in the y axis also. I just started to learn how to use PyGame so please help me thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As you move the player, you change the playerx and playery variables. However, the player id drawn to the position that is stored in player_location. You must update player_location before drawing the player:

while True:
    screen.fill((4, 124, 32))
    player_location = [playerx, playery]
    screen.blit(player_image, player_location, player_rect)

    # [...]

Note that you don't need player_location at all. Draw the player at [playerx, playery]:

while True:
    screen.fill((4, 124, 32))
    screen.blit(player_image, [playerx, playery], player_rect)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...