-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.py
103 lines (88 loc) · 3.67 KB
/
Player.py
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
import pygame
# https://stackoverflow.com/questions/47391774/python-send-and-receive-objects-through-sockets/47396267
# surface objects cannot be sent across server with pickle
from util import make_vector, SCREEN_RECT
pygame.mixer.init()
wall_collision_sound = pygame.mixer.Sound('Assets/music/wall_hit.wav')
class Player:
def __init__(self, x, y, mode):
self.velocity = 2
self.mode = mode
self.rect = pygame.Rect(x, y, 55, 55)
self.sprite_index = 12
self.sprite_index_all = [0, 4, 8, 12] # left, right, up, down
# when sprite becomes stationary, the character index changes to 12. This variable is used for that
self.elapsed = 0
self.enemy_pos = (50, 50) # doesnt matter what it is, as when the server starts, it's going to be filled
# used since online one object of player class is being used to communicate trhough the server
self.game_over = False
self.victory = False
self.is_connected = False
self.is_start_clicked = False
# updates each frame in the sprite to create an animation
def update(self, dir):
if dir == "left":
self.sprite_index_all[0] += 1
if self.sprite_index_all[0] > 3:
self.sprite_index_all[0] = 0
self.sprite_index = self.sprite_index_all[0]
elif dir == "right":
self.sprite_index_all[1] += 1
if self.sprite_index_all[1] > 7:
self.sprite_index_all[1] = 4
self.sprite_index = self.sprite_index_all[1]
elif dir == "up":
self.sprite_index_all[2] += 1
if self.sprite_index_all[2] > 11:
self.sprite_index_all[2] = 8
self.sprite_index = self.sprite_index_all[2]
elif dir == "down":
self.sprite_index_all[3] += 1
if self.sprite_index_all[3] > 15:
self.sprite_index_all[3] = 12
self.sprite_index = self.sprite_index_all[3]
elif dir == "reset": # reset all pointers
self.sprite_index_all[0] = 0 # start of left sprite
self.sprite_index_all[1] = 4 # start of right sprite
self.sprite_index_all[2] = 8 # start of up sprite
self.sprite_index_all[3] = 12 # start of down sprite
self.sprite_index = 12
def move(self, dx, dy, walls):
# print("self.rect.pos: ", self.rect.x, self.rect.y)
self.rect.x += dx
self.rect.y += dy
for wall in walls:
if self.rect.colliderect(wall.rect):
self.rect.x -= dx
self.rect.y -= dy
wall_collision_sound.play()
# add code for boundary condition
self.rect.clamp_ip(SCREEN_RECT)
return make_vector((self.rect.x, self.rect.y))
# returns the final position of the player after calculating wall collisions in vector form
def player_movement(self, walls):
key = pygame.key.get_pressed()
dx = 0
dy = 0
if key[pygame.K_LEFT]:
dx = -self.velocity
self.update("left")
elif key[pygame.K_RIGHT]:
dx = self.velocity
self.update("right")
elif key[pygame.K_UP]:
dy = -self.velocity
self.update("up")
elif key[pygame.K_DOWN]:
dy = self.velocity
self.update("down")
else:
self.elapsed += 1
if self.elapsed > 5:
self.elapsed = 0
self.update("reset")
return self.move(dx, dy, walls)
def set_enemy_goal(self, pos):
self.enemy_goal = pos
def render(self, screen, img):
screen.blit(img, (self.rect.x, self.rect.y))