-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_player.py
More file actions
101 lines (58 loc) · 2.29 KB
/
test_player.py
File metadata and controls
101 lines (58 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import pygame
from interactable_object import InteractableObject, Direction
from hitbox import Hitbox
from Objects.Projectiles.simple_projectile import SimpleProjectile
from math import cos, sin, sqrt
class TestPlayer(InteractableObject):
def __init__(self, x: int, y: int, w: int, h: int, hitboxes, engine):
super().__init__(x, y, w, h, hitboxes, engine)
self.speed = 4.2
self.jump_height = 7.5
self.maxspeed = 20
self.image = pygame.image.load("Images\\sprite_0.png")
self.image = pygame.transform.scale(self.image, (w, h))
self.engine = engine
self.hitboxes.append(Hitbox(x, y, w, h))
self.jumping = False
self.up_pressed = False
self.circle_radius = self.w
self.downhit_obj = None
self.next_hit_obj = None
self.jump_ctr = 2
self.cw, self.ch = self.w+5, self.h+5
self.angle = 0
self.radius = 100
self.angular_speed = 0.2 #per tick
self.endpoint = (0, 0)
def onHit(self, other: InteractableObject, direction: int) -> None:
self.jump_ctr = 2
def action(self, keys) -> None:
if keys[pygame.K_LEFT] and self.x_vel > -self.maxspeed:
self.x_vel -= self.speed
elif keys[pygame.K_RIGHT] and self.x_vel < self.maxspeed:
self.x_vel += self.speed
if keys[pygame.K_SPACE] and abs(self.y_vel) < self.maxspeed and self.jump_ctr > 0:
self.y_vel -= self.jump_height
self.jump_ctr -= 1
if keys[pygame.K_UP]:
self.up_pressed = True
elif self.up_pressed and not keys[pygame.K_UP]:
self.up_pressed = False
x, y = (self.endpoint[0]-self.x, self.endpoint[1]-self.y)
norm = sqrt(x*x + y*y)
#spawning projectile
initial_x_vel = x / norm
initial_y_vel = y / norm
proj = SimpleProjectile(self.x + self.w//2, self.y + self.h//2, 10, 10, self.engine, initial_x_vel, initial_y_vel)
self.engine.add_extra_object(proj)
else:
self.up_pressed = False
def draw(self, screen) -> None:
screen.blit(self.image, (int(self.x), int(self.y), int(self.w), int(self.h)))
#drawing the aim line
if self.up_pressed:
self.angle += self.angular_speed
if self.angle > 360:
self.angle -= 360
self.endpoint = (int(cos(self.angle) * self.radius + self.x + self.w//2), int(sin(self.angle)*self.radius + self.y + self.h//2))
pygame.draw.line(screen, (0xFF, 0x0, 0x0), (self.x + self.w//2, self.y + self.h//2), self.endpoint, 5)