My Pygame Game Engine
1from src.scripts.modules.entity import *
2import pygame, math
3
4class Player_Renderer_Component:
5 def __init__(self, game, parent) -> None:
6 self.game = game
7 self.parent = parent
8
9 self.walking_animation = []
10 self.walking_animation_frame = 0
11 self.walking_animation_frame_counter = 0
12 for i in range(18):
13 if i < 10:
14 i = '0' + str(i)
15 self.walking_animation.append(self.game.image.load('player/walking_' + str(i) + '.png'))
16
17 def update(self):
18 pass
19
20 def render(self):
21 if self.game.input.is_pressed(self.parent.fwd):
22 self.walking_animation_frame_counter += 100 * self.game.delta_time
23 if self.walking_animation_frame_counter >= 6:
24 self.walking_animation_frame += 1
25 self.walking_animation_frame_counter = 0
26 if self.walking_animation_frame > 17:
27 self.walking_animation_frame = 0
28
29 pygame.draw.circle(self.game.renderer.main_surface, self.parent.color2, (self.parent.camera_relative_x, self.parent.camera_relative_y), 34)
30 pygame.draw.circle(self.game.renderer.main_surface, self.parent.color, (self.parent.camera_relative_x, self.parent.camera_relative_y), 32)
31
32 mx, my = pygame.mouse.get_pos()
33 if self.game.input.is_pressed(self.parent.fwd):
34 self.parent.transform.direction = 180 - self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, mx, my) + 360
35
36 image = self.walking_animation[self.walking_animation_frame]
37 image = pygame.transform.scale(image, (64, 64))
38 image, rect = self.game.math.rotate_center(image, self.parent.transform.direction, self.parent.camera_relative_x, self.parent.camera_relative_y)
39 self.game.renderer.main_surface.blit(image, rect)
40
41class Player_Movement_Component():
42 def __init__(self, game, parent) -> None:
43 self.game = game
44 self.parent = parent
45
46 def update(self):
47 move_acc = 50 * self.game.delta_time
48 self.parent.move_speed = min(self.parent.move_speed, 400 * self.game.delta_time)
49 if self.game.input.is_pressed(self.parent.fwd):
50 self.parent.move_speed += move_acc
51 else:
52 self.parent.move_speed -= self.parent.move_speed / 32
53 mx, my = pygame.mouse.get_pos()
54 angle = self.game.math.direction_between_points(self.parent.transform.x, self.parent.transform.y, mx, my)
55 self.parent.transform.x += self.game.math.lengthdir_x(self.parent.move_speed, angle)
56 self.parent.transform.y += self.game.math.lengthdir_y(self.parent.move_speed, angle)
57
58 def render(self):
59 pass
60
61class player(Entity):
62 def __init__(self, game) -> None:
63
64 self.game = game
65
66 self.components = []
67 self.transform = Transform()
68
69 self.move_speed = 0
70 self.fwd = 'w'
71
72 self.color = (238, 195, 154)
73 self.color2 = (238, 148, 127)
74
75 self.add_component(Camera_Relative_Component(game, self))
76 self.add_component(Player_Renderer_Component(game, self))
77 self.add_component(Player_Movement_Component(game, self))