added player movement

This commit is contained in:
specCon18 2025-01-14 22:26:04 -05:00
parent 5b8a9dd113
commit 703023c019
2 changed files with 11 additions and 3 deletions

View file

@ -15,10 +15,10 @@ def main():
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
return return
screen.fill((0, 0, 0)) screen.fill((0, 0, 0))
p1.draw(screen) p1.draw(screen)
p1.update(dt) p1.update(dt)
pygame.display.flip() pygame.display.flip()
tick = clock.tick(60) tick = clock.tick(60)
dt = tick/1000 dt = tick/1000
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -5,7 +5,7 @@ import pygame
class Player(circleshape.CircleShape): class Player(circleshape.CircleShape):
def __init__(self,x,y): def __init__(self,x,y):
super().__init__(x,y,constants.PLAYER_RADIUS) super().__init__(x,y,constants.PLAYER_RADIUS)
self.rotation = 0 self.rotation = 0
def triangle(self): def triangle(self):
forward = pygame.Vector2(0, 1).rotate(self.rotation) forward = pygame.Vector2(0, 1).rotate(self.rotation)
@ -24,6 +24,7 @@ class Player(circleshape.CircleShape):
self.rotation += dt*constants.PLAYER_ROTATION_SPEED self.rotation += dt*constants.PLAYER_ROTATION_SPEED
else: else:
self.rotation += -dt*constants.PLAYER_ROTATION_SPEED self.rotation += -dt*constants.PLAYER_ROTATION_SPEED
def update(self, dt): def update(self, dt):
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
@ -31,3 +32,10 @@ class Player(circleshape.CircleShape):
self.rotate(dt,"left") self.rotate(dt,"left")
if keys[pygame.K_d]: if keys[pygame.K_d]:
self.rotate(dt,"right") self.rotate(dt,"right")
if keys[pygame.K_w]:
self.move(dt)
if keys[pygame.K_s]:
self.move(-dt)
def move(self,dt):
forward = pygame.Vector2(0, 1).rotate(self.rotation)
self.position += forward * constants.PLAYER_SPEED * dt