我似乎无法使用控件旋转我的精灵

时间:2017-12-13 15:55:17

标签: python pygame transform

class Player(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    # currently this img stuff is a filler until I am bothered to do graphics or someone is willing to do them
    self.image = player_img
    self.image0 = new_player_img
    self.rect = self.image.get_rect()
    self.pos = vec(WIDTH / 2, HEIGHT / 2)
    self.vel = vec(0, 0)
    self.acc = vec(0, 0)
    self.rot = 0
    self.rot_speed = vec(14, -14)
    self.last_update = pygame.time.get_ticks()

def rotate(self):
    rkeys = pygame.key.get_pressed()
    if rkeys[pygame.K_z]:
        self.rot_speed.x = self.rot
    if rkeys[pygame.K_c]:
        self.rot_speed.y = self.rot

    pygame.transform.rotate(self.image0, self.rot)
    self.image = self.image0


def update(self):
    self.acc = vec(0, 0)
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        self.acc.x = -PLAYER_ACC
    if keys[pygame.K_RIGHT]:
        self.acc.x = PLAYER_ACC
    if keys[pygame.K_UP]:
        self.acc.y = -PLAYER_ACC
    if keys[pygame.K_DOWN]:
        self.acc.y = PLAYER_ACC

    #apply translations
    self.acc += self.vel * PLAYER_FRICTION
    self.vel += self.acc
    self.pos += self.vel + 0.5 * self.acc
    self.image0 = self.image
    self.rotate()

    # wrapping around the screen for now until ive worked out a playing area
    if self.pos.x > WIDTH:
        self.pos.x = 0
    if self.pos.x < 0:
        self.pos.x = WIDTH
    if self.pos.y > HEIGHT:
        self.pos.y = 0
    if self.pos.y < 0:
        self.pos.y = HEIGHT

    self.rect.center = self.pos

我不知道为什么但是由于某种原因旋转功能什么也没做,我正在旋转并将播放器图像设置为旋转图像。谁能帮我这个?所有我需要它来为用户旋转精灵,所以我可以像汽车一样“驱动”精灵。

2 个答案:

答案 0 :(得分:1)

rotate(image)不会更改原始image,但会返回新的,因此您必须将其分配给变量

self.image = pygame.transform.rotate(self.image0, self.rot)

顺便说一句:你不应该将image分配给image0

self.image0 = self.image

因为这样你就会松开你应该保持不变的原始image0并用来生成旋转的图像。使用始终原始image0创建旋转的图像,您应该获得更好看的图像,然后旋转image

答案 1 :(得分:1)

pygame.transform.rotate函数返回一个新曲面,并且不会修改原始图像,因此您必须将其指定给self.image属性。之后你必须用self.image.get_rect创建一个新的矩形,以便精灵保持居中。

下一个问题是转速。 rot属性是您必须每帧rot_speed更新的当前角度,例如self.rot += self.rot_speed

我还将事件处理移至更新方法,然后将所需的rot_speed传递给rotate方法。

update方法中,self.acc += self.vel * PLAYER_FRICTIONself.image0 = self.image导致了错误行为,因此我将其删除了。

这是一个固定的最小和完整版本:

import pygame
from pygame.math import Vector2 as Vec

WIDTH, HEIGHT = 640, 480
PLAYER_FRICTION = .95
PLAYER_ACC = .1

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 50), pygame.SRCALPHA)
        self.image.fill(pygame.Color('dodgerblue1'))
        self.image0 = self.image
        self.rect = self.image.get_rect()
        self.pos = Vec(WIDTH / 2, HEIGHT / 2)
        self.vel = Vec(0, 0)
        self.acc = Vec(0, 0)
        self.rot = 0
        self.rot_speed = 3
        self.last_update = pygame.time.get_ticks()

    def rotate(self, rot_speed):
        self.rot += rot_speed  # Change `self.rot` (the angle).
        # Rotate the original image.
        self.image = pygame.transform.rotate(self.image0, self.rot)
        # Get a new rect with the center of the old rect.
        self.rect = self.image.get_rect(center=self.rect.center)

    def update(self):
        self.acc = Vec(0, 0)
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.acc.x = -PLAYER_ACC
        if keys[pygame.K_RIGHT]:
            self.acc.x = PLAYER_ACC
        if keys[pygame.K_UP]:
            self.acc.y = -PLAYER_ACC
        if keys[pygame.K_DOWN]:
            self.acc.y = PLAYER_ACC
        # Call self.rotate with the desired speed.
        if keys[pygame.K_z]:
            self.rotate(self.rot_speed)
        if keys[pygame.K_c]:
            self.rotate(-self.rot_speed)

        # apply translations
        self.vel += self.acc
        self.vel *= PLAYER_FRICTION
        self.pos += self.vel + 0.5 * self.acc

        # wrapping around the screen
        if self.pos.x > WIDTH:
            self.pos.x = 0
        if self.pos.x < 0:
            self.pos.x = WIDTH
        if self.pos.y > HEIGHT:
            self.pos.y = 0
        if self.pos.y < 0:
            self.pos.y = HEIGHT

        self.rect.center = self.pos


def main():
    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    all_sprites = pygame.sprite.Group(Player())

    done = False

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

        all_sprites.update()
        screen.fill((30, 30, 30))
        all_sprites.draw(screen)
        pygame.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pygame.init()
    main()
    pygame.quit()

如果你想像汽车一样移动物体,你必须改变一些东西。我将self.acc设置为所需的值,然后在按下向上或向下键时将其添加到更新方法中的self.vel。然后,您还必须在self.acc方法中轮换self.velrotate向量,它可以根据需要运行。

import pygame
from pygame.math import Vector2 as Vec

WIDTH, HEIGHT = 800, 600
PLAYER_FRICTION = .95

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 50), pygame.SRCALPHA)
        self.image.fill(pygame.Color('dodgerblue1'))
        self.image0 = self.image
        self.rect = self.image.get_rect()
        self.pos = Vec(WIDTH / 2, HEIGHT / 2)
        self.vel = Vec(0, 0)
        self.acc = Vec(0, -1)
        self.rot = 0
        self.rot_speed = 3
        self.last_update = pygame.time.get_ticks()

    def rotate(self, rot_speed):
        self.rot += rot_speed  # Change `self.rot` (the angle).
        self.acc.rotate_ip(-rot_speed)
        self.vel.rotate_ip(-rot_speed)
        # Rotate the original image.
        self.image = pygame.transform.rotate(self.image0, self.rot)
        # Get a new rect with the center of the old rect.
        self.rect = self.image.get_rect(center=self.rect.center)

    def update(self):
        keys = pygame.key.get_pressed()
        # Call self.rotate with the desired speed.
        if keys[pygame.K_LEFT]:
            self.rotate(self.rot_speed)
        if keys[pygame.K_RIGHT]:
            self.rotate(-self.rot_speed)
        # Accelerate if UP or DOWN get pressed.
        if keys[pygame.K_UP]:
            self.vel += self.acc
        if keys[pygame.K_DOWN]:
            self.vel += self.acc

        pygame.display.set_caption('Vel {}, Acc: {}'.format(self.vel, self.acc))
        # apply translations
        self.vel *= PLAYER_FRICTION
        self.pos += self.vel

        # wrapping around the screen
        if self.pos.x > WIDTH:
            self.pos.x = 0
        if self.pos.x < 0:
            self.pos.x = WIDTH
        if self.pos.y > HEIGHT:
            self.pos.y = 0
        if self.pos.y < 0:
            self.pos.y = HEIGHT

        self.rect.center = self.pos


def main():
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    clock = pygame.time.Clock()
    all_sprites = pygame.sprite.Group(Player())

    done = False

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

        all_sprites.update()
        screen.fill((30, 30, 30))
        all_sprites.draw(screen)
        pygame.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pygame.init()
    main()
    pygame.quit()