I’ve been trying to make a space invaders clone in python, but when I try to move player in circle-shaped path, it slides off a bit over time.
I’ve already tried changing image x and y through sin and cos functions, but it doesn’t seem to work
Here is my code:
import pygame
import math
pygame.init()
class Player():
def __init__(self,x,y,width,height,image):
self.original_image = pygame.image.load(image)
self.image = pygame.transform.scale(self.original_image, (width, height))
self.rotated_image = self.image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = width
self.height = height
def move(self, angle, length):
self.rect.x += length * math.sin(math.radians(angle))
self.rect.y -= length * math.cos(math.radians(angle))
self.rotated_image = pygame.transform.rotate(self.image, -angle-90)
window = pygame.display.set_mode((750, 750))
player = Player(375, 630, 40, 48, 'player.png') #basic green arrow
dir = None
cangle = -90
clock = pygame.time.Clock()
game = True
while game:
window.fill((0,0,0))
pygame.draw.circle(window, (255,255,255), (375, 343.5), 286.5, 1)
window.blit(player.rotated_image, (player.rect.x - player.rect.width/2, player.rect.y-player.rect.height/2))
keys = pygame.key.get_pressed()
if dir:
cangle += dir
player.move(cangle, dir * 5)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 3:
if dir == 1:
dir = -1
elif dir == -1:
dir = 1
else:
dir = 1
clock.tick(600) #supposed to be 60, but I put 600 so inaccuracy is easier to find
pygame.display.update()
pygame.quit()
This is where player turns to be after a while
Thank you!