应该用键盘输入移动的Sprite不会显示

时间:2014-10-15 08:42:49

标签: python pygame

import pygame
from pygame.locals import *
import sys

pygame.init()

# Creating a Window
pygame.display.set_caption("Hello there")
screen = pygame.display.set_mode((640, 480))

player = pygame.image.load("player.png")
clock = pygame.time.Clock()

# X AND Y VALUES FOR PLAYER
player_x = 32
player_y = 32

#MOVING AROUND
#moving_right = False
#moving_left = False
#moving_up = False
#moving_down = False

#GAME LOOP
moving_right = False
moving_left = False
moving_up = False
moving_down = False
clock.tick(60)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_LEFT:
                moving_left = True
            elif event.key == K_RIGHT:
                moving_right = True
            elif event.key == K_UP:
                moving_up = True
            elif event.key == K_DOWN:
                moving_down = True

    elif event.type == KEYUP:
        if event.key == K_LEFT:
            moving_left = False
        elif event.key == K_RIGHT:
            moving_right = False
        elif event.key == K_UP:
            moving_up = False
        elif event.key == K_DOWN:
            moving_down = False

screen.blit(player, (player_x, player_y))

pygame.display.update()

  #UPDATING PLAYER
player_speed = 15
if moving_up:
    player_y -= player_speed
elif moving_down:
    player_y += player_speed
if moving_left:
    player_x -= player_speed
elif moving_right:
    player_x += player_speed


#CLEAR THE SCREEN OFF SO THERE'S NO TRAIL WHEN THE PLAYER MOVES
screen.fill((0, 0, 0))

我似乎无法看到我出错的地方。当我运行游戏时没有错误,sptite只是没有出现在屏幕上。我希望能够在按住键的同时移动精灵。

1 个答案:

答案 0 :(得分:1)

你的代码很好,你只是有缩进问题。 While True:循环应如下所示:

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_LEFT:
                moving_left = True
            elif event.key == K_RIGHT:
                moving_right = True
            elif event.key == K_UP:
                moving_up = True
            elif event.key == K_DOWN:
                moving_down = True

        elif event.type == KEYUP:
            if event.key == K_LEFT:
                moving_left = False
            elif event.key == K_RIGHT:
                moving_right = False
            elif event.key == K_UP:
                moving_up = False
            elif event.key == K_DOWN:
                moving_down = False

    screen.blit(player, (player_x, player_y))

    pygame.display.update()

      #UPDATING PLAYER
    player_speed = 15
    if moving_up:
        player_y -= player_speed
    elif moving_down:
        player_y += player_speed
    if moving_left:
        player_x -= player_speed
    elif moving_right:
        player_x += player_speed


    #CLEAR THE SCREEN OFF SO THERE'S NO TRAIL WHEN THE PLAYER MOVES
    screen.fill((0, 0, 0))

这里的想法是,您需要在While循环中对每个发生的event进行blit并更新屏幕。