-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
218 lines (200 loc) · 8.41 KB
/
Copy pathplayer.py
File metadata and controls
218 lines (200 loc) · 8.41 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from tkinter import *
#from networktest import *
# cmu_112_graphics cited from
# http://www.cs.cmu.edu/~112/notes/notes-animations-part1.html
from cmu_112_graphics import *
import random
from card import Card
from utilities import *
#player class stores all the data about the player, and makes it easy to pass that data to the server
class Player(object):
enemyBoxDims = 300, 0, 600, 100
manaMax = 10
cardWidth = 100
def __init__(self):
self.health = 20
self.currentPlayer = True
self.mana = 1
self.currMana = 1
self.firstTurn = True #used to prevent first the second player from starting with two mana and drawing
self.hand = []
self.deck = []
self.discard = []
self.board = []
self.makeBasicDeck()
self.message = 'connecting' #message to be displayed to the player at a given time
self.gameStarted = False
random.shuffle(self.deck)
self.opponentImage = None #only the opponent is visualized, so this all that needs to be known
def draw(self, canvas, width, height, side):
#Draw Hand
self.drawHand(canvas, side)
self.drawBoard(canvas, side)
self.drawHud(canvas, width, height, side)
#Draw Deck
canvas.create_rectangle(675, 575, 775, 725, fill = 'black', outline = 'cyan', width = 6)
#TODO update
deckTextTop = getCustomFontText('Deck', 30, 'cyan')
deckTextBottom = getCustomFontText(str(len(self.deck)), 30, 'cyan')
canvas.create_image(720, 600, image = getCachedPhotoImage(deckTextTop))
canvas.create_image(720, 630, image = getCachedPhotoImage(deckTextBottom))
def drawHand(self, canvas, side):
X, Y = 100, 725
if side == 0: #main player
for card in self.hand:
if not card.selected:
card.x = X
card.y = Y
card.drawCard(canvas, Player.cardWidth, 'cyan')
X += 100
def drawBoard(self, canvas, side):
X, Y = 100, 425
if side == 0: #main player
for card in self.board:
if not card.selected:
card.x = X
card.y = Y
card.drawCard(canvas, Player.cardWidth, 'cyan')
X += 150
X, Y = 100, 200
if side == 1: # opponent
for card in self.board:
if not card.selected:
card.x = X
card.y = Y
card.drawCard(canvas, Player.cardWidth, 'magenta')
X += 150
canvas.create_rectangle(Player.enemyBoxDims[0], Player.enemyBoxDims[1],
Player.enemyBoxDims[2], Player.enemyBoxDims[3],
fill = 'black', outline = 'magenta', width = 6)
enemyX = Player.enemyBoxDims[2] - Player.enemyBoxDims[0]
enemyY = Player.enemyBoxDims[3] - Player.enemyBoxDims[1]
canvas.create_image(500, 50, image = getCachedPhotoImage(self.opponentImage))
def drawHud(self, canvas, width, height, side):
if side == 0:
messageText = getCustomFontText(self.message, 25, 'cyan')
healthText = getCustomFontText(f'LIFE {self.health}', 25, 'cyan')
manaText = getCustomFontText(f'MANA {self.currMana}', 25, 'cyan')
canvas.create_image(30, 575, image = getCachedPhotoImage(messageText), anchor = 'nw')
canvas.create_image(637, 525, image = getCachedPhotoImage(healthText), anchor = 'nw')
canvas.create_image(637, 500, image = getCachedPhotoImage(manaText), anchor = 'nw')
if side == 1:
healthTextOpponnent = getCustomFontText(f'LIFE {self.health}', 25, 'magenta')
manaTextOpponent = getCustomFontText(f'MANA {self.currMana}', 25, 'magenta')
canvas.create_image(330, 13, image = getCachedPhotoImage(healthTextOpponnent), anchor = 'nw')
canvas.create_image(330, 70, image = getCachedPhotoImage(manaTextOpponent), anchor = 'nw')
def makeBasicDeck(self):
for i in range(3):
self.deck.append(Card('Newt', 1, (2,1), 'Rush'))
for i in range(4):
self.deck.append(Card('Pally', 1, (2,2), 'DivineShield'))
for i in range(4):
self.deck.append(Card('Minion', 2, (3,3), 'Taunt'))
for i in range(3):
self.deck.append(Card('Hound', 2, (1,8), 'Poison'))
for i in range(4):
self.deck.append(Card('Beast', 4, (8,8), 'QuickStrike'))
for i in range(2):
self.deck.append(Card('Dragon', 10, (12,12), None))
#reanamed to avoid confusion with draw methods
def pickupCard(self):
if len(self.deck) > 0:
self.hand.append(self.deck.pop())
return 1
return 0
def clearBoard(self):
removeList = []
index = 0
while index < len(self.board):
if self.board[index].curLife <= 0:
removeList.append(self.board[index].name)
self.discard.append(self.board[index])
self.board.remove(self.board[index])
else:
index += 1
if len(removeList) > 0:
removeString = ', '.join(removeList) + ' Died'
self.message = removeString
def selectCard(self, x, y):
#remove current selection
for card in self.hand:
card.selected = False
for card in self.board:
card.selected = False
#make new selection
halfWidth, halfHeight = 50, 100
for card in self.hand:
if card.x + halfWidth > x and card.x - halfWidth < x:
if card.y + halfHeight > y and card.y - halfHeight < y:
card.selected = True
for card in self.board:
if card.x + halfWidth > x and card.x - halfWidth < x:
if card.y + halfHeight > y and card.y - halfHeight < y:
card.selected = True
def getSelected(self):
selection = None
for card in self.hand:
if card.selected == True:
selection = card
break
for card in self.board:
if card.selected == True:
selection = card
break
return selection
def buildPlayerFromString(self, string):
components = string.split(';')
self.health = int(components[0])
self.currentPlayer = (components[1] == 'True')
self.mana = int(components[2])
self.currMana = int(components[3])
self.firstTurn = (components[4] == 'True')
self.message = components[6]
self.gameStarted = components[5]
self.deck = Player.buildZoneFromString(components[7])
self.hand = Player.buildZoneFromString(components[8])
self.board = Player.buildZoneFromString(components[9])
self.discard = Player.buildZoneFromString(components[10])
def buildDeckFromString(self, string):
self.deck = Player.buildZoneFromString(string)
@staticmethod
def buildZoneFromString(string):
if string == '':
return []
zoneString = string.split(',')
zoneList = []
for cardString in zoneString:
card = Card()
card.createCardFromString(cardString)
zoneList.append(card)
return zoneList
def __repr__(self):
deck = ''
for card in self.deck:
deck = deck + str(card) + ','
if deck != '':
deck = deck[0:-1] #remove last comma
hand = ''
for card in self.hand:
hand = hand + str(card) + ','
if hand != '':
hand = hand[0:-1]
board = ''
for card in self.board:
board = board + str(card) + ','
if board != '':
board = board[0:-1]
discard = ''
for card in self.discard:
discard = discard + str(card) + ','
if discard != '':
discard = discard[0:-1]
cards = deck + ';' + hand + ';' + board + ';' + discard + ';'
rep = (str(self.health) + ';' +
str(self.currentPlayer) + ';' +
str(self.mana) + ';' +
str(self.currMana) + ';' +
str(self.firstTurn) + ';' +
str(self.gameStarted) + ';' +
str(self.message) + ';' + cards)
return rep