-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.py
More file actions
90 lines (81 loc) · 3.46 KB
/
Player.py
File metadata and controls
90 lines (81 loc) · 3.46 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
import pygame
import Utility
class Player:
radius = 25
uid = None
health = 0
mana = 0
# MOVEMENT
acceleration_maximum = 5
acceleration_minimum = -acceleration_maximum
velocity_maximum = 15
velocity_minimum = -velocity_maximum
horizontal_velocity = 0
vertical_velocity = 0
horizontal_acceleration = 0
vertical_acceleration = 0
accelerate_tick_wait = 0
accelerate_tick_wait_default = 5
can_accelerate = False
target_x = 0
target_y = 0
def __init__(self, startx, starty, color=(255, 0, 0)):
self.x = startx
self.y = starty
self.color = color
def request_movement(self, dirn):
"""
:param dirn: 0 - 3 (right, left, up, down)
"""
# this controls your movement
if dirn == 0:
if self.can_accelerate:
self.horizontal_acceleration += 1
if self.horizontal_acceleration > self.acceleration_maximum:
self.horizontal_acceleration = self.acceleration_maximum
elif dirn == 1:
if self.can_accelerate:
self.horizontal_acceleration -= 1
if self.horizontal_acceleration < self.acceleration_minimum:
self.horizontal_acceleration = self.acceleration_minimum
elif dirn == 2:
# accelerate up
if self.can_accelerate:
self.vertical_acceleration -= 1
if self.vertical_acceleration < self.acceleration_minimum:
self.vertical_acceleration = self.acceleration_minimum
elif dirn == 3:
# accelerate down
if self.can_accelerate:
self.vertical_acceleration += 1
if self.vertical_acceleration > self.acceleration_maximum:
self.vertical_acceleration = self.acceleration_maximum
elif dirn == 4:
# stop is pressed
self.horizontal_velocity = 0
self.vertical_velocity = 0
self.horizontal_acceleration = 0
self.vertical_acceleration = 0
def draw(self, g, ):
# your player
pos = [self.x, self.y]
pygame.draw.circle(g, self.color, pos, self.radius)
# heathbar
health_length = 20
health_height = 3
up = 40
healthbar_vert = [self.x + health_length, (self.y - up) + health_height], [self.x - health_length,
(self.y - up) + health_height], [
self.x - health_length, (self.y - up) - health_height], [self.x + health_length,
(self.y - up) - health_height]
pygame.draw.polygon(g, Utility.COLORS['RED'], healthbar_vert)
# manabar
mana_length = 20
mana_height = 3
up = 32
manabar_vert = [self.x + mana_length, (self.y - up) + mana_height], [self.x - mana_length,
(self.y - up) + mana_height], [
self.x - mana_length, (self.y - up) - mana_height], [self.x + mana_length,
(self.y - up) - mana_height]
pygame.draw.polygon(g, Utility.COLORS['DEEPSKYBLUE'], manabar_vert)
pygame.draw.circle(g, Utility.COLORS['BLACK'], [int(self.target_x), int(self.target_y)], 5)