如何按键按下玩家图像面?

时间:2018-01-01 09:48:11

标签: python pygame sprite

我试图让角色面对玩家在python中按下pygame的方式我已经研究了一段时间而无法找到我能做的事情。基本上我希望角色的面部或方向面对玩家按下的内容,对于这个游戏我只需要左右。有人可以给我这么帮忙吗? (这是我到目前为止所做的。)

import pygame
from pygame_functions import *

pygame.init()

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("The Adventure of the Excorist's Meter")

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()

# Set positions of graphics
background_position = [0, 0]

# Load and set up graphics.
background_image = pygame.image.load("skyline_background.jpg").convert()

crashed = False
character_img = pygame.image.load('character_left.png')

def player(x,y):
    gameDisplay.blit(character_img, (x,y))

x =  (display_width * 0.45)
y = (display_height * 0.65)
x_change = 0
player_speed = 0

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
        crashed = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -5
            elif event.key == pygame.K_RIGHT:
                x_change = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 0

    x += x_change

    # Copy image to screen:
    gameDisplay.blit(background_image, background_position)

    player(x,y)

    pygame.display.update()
    clock.tick(60)
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:1)

在两个变量character_leftcharacter_right中使用两个图像并确定正确的图像 - 即。 character_img = character_left - 当你改变方向时。

您可以加载两个不同的图像,或使用pygame.transform.flip创建第二个(翻转)图像。

代码看起来像这样(我没有测试过它)。

import pygame

# --- constants --- (UPPER_CASE_NAMES)

DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

FPS = 30

# --- classes --- (CamelCaseNames)
# empty
# --- functions --- (lower_case_names)
# empty
# --- main --- (lower_case_names)

# - init -

pygame.init()

gameDisplay = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption("The Adventure of the Excorist's Meter")

# - objects -

background_image = pygame.image.load("skyline_background.jpg").convert()
background_position = [0, 0]


player_image_left = pygame.image.load('character_left.png').convert()
player_image_right = pygame.image.load('character_right.png').convert()

player_image = player_image_left

player_speed = 0
player_x = DISPLAY_WIDTH * 0.45
player_y = DISPLAY_HEIGHT * 0.65
player_x_change = 0

# - mainloop -

clock = pygame.time.Clock()
crashed = False

while not crashed:

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_LEFT:
                player_x_change = -5
                player_image = player_image_left

            elif event.key == pygame.K_RIGHT:
                player_x_change = 5
                player_image = player_image_right

        if event.type == pygame.KEYUP:
            if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                player_x_change = 0

    # - updates -

    player_x += player_x_change

    # - draws -

    gameDisplay.blit(background_image, background_position)

    gameDisplay.blit(player_image, (player_x, player_y))

    pygame.display.update()

    clock.tick(FPS)

# - end -

pygame.quit()