-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.py
160 lines (137 loc) · 6 KB
/
Board.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
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
155
156
157
158
159
160
import Room
import os, pygame
class Board:
def __init__(self, DISPLAYSURF, PLAYERIMAGES):
# Creates a brand new, blank board data structure
global boardimg, images
boardimg = pygame.image.load('gameboard.png')
images = {'G': PLAYERIMAGES[0], 'M': PLAYERIMAGES[1], 'S': PLAYERIMAGES[2],'W': PLAYERIMAGES[3], 'P': PLAYERIMAGES[4], 'C': PLAYERIMAGES[5]}
DISPLAYSURF.blit(boardimg, (0,0))
DISPLAYSURF.blit(PLAYERIMAGES[0], (295,650))
gstartx = 295
gstarty = 650
DISPLAYSURF.blit(PLAYERIMAGES[3], (605,650))
wstartx = 605
wstarty = 650
DISPLAYSURF.blit(PLAYERIMAGES[5], (20,450))
cstartx = 20
cstarty = 450
DISPLAYSURF.blit(PLAYERIMAGES[4], (20,230))
pstartx = 20
pstarty = 230
DISPLAYSURF.blit(PLAYERIMAGES[2], (605,20))
sstartx = 605
sstarty = 20
DISPLAYSURF.blit(PLAYERIMAGES[1], (900,230))
mstartx = 900
mstarty = 230
self.board = DISPLAYSURF
self.rooms = [Room.Room(roomType) for roomType in Room.RoomType]
self.weapons = [w.value for w in Room.WeaponType]
self.avatarposx = {'G': gstartx, 'W': wstartx, 'C': cstartx, 'P': pstartx, 'M': mstartx, 'S':sstartx}
self.avatarposy = {'G': gstarty, 'W': wstarty, 'C': cstarty, 'P': pstarty, 'M': mstarty, 'S':sstarty}
# MINUS 6 HERE - index 0 = -6 room type number enum
self.abs_pos = [
{'x': 125, 'y':130}, #STUDY = 0
{'x': 435, 'y':130}, #HALL = 1
{'x': 745, 'y':130}, #LOUNGE = 2
{'x': 125, 'y':332}, #LIBRARY = 3
{'x': 435, 'y':332}, #BILLIARD_ROOM = 4
{'x': 745, 'y':332}, #DINING_ROOM = 5
{'x': 125, 'y':547}, #CONSERVATORY = 6
{'x': 435, 'y':547}, #BALL_ROOM = 7
{'x': 745, 'y':547}, #KITCHEN = 8
{'x': 0, 'y':0}, #MAX_ROOM = 9
{'x': 300, 'y':95}, #H_STUDY_HALL = 10
{'x': 600, 'y':95}, #H_HALL_LOUNGE = 11
{'x': 150, 'y':200}, #H_STUDY_LIBRARY = 12
{'x': 450, 'y':200}, #H_HALL_BILLIARD_ROOM = 13
{'x': 750, 'y':200}, #H_LOUNGE_DINING_ROOM = 14
{'x': 300, 'y':305}, #H_LIBRARY_BILLIARD_ROOM = 15
{'x': 600, 'y':305}, #H_BILLIARD_ROOM_DINING_ROOM = 16
{'x': 150, 'y':410}, #H_LIBRARY_CONSERVATORY = 17
{'x': 450, 'y':410}, #H_BILLIARD_ROOM_BALL_ROOM = 18
{'x': 750, 'y':410}, #H_DINING_ROOM_KITCHEN = 19
{'x': 300, 'y':515}, #H_CONSERVATORY_BALL_ROOM = 20
{'x': 600, 'y':515} #H_BALL_ROOM_KITCHEN = 21
]
def draw(self, img, x,y):
self.board.blit(img, (x,y))
def updatePlayerLocationsOnBoard(self):
'''
updates the game board to properly display where the
players currently are
Works by making a list of the characters symbols and then padding to the desired size
'''
self.board.blit(boardimg,(0,0))
for room in self.rooms:
layer = 25
coords = self.abs_pos[room.getRoomType()]
x = coords['x']
y = coords['y']
players_inside = room.getPlayers()
if len(players_inside) > 1 and room.getRoomType() < Room.RoomType.MAX_ROOM:
x = x - layer
for player in players_inside:
self.avatarposx[str(player)] = x
self.avatarposy[str(player)] = y
if room.getRoomType() < Room.RoomType.MAX_ROOM:
x += layer
self.board.blit(images[str(player)], (self.avatarposx[str(player)] + 15, self.avatarposy[str(player)] + 40))
def getPlayerRoom(self, player):
for room in self.rooms:
if player in room.getPlayers():
return room
print('ERROR: Player was in none of the rooms')
exit()
def updatePlayerPos(self, player, room):
'''
This function simply puts the player in a new place without checking,
having assumed that the host checked before sending the message that
triggers this update
'''
curRoom = self.getPlayerRoom(player)
curRoom.removePlayer(player)
room = self.rooms[room]
room.addPlayer(player)
def movePlayer(self, player, action):
'''
This function validates a move and is generally only used by the host
'''
oldRoom = self.getPlayerRoom(player)
global allowedMoves, roomAdjacencies
# print(oldRoom.getRoomType())
canMove = True
newRoom = None # scope this
# move validation
if action not in Room.allowedMoves[oldRoom.getRoomType()]: # move is a valid direction?
canMove = False
else: # if it is valid direction
newRoom = self.rooms[Room.roomAdjacencies[oldRoom.getRoomType()][action]] #lol kinda disgusting but whatever
if newRoom.getRoomType() > Room.RoomType.MAX_ROOM and newRoom.getPlayers(): # is the hallway already occupied?
canMove = False
if canMove:
newRoom.addPlayer(player)
oldRoom.removePlayer(player)
return True
else:
# prints on server
# print("invalid move") # invalid move message - real message sent in Main.py
return False
def getWeapons(self):
'''
This function just returns a list of all weapons held by the Board instance.
'''
return self.weapons
def getRooms(self):
'''
This function just returns a list of all rooms held by the Board instance.
'''
return self.rooms
def getWeaponRoom(self, weapon):
for room in self.rooms:
#print(str(weapon) + " : " + str(room.getWeapons()))
if weapon in room.getWeapons():
return room
print('ERROR: Weapon was in none of the rooms')
exit()