-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcharacter.py
More file actions
154 lines (131 loc) · 5.92 KB
/
Copy pathcharacter.py
File metadata and controls
154 lines (131 loc) · 5.92 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from setting import *
from common_class import Position
from weapons import RPG7
class Team:
def __init__(self, id, nb_viking):
self.id = id
self.vikings = []
for i in range(nb_viking):
self.vikings.append(Viking(i, image_path=f"images/player/Viking{id + 1}.png"))
class Viking:
def __init__(self, id, name="", health=100, image_path="", flipped=False):
self.falling = False
self.id = id
self.name = name
self.health = health
self.position = Position(0, 0)
self.flipped = flipped
self.jumping = False
self.isMoving = False
self.jump_height = 100
self.jump_velocity = 0
self.initialY = 0
self.rpg7_visible = False
self.raw_image = pygame.image.load(image_path)
self.image = pygame.transform.scale(self.raw_image, (int(self.raw_image.get_width() * SCALE_VIKING), int(self.raw_image.get_height() * SCALE_VIKING)))
self.image = pygame.transform.flip(self.image, True, False) if self.flipped else self.image
self.rect = self.image.get_rect()
def draw(self):
screen.blit(self.image, (self.position.x, self.position.y - self.rect.height))
def capXandY(self):
# cap x
if (self.position.x + int(self.rect.width / 2)) >= int(SCREEN_WIDTH / TILE_SIZE):
self.position.x = int(SCREEN_WIDTH / TILE_SIZE) - int(self.rect.width / 2) - 1
self.health = 0
if self.position.x + int(self.rect.width / 2) < 0:
self.position.x = 0 - int(self.rect.width / 2)
self.health = 0
#
# cap y
if self.position.y >= int(SCREEN_HEIGHT / TILE_SIZE) - 1:
self.position.y = int(SCREEN_HEIGHT / TILE_SIZE) - 2
self.health = 0
if self.position.y <= 0:
self.position.y = 1
self.health = 0
#
def doMath(self, map): # not meth
self.capXandY()
# falling calculations
if map[int(self.position.x + int(self.rect.width/2))][int(self.position.y+1)] == 0 and not self.jumping:
if not self.falling:
print('start falling')
self.setupFalling(self.position.y)
time = (pygame.time.get_ticks() - self.initialTime) / 1000
self.position.y = int((-0.5 * self.gravity) * (time * time) + (self.position.y * time) + self.initialY)
#
self.capXandY()
# go above the ground
while map[int(self.position.x + int(self.rect.width / 2))][int(self.position.y)] == 1:
self.position.y -= TILE_SIZE
#
self.capXandY()
# stop the fall
if map[int(self.position.x + int(self.rect.width / 2))][int(self.position.y + 1)] == 1 and self.falling and not self.jumping:
self.falling = False
print('stop falling')
def setupFalling(self, initialY):
self.falling = True
self.gravity = GRAVITY
self.initialY = initialY
self.initialTime = pygame.time.get_ticks()
# def shoot(self):
# if self.stock_rocket > 0:
# self.stock_rocket -= 1
# else:
# print(f"{self.name} has no rocket")
# def takeDamage(self, damage):
# self.health -= damage
# if self.health <= 0:
# print(f"{self.name} died")
# else:
# print(f"{self.name} has lost {damage} health")
# def reload(self):
# self.stock_rocket = 5
def getFlipped(self, flipped):
self.flipped = flipped
self.image = pygame.transform.flip(self.image, True, False)
def move(self, key, delta_time, timer, bitMap, rocketSelected):
left_movement = key[pygame.K_q]
right_movement = key[pygame.K_d]
take_rpg = key[pygame.K_UP]
self.isMoving = False
speed_x = self.position.speed_x
if not timer <= 0:
if left_movement:
self.position.x -= speed_x * delta_time
self.isMoving: True
if self.flipped:
self.getFlipped(False)
if right_movement:
self.position.x += speed_x * delta_time
self.isMoving: True
if not self.flipped:
self.getFlipped(True)
if key[pygame.K_SPACE] and not self.jumping and not self.falling:
self.isMoving: True
print(self.falling)
print('start jumping')
self.setupJump(self.position.y, -50)
if self.jumping:
self.isMoving: True
time = (pygame.time.get_ticks() - self.initialTime)/100
self.position.y = int((-0.5 * self.gravity) * (time * time) + (self.jump_velocity * time) + self.initialY)
if not int(self.position.x + self.image.get_width()/2) >= int(SCREEN_WIDTH/TILE_SIZE)-1 and not int(self.position.x + self.image.get_width()/2) < 0 and not int(self.position.y+2) >= int(SCREEN_HEIGHT/TILE_SIZE)-1 and not bitMap[int(self.position.x + self.image.get_width()/2)][int(self.position.y+2)] == 1:
self.thereWasGround = False
if not self.position.y > int(SCREEN_HEIGHT/TILE_SIZE) and not self.position.y < 0 and not self.thereWasGround:
if bitMap[int(self.position.x + self.image.get_width()/2)][int(self.position.y+2)] == 1:
print('stop jumping')
self.jumping = False
if rocketSelected:
self.draw_RPG7()
def draw_RPG7(self):
LaunchRPG7 = RPG7(self.position.x, self.position.y-40)
LaunchRPG7.draw()
def setupJump(self, initialY, jump_velocity):
self.thereWasGround = True
self.jumping = True
self.initialY = initialY
self.initialTime = pygame.time.get_ticks()
self.gravity = GRAVITY
self.jump_velocity = jump_velocity