1

I am trying to make an asteroid game and was wondering how to rotate the player clock wise or counter clock wise when the right or left keys have been pressed, and then when the up key is pressed the player should move forward.

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)````
Justin Audet
  • 141
  • 14
  • 1
    This may prove to be a useful reference for you: https://stackoverflow.com/questions/19316759/rotate-image-using-pygame – Hoog Jun 21 '19 at 19:35

1 Answers1

0

You can find below a working example (just rename the loaded image), I've kept only the essential code for movement and rotation.

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)

Since you want rotation, you do not need to assing speed directly to x and y but you need to calculate it according to the direction the player is facing.

The basic idea is to use transform.rotate. Keep track of the rotation angle (I called it current_direction), add / subcract a fixed amount (the rotation speed, if you wish) to this angle when LEFT or RIGTH keys are pressed, and then rotate the original image. Since rotation scales the image, I also keep track of the center of the rect, save the new rect from the iamge and and reassing the previous center to the rect.center after rotation, so that the image remains centered on rotation.

When UP or DOWN keys are pressed, you need to decompose the velocity on x and y axes using trigonometry and move the rect attribute coordinates.

Valentino
  • 7,291
  • 6
  • 18
  • 34