当我按向右或向左键时,如何使播放器旋转?

时间:2019-06-21 19:11:32

标签: python pygame

我正在尝试制作一个小行星游戏,想知道如何在按向右或向左键时如何顺时针或逆时针旋转玩家,然后在按向上键时玩家应该向前移动。

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.scale(player_img, (50, 38))
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()
        self.radius = 20
        # pygame.draw.circle(self.image, RED, self.rect.center, self.radius)
        self.rect.centerx = WIDTH / 2
        self.rect.bottom = HEIGHT - 10
        self.speedx = 0
        self.speedy = 0
        self.shield = 100
        self.shoot_delay = 250
        self.last_shot = pygame.time.get_ticks()
        self.lives = 3



    def update(self):
        self.speedx = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.speedx = -8
        if keystate[pygame.K_RIGHT]:
            self.speedx = 8
        if keystate[pygame.K_DOWN]:
            self.speedy = 8
        if keystate[pygame.K_UP]:
            self.speedy = -8

        if keystate[pygame.K_SPACE]:
            self.shoot()

        self.rect.x += self.speedx
        self.rect.y += self.speedy

        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.left < 0:
            self.rect.left = 0

    def shoot(self):
        now = pygame.time.get_ticks()
        if now - self.last_shot > self.shoot_delay:
            self.last_shot = now
            bullet = Bullet(self.rect.centerx, self.rect.top)
            all_sprites.add(bullet)
            bullets.add(bullet)

    def hide(self):
        # hide player temporarily
        self.hidden = True
        self.hide_timer = pygame.time.get_ticks()
        self.rect.center = (WIDTH / 2, HEIGHT + 200)````

1 个答案:

答案 0 :(得分:0)

您可以在下面的一个工作示例中找到它(只是重命名加载的图像),我只保留了移动和旋转的基本代码。

gender

由于要旋转,因此不需要直接将速度设置为x和y,而是需要根据return User::create([ 'name' => 'testname', 'avatar' => $avatar_path, 'gender' => 'MALE', 'email' => 'aa@bb.com', 'password' => Hash::make($data['password']), ]); 面向的方向进行计算。

基本思想是使用transform.rotate。跟踪旋转角度(我称其为gender),在按下LEFT或RIGTH键时,对该角度添加/细分一个固定的量(如果需要,旋转速度),然后旋转原始图像。由于旋转缩放了图像,因此我也跟踪protected $fillable = [ 'name', 'avatar', 'gender', <--- make sure that gender is here // Etc. ]; 的中心,从图像中保存新的import sys import math import pygame class Player(pygame.sprite.Sprite): def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) self.player_img = pygame.image.load("yourimage.png").convert() self.image = self.player_img self.rect = self.image.get_rect() self.rect.move_ip(x, y) self.current_direction = 0 #0 degree == up self.speed = 10 def update(self): self.speedx = 0 keystate = pygame.key.get_pressed() prev_center = self.rect.center if keystate[pygame.K_LEFT]: self.current_direction += 10 if keystate[pygame.K_RIGHT]: self.current_direction -= 10 if keystate[pygame.K_DOWN]: self.rect.x += self.speed * math.sin(math.radians(self.current_direction)) self.rect.y += self.speed * math.cos(math.radians(self.current_direction)) if keystate[pygame.K_UP]: self.rect.x -= self.speed * math.sin(math.radians(self.current_direction)) self.rect.y -= self.speed * math.cos(math.radians(self.current_direction)) if keystate[pygame.K_LEFT] or keystate[pygame.K_RIGHT]: self.image = pygame.transform.rotate(self.player_img, self.current_direction) self.rect = self.image.get_rect() self.rect.center = prev_center pygame.init() screen = pygame.display.set_mode((500, 500)) player = Player(200, 200) clock = pygame.time.Clock() while True: screen.fill((0, 0, 0), player.rect) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() player.update() screen.blit(player.image, player.rect) pygame.display.update() clock.tick(50) ,并在旋转后将先前的中心重新设置为player,以便图像仍以旋转为中心。

按下UP或DOWN键时,需要使用三角函数分解x和y轴上的速度,并移动current_direction属性坐标。