feat: add speedometer
This commit is contained in:
		
							
								
								
									
										32
									
								
								src/game.py
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								src/game.py
									
									
									
									
									
								
							| @@ -1,3 +1,4 @@ | |||||||
|  | from math import cos, radians, sin | ||||||
| import pygame | import pygame | ||||||
|  |  | ||||||
| from src.camera import Camera | from src.camera import Camera | ||||||
| @@ -29,6 +30,7 @@ class Game: | |||||||
|             str(ROOT / "assets" / "fonts" / "Ubuntu-M.ttf"), 20 |             str(ROOT / "assets" / "fonts" / "Ubuntu-M.ttf"), 20 | ||||||
|         ) |         ) | ||||||
|         self.show_fps: bool = True |         self.show_fps: bool = True | ||||||
|  |         self.show_speed: bool = True | ||||||
|  |  | ||||||
|     def mainloop(self): |     def mainloop(self): | ||||||
|         while self.running: |         while self.running: | ||||||
| @@ -64,6 +66,8 @@ class Game: | |||||||
|         self.car.render(self.win, self.camera) |         self.car.render(self.win, self.camera) | ||||||
|         if self.show_fps: |         if self.show_fps: | ||||||
|             self.render_fps() |             self.render_fps() | ||||||
|  |         if self.show_speed: | ||||||
|  |             self.render_speedometer() | ||||||
|  |  | ||||||
|         pygame.display.flip() |         pygame.display.flip() | ||||||
|  |  | ||||||
| @@ -76,6 +80,10 @@ class Game: | |||||||
|             self.car.left = True |             self.car.left = True | ||||||
|         elif event.key == pygame.K_d: |         elif event.key == pygame.K_d: | ||||||
|             self.car.right = True |             self.car.right = True | ||||||
|  |         elif event.key == pygame.K_f: | ||||||
|  |             self.show_fps = not self.show_fps | ||||||
|  |         elif event.key == pygame.K_v: | ||||||
|  |             self.show_speed = not self.show_speed | ||||||
|  |  | ||||||
|     def on_key_up(self, event: pygame.event.Event): |     def on_key_up(self, event: pygame.event.Event): | ||||||
|         if event.key == pygame.K_w: |         if event.key == pygame.K_w: | ||||||
| @@ -92,3 +100,27 @@ class Game: | |||||||
|             f"{self.clock.get_fps():.1f}", True, self.FPS_COLOR |             f"{self.clock.get_fps():.1f}", True, self.FPS_COLOR | ||||||
|         ) |         ) | ||||||
|         self.win.blit(txt, (self.win.get_width() - txt.get_width(), 0)) |         self.win.blit(txt, (self.win.get_width() - txt.get_width(), 0)) | ||||||
|  |  | ||||||
|  |     def render_speedometer(self): | ||||||
|  |         if self.car.speed == 0: | ||||||
|  |             return | ||||||
|  |         angle: float = self.car.speed / self.car.MAX_SPEED * 180 | ||||||
|  |  | ||||||
|  |         pts1: list[tuple[float, float]] = [] | ||||||
|  |         pts2: list[tuple[float, float]] = [] | ||||||
|  |  | ||||||
|  |         n: int = 30 | ||||||
|  |         r: float = 50 | ||||||
|  |         ox: float = r + 10 | ||||||
|  |         oy: float = r + 10 | ||||||
|  |         thickness: float = 5 | ||||||
|  |         r2: float = r - thickness | ||||||
|  |  | ||||||
|  |         for i in range(n): | ||||||
|  |             a: float = radians(angle * i / (n - 1)) | ||||||
|  |             dx: float = -cos(a) | ||||||
|  |             dy: float = -sin(a) | ||||||
|  |             pts1.append((ox + r * dx, oy + r * dy)) | ||||||
|  |             pts2.append((ox + r2 * dx, oy + r2 * dy)) | ||||||
|  |  | ||||||
|  |         pygame.draw.polygon(self.win, (200, 200, 200), pts1 + pts2[::-1]) | ||||||
|   | |||||||
| @@ -28,8 +28,6 @@ class Road(TrackObject): | |||||||
|             p3: Vec = p1 - pt.normal * pt.width |             p3: Vec = p1 - pt.normal * pt.width | ||||||
|             side1.append(camera.world2screen(p2)) |             side1.append(camera.world2screen(p2)) | ||||||
|             side2.append(camera.world2screen(p3)) |             side2.append(camera.world2screen(p3)) | ||||||
|             col: tuple[float, float, float] = (i * 10 + 150, 100, 100) |  | ||||||
|             pygame.draw.circle(surf, col, camera.world2screen(p1), 5) |  | ||||||
|  |  | ||||||
|         n: int = len(self.pts) |         n: int = len(self.pts) | ||||||
|         for i in range(n): |         for i in range(n): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user