旋转在 Pygame - 汽车游戏中不起作用



我的车应该在屏幕上移动,它确实如此,但也应该由于方向而旋转 - 但这不会发生。

我已经尝试了很多不同的东西,但它们不起作用,而且由于我是一个乞丐,如果有人能告诉需要更改 - 可能是Global Direction代码,那就太好了。

import pygame
pygame.init()
done = False

size = (900, 570)
x =  (50)
y = (320)
x_change = 0
y_change = 0
pygame.display.set_caption('Car Racing!')
gameDisplay = pygame.display.set_mode((size))
background_image = pygame.image.load('backgroundpicture.png').convert()
bluecar = pygame.image.load('car_black_small_1.png').convert_alpha()
bluecar = pygame.transform.scale(bluecar, (20, 50))
direction = 'right'
def rotate():
    global direction
    if direction == 'right':
        pygame.transform.rotate(bluecar, 270)
    if direction == 'left':
        pygame.transform.rotate(bluecar, 90)
    if direction == 'up':
        pygame.transform.rotate(bluecar, 0)
    if direction == 'down':
        pygame.transform.rotate(bluecar, 180)
def car(x,y):
    gameDisplay.blit(bluecar, (x,y))

while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -0.25
                direction = 'left'
            elif event.key == pygame.K_RIGHT:
                x_change = 0.25
                direction = 'right'
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                y_change = -0.25
                direction = 'up'
            elif event.key == pygame.K_DOWN:
                y_change = 0.25
                direction = 'down'
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 0
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                y_change = 0
    x += x_change
    y += y_change
    gameDisplay.blit(background_image, [0,0])
    car(x,y)
    rotate()
    pygame.display.flip()
pygame.quit()

我希望汽车会随着钥匙的方向旋转 - 即。 左键向左旋转汽车

pygame.transform.rotate返回一个新的旋转pygame.Surface
不要旋转原始图像,在函数rotate中返回一个新的旋转图像:

def rotate():
    rot_image = bluecar
    if direction == 'right':
        rot_image = pygame.transform.rotate(bluecar, 270)
    if direction == 'left':
        rot_image = pygame.transform.rotate(bluecar, 90)
    if direction == 'up':
        rot_image = pygame.transform.rotate(bluecar, 0)
    if direction == 'down':
        rot_image = pygame.transform.rotate(bluecar, 180)
    return rot_image

见阿洛斯 如何使用 Pygame 围绕其中心旋转图像?

在函数car中旋转图像,通过.get_rect()获取图像的pygame.Rect,并将中心设置为(xy(:

def car(x,y):
    rot_image = rotate()
    rect = rot_image.get_rect(center = (x,y))
    gameDisplay.blit(rot_image, rect)

移动时图像旋转的答案与 d 显示了如何将 pygame.sprite.Sprite用于移动的汽车。

使用链接中的类Car并创建一个 car 对象,并将 car 添加到 wain 循环之前的pygame.sprite.Group.update().draw()循环中的组:

car = Car(bluecar, x, y, 5, 0)
group = pygame.sprite.Group(car) 
clock = pygame.time.Clock()
while done == False:
    clock.tick(60)
    # [...]
    gameDisplay.blit(background_image, [0,0])
    group.update()
    group.draw(gameDisplay)
    pygame.display.flip()

最新更新