在按住按钮时旋转pygame精灵的最简单方法是什么?

时间:2017-10-02 11:58:39

标签: python pygame sprite

我目前正在开发一款使用Pygame的赛车游戏。我知道为了让精灵像真车一样移动,需要三角法。但是,就目前而言,我只是试图让用户按住按钮时旋转赛车图像。最简单的方法是什么?

import pygame
pygame.init()

#DEFING COLOURS
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
BLUE = (0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)

size = (800,600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My First Game")

class Car(pygame.sprite.Sprite):
    #BASE CAR CLASS

    def __init__(self, filename):
        #INITIALISE OBJECT PROPERTIES
        super().__init__()

        #LOAD IMAGE
        self.image = pygame.image.load(filename).convert_alpha()

        #SET BACKGROUND COLOUR
        #self.image.set_colorkey(WHITE)

        #SET RECTANGLE COLLISION BOX
        self.rect = self.image.get_rect()

        self.angle = 0
        self.angle_change = 0

#Create sprites list
all_sprites_list = pygame.sprite.Group()

#Create F1car object
F1car = Car("car.png")
car_rotation = 0.0
surface = pygame.Surface((15, 15))

#Add F1car to sprites list
all_sprites_list.add(F1car)

#LOOP UNTIL USER EXITS THE GAME
carryOn = True

#CLOCK TO CONTROL FRAME RATE
clock = pygame.time.Clock()

##MAIN LOOP##
while carryOn:
    #MAIN EVENT LOOP
    for event in pygame.event.get(): #USER DID SOMETHING
        if event.type == pygame.QUIT: #IF USER CLICKED CLOSE
            carryOn = False #END THE LOOP
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                car_rotation += 0.1
                pygame.transform.rotate(surface, car_rotation)

    #GAME LOGIC

    #DRAWING CODE

    #CLEARING SCREEN
    screen.fill(WHITE)

    #DRAWING SHAPES
    pygame.draw.rect(screen, RED, [55, 200, 100, 70], 0)
    pygame.draw.rect(screen, BLUE, [78, 300, 60, 70], 0)

    #LIST OF SPRITES TO COLLIDE WITH EACHOTHER
    #blocks_hit_list = pygame.sprite.spritecollide(F1car, )

    #DRAW SPRITES FROM all_sprites_list LIST
    all_sprites_list.draw(screen)

    #UPDATE THE SCREEN
    pygame.display.flip()

    #SET UPDATE RATE
    clock.tick(60)

pygame.quit()

2 个答案:

答案 0 :(得分:0)

这可以简单地完成。你需要一个检查输入的游戏循环。然后,您必须检查是否存在所需的输入,并在每次输入时增加汽车的旋转。

import pygame

run = True

car_rotation = 0.0
surface = pygame.Surface((100, 60)) # 100 horizontal length. 60 is the vertical length.

while run:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                car_rotation += 0.1
                surface = pygame.transform.rotate(surface, car_rotation)

对于您的代码

你在循环中做了一些错误的检查。将代码从pygame.K_RIGHT更改为pygame.K_r,以使用R键旋转精灵。要使用鼠标,请将pygame.event.type更改为.MOUSEBUTTONDOWN.MOUSEBUTTONUP,并保留pygame.K_RIGHT

将if语句if event.key == pygame.K_r更改为

if event.key == pygame.K_r
    car_rotation += 1.0
    for car in all_sprites_list:
        car.image = pygame.transform.rotate(car.image, car_rotation)

然后删除surface = pygame.Surface((15, 15))

答案 1 :(得分:0)

我为Car课程提供update方法,如果self.angle_change不为0,则在此方法中轮播汽车。这样您就可以拨打all_sprites.update()调用所有包含的精灵的update方法。

在事件循环中设置angle_change以开始旋转。在更新方法中,按self.angle增加self.angle_change,然后使用pygame.transform.rotate.rotozoom并传递self.angle。之后你需要获得一个新的rect并将旧rect的中心作为center参数传递。

import pygame


pygame.init()

screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

WHITE = (255, 255, 255)
CAR_IMAGE = pygame.Surface((45, 90), pygame.SRCALPHA)
CAR_IMAGE.fill((150, 20, 0))


class Car(pygame.sprite.Sprite):

    def __init__(self, pos, image):
        super().__init__()
        self.image = image
        # Store a reference to the original to preserve the image quality.
        self.orig_image = self.image
        self.rect = self.image.get_rect(center=pos)

        self.angle = 0
        self.angle_change = 0

    def update(self):
        if self.angle_change != 0:
            self.angle += self.angle_change
            # I prefer rotozoom because it looks smoother.
            self.image = pygame.transform.rotozoom(self.orig_image, self.angle, 1)
            self.rect = self.image.get_rect(center=self.rect.center)


all_sprites = pygame.sprite.Group()
f1_car = Car((300, 300), CAR_IMAGE)
all_sprites.add(f1_car)

carryOn = True

while carryOn:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            carryOn = False
        elif event.type == pygame.KEYDOWN:
            # Set the rotation speed of the car sprite.
            if event.key == pygame.K_RIGHT:
                f1_car.angle_change = -3
            elif event.key == pygame.K_LEFT:
                f1_car.angle_change = 3
        elif event.type == pygame.KEYUP:
            # Stop rotating if the player releases the keys.
            if event.key == pygame.K_RIGHT and f1_car.angle_change < 0:
                f1_car.angle_change = 0
            elif event.key == pygame.K_LEFT and f1_car.angle_change > 0:
                f1_car.angle_change = 0

    all_sprites.update()

    screen.fill(WHITE)
    all_sprites.draw(screen)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()