refactor: move car rendering to Car
This commit is contained in:
		
							
								
								
									
										22
									
								
								src/car.py
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								src/car.py
									
									
									
									
									
								
							| @@ -1,4 +1,8 @@ | ||||
| from math import radians | ||||
|  | ||||
| import pygame | ||||
|  | ||||
| from src.camera import Camera | ||||
| from src.vec import Vec | ||||
|  | ||||
|  | ||||
| @@ -6,6 +10,9 @@ class Car: | ||||
|     MAX_SPEED = 0.05 | ||||
|     MAX_BACK_SPEED = -0.025 | ||||
|     ROTATE_SPEED = radians(1) | ||||
|     COLOR = (230, 150, 80) | ||||
|     WIDTH = 0.4 | ||||
|     LENGTH = 0.6 | ||||
|  | ||||
|     def __init__(self, pos: Vec, direction: Vec) -> None: | ||||
|         self.pos: Vec = pos | ||||
| @@ -40,3 +47,18 @@ class Car: | ||||
|         self.speed *= 0.98 | ||||
|  | ||||
|         self.pos += self.direction * self.speed | ||||
|  | ||||
|     def render(self, surf: pygame.Surface, camera: Camera): | ||||
|         pts: list[Vec] = self.get_corners() | ||||
|         pts = [camera.world2screen(p) for p in pts] | ||||
|         pygame.draw.polygon(surf, self.COLOR, pts) | ||||
|  | ||||
|     def get_corners(self) -> list[Vec]: | ||||
|         u: Vec = self.direction * self.LENGTH / 2 | ||||
|         v: Vec = self.direction.perp * self.WIDTH / 2 | ||||
|         pt: Vec = self.pos | ||||
|         p1: Vec = pt + u + v | ||||
|         p2: Vec = pt - u + v | ||||
|         p3: Vec = pt - u - v | ||||
|         p4: Vec = pt + u - v | ||||
|         return [p1, p2, p3, p4] | ||||
|   | ||||
		Reference in New Issue
	
	Block a user