-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcards.py
More file actions
383 lines (318 loc) · 14.2 KB
/
Copy pathcards.py
File metadata and controls
383 lines (318 loc) · 14.2 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# Classes for the different types of cards
# Current card types : Object, Enemy, Player, Setting, NPC
from pickletools import stackslice
import pygame as pg
import time
import random
class Card:
def __init__(self, game, card_image, name='Base Card', description='This is a base card', accepted_cards=None):
self.delete = False
self.game = game
self.settings = game.settings
self.screen = self.game.screen
self.name = name
self.description = description
self.card_image = card_image
self.rect = self.card_image.get_rect()
self.rect.clamp_ip(self.settings.play_area_rect)
self.accepted_cards = accepted_cards
# a list of what the card can interact with
self.targets = []
self.highlight_rect = pg.Rect(self.rect.x-5, self.rect.y-5, self.rect.width + 10, self.rect.height + 10)
self.highlight_color = (0, 150, 0)
self.flash_time_start = None
self.is_flashing = False
self.flash_alpha = 0
self.flash_tick = .05
self.flash_rect = pg.Surface((self.rect.width, self.rect.height))
self.flash_rect.fill(self.highlight_color)
self.flash_rect.set_alpha(0)
# moves the card when the user drags it around
# this is used by the createcards class
def drag(self):
self.rect.center = pg.mouse.get_pos()
self.rect.clamp_ip(self.settings.play_area_rect)
# highlight the borders around the card
def highlight(self, color=None):
self.highlight_rect.clamp_ip(self.rect)
if color == None:
pg.draw.rect(self.screen, self.highlight_color, self.highlight_rect, border_radius=10)
else: pg.draw.rect(self.screen, color, self.highlight_rect, border_radius=10)
# colors the entire card
def flash_card(self, color):
self.flash_rect.fill(color)
self.is_flashing = True
self.flash_time_start = time.time()
self.flash_alpha = 256
self.flash_rect.set_alpha(256)
# reduces the alpha of the flash over time until it reaches 0
def reduce_flash(self):
if self.is_flashing == True:
t_delta = time.time() - self.flash_time_start
if t_delta > self.flash_tick:
self.flash_time_start += self.flash_tick
self.flash_alpha -= 51.2
self.flash_rect.set_alpha(self.flash_alpha)
else:
self.flashing = False
def __repr__(self):
return repr(str(type(self)) + ' ' + self.name + str(self.rect.center))
def draw(self):
self.screen.blit(self.card_image, (self.rect.x, self.rect.y))
if self.is_flashing:
self.screen.blit(self.flash_rect, (self.rect.x, self.rect.y))
def update(self):
self.reduce_flash()
self.draw()
class PlayerCard(Card):
def __init__(self, game, card_image, name, description):
super().__init__(game, card_image, name, description)
self.max_health = 10
self.max_damage = 5
self.health = 10
self.damage = 5
# player attacks the given card
def attack(self, card):
enemy_damage = card.damage
card.take_damage(self.damage)
if self.damage > 1: self.damage -= 1
self.health -= enemy_damage
if self.health <= 0:
self.health = 0
self.die()
self.flash_card((255, 0, 0))
# keeps health/damage stats within max boundaries
def consume_item(self, card):
if type(card) == ConsumableCard:
if self.health + card.health >= self.max_health:
self.health = 10
elif self.max_health + card.health <= 0:
self.health = 0
self.die()
else: self.health += card.health
if self.damage + card.damage >= self.max_damage:
self.damage = 5
elif self.damage + card.damage <= 1:
self.damage = 1
else: self.damage += card.damage
card.consume()
self.flash_card((0, 0, 255))
# this is called whenever player health reaches 0
def die(self):
pg.quit()
quit()
def display_health(self):
y_val = 118
for _ in range(self.health):
pg.draw.rect(self.screen, (255, 0, 0), (self.rect.x-8, self.rect.y+y_val, 6, 9))
y_val -= 13
def display_attack(self):
y_val = 118
for _ in range(self.damage):
pg.draw.rect(self.screen, (0, 0, 255), (self.rect.x-16, self.rect.y+y_val, 6, 9))
y_val -= 13
def update(self):
super().update()
self.display_health()
self.display_attack()
# The accepted_cards variable is a dict for this class - the value of dict is what the NPC gives for the given key of the dict
class NPCCard(Card):
def __init__(self, game, card_image, accepted_cards, return_card, name, description):
super().__init__(game, card_image, name, description, accepted_cards)
self.return_card = return_card
def spawn_card(self, card):
if card in self.accepted_cards:
copy = self.return_card[card.name]
return_copy = ConsumableCard(self.game, copy.card_image, copy.name,
copy.description, copy.accepted_cards, copy.health, copy.damage, copy.transform_to)
return_copy.rect.center = (self.rect.x + self.rect.width / 2, self.rect.y + 190)
return_copy.rect.y += 20
self.game.cards.update_list.append(return_copy)
self.game.cards.all_cards.append(return_copy)
self.flash_card((255, 255, 255))
def update_accepted_cards(self):
for card in self.accepted_cards:
for element in self.game.cards.update_list:
if type(element) == ConsumableCard:
if card.name == element.name and element not in self.accepted_cards:
self.accepted_cards.append(element)
def consume_item(self, card):
self.spawn_card(card)
card.consume()
def update(self):
super().update()
self.update_accepted_cards()
class EnemyCard(Card):
def __init__(self, game, card_image, hp, dmg, accepted_cards, name, description, loot_drop_chance, loot_cards, points):
super().__init__(game, card_image, name, description, accepted_cards)
self.health = hp
self.damage = dmg
self.highlight_color = (150, 0, 0)
self.loot_drop_chance = loot_drop_chance
self.loot_cards = loot_cards
self.points = points
def take_damage(self, damage):
self.health -= damage
if self.health <= 0:
self.health = 0
self.die()
else:
self.flash_card((255, 0, 0))
def display_stats(self):
y_val = 118
for _ in range(self.damage):
pg.draw.rect(self.screen, (0, 0, 255), (self.rect.x+106, self.rect.y+y_val, 6, 9))
y_val -= 13
y_val = 118
for _ in range(self.health):
pg.draw.rect(self.screen, (255, 0, 0), (self.rect.x+98, self.rect.y+y_val, 6, 9))
y_val -= 13
# checks if card is dead or not
def die(self):
self.drop_loot()
card_stack = self.game.cards.is_in_stack(self)
card_stack.remove_card(self)
self.game.cards.all_cards.remove(self)
self.game.cards.update_list.remove(self)
self.game.scoreboard.update_points(self.points)
# chooses a loot card to drop based on probabilities
# return None when no card is chosen
def choose_loot(self):
if self.loot_cards != None:
for card in self.loot_cards:
roll = random.random()
if roll < self.loot_drop_chance[self.loot_cards.index(card)]:
return card
else: return None
else: return None
def drop_loot(self):
card = self.choose_loot()
if card != None:
copy = ConsumableCard(self.game ,card.card_image, card.name, card.description, card.accepted_cards, card.health, card.damage, card.transform_to)
copy.rect.center = (self.rect.x + self.rect.width / 2, self.rect.y + 190)
copy.rect.y += 20
self.game.cards.update_list.append(copy)
self.game.cards.all_cards.append(copy)
# when other goblin cards are made, add them to the accepted_cards list
def update_accepted_cards(self):
for card in self.game.cards.update_list:
if type(card) == EnemyCard and card.name == self.name:
if card not in self.accepted_cards:
self.accepted_cards.append(card)
def update(self):
super().update()
self.update_accepted_cards()
# event_card param : list of cards that are to be spawned upon trigger_event()
# only duplicates of these cards are created,
# event_spawn_chance: list of percentages to spawn cards
# accepted cards param : list of cards that can interact with this card
# duration param : seconds to run progress bar
class SettingCard(Card):
def __init__(self, game ,card_image, name='Setting Card', description='This is a setting card',
duration = 1, accepted_cards = None, event_cards = None, event_spawn_chance = None, max_card_spawns=0):
super().__init__(game, card_image, name, description, accepted_cards)
# list of cards that can interact with this card
self.event_cards = event_cards
self.event_spawn_chance = event_spawn_chance
self.rect.x = 100
self.rect.y = 100
self.game = game
self.settings = game.settings
self.card_size = self.settings.card_size
self.bar_color = (0, 0, 255)
self.max_card_spawns = max_card_spawns
self.spawned_cards = []
self.duration = duration
self.start_time = None
self.time_progress = 0
self.progress_rect = pg.Rect(self.rect.x, self.rect.y - 10, 0, 5)
self.active = False
def progress_bar(self):
if self.active == False and self.time_progress != 0:
self.time_progress = 0
self.start_time = None
self.progress_rect.width = 0
elif self.active:
self.progress_rect.x = self.rect.x
self.progress_rect.y = self.rect.y - 10
if self.start_time == None: self.start_time = time.time()
current_time = time.time()
if self.max_card_spawns > len(self.spawned_cards):
if current_time - self.start_time > self.time_progress and self.time_progress < self.duration:
self.progress_rect.width += self.rect.width / self.duration
self.time_progress += 1
elif self.time_progress == self.duration:
self.start_time = None
self.time_progress = 0
self.trigger_event()
self.progress_rect.width = 0
else:
self.active = False
self.start_time = None
pg.draw.rect(self.screen, self.bar_color, self.progress_rect, 5)
# activate the setting card
def activate(self, activate = True):
self.active = activate
# runs the probabilites within event_spawn_chance and returns the card chosen
# make sure the card probabilities are ordered in ascending order
def choose_event(self):
roll = random.random()
for chance in self.event_spawn_chance:
if roll < chance:
return self.event_cards[self.event_spawn_chance.index(chance)]
else: return self.event_cards[-1]
# spawns cards in event_cards into play according to percent chances
def trigger_event(self):
card = self.choose_event()
copy = None
if type(card) == EnemyCard:
copy = EnemyCard(self.game, card.card_image, card.health, card.damage, card.accepted_cards, card.name,
card.description, card.loot_drop_chance, card.loot_cards, card.points)
elif type(card) == ConsumableCard:
copy = ConsumableCard(self.game, card.card_image, card.name, card.description, card.accepted_cards,card.health,card.damage)
if copy:
copy.rect.center = (self.rect.x + self.rect.width / 2, self.rect.y + 190)
copy.rect.y += 20
self.game.cards.update_list.append(copy)
self.game.cards.all_cards.append(copy)
self.spawned_cards.append(copy)
self.flash_card((255, 255, 255))
# keeps track of whether the cards spawned are despawned or not
def check_spawned_cards(self):
for card in self.spawned_cards:
if card not in self.game.cards.update_list and not self.game.cards.is_in_stack(card):
self.spawned_cards.remove(card)
def update(self):
super().update()
self.progress_bar()
self.check_spawned_cards()
class ConsumableCard(Card):
def __init__(self, game ,card_image, name, description,
accepted_cards = [], health = 0, damage = 0, transform_to = None):
super().__init__(game, card_image, name, description, accepted_cards)
self.health = health
self.damage = damage
self.transform_to = transform_to
# remove this card from play
def consume(self):
if self.transform_to != None:
self.transform()
card_stack = self.game.cards.is_in_stack(self)
card_stack.remove_card(self)
self.game.cards.all_cards.remove(self)
self.game.cards.update_list.remove(self)
# delete this card and spawn the transform_to card in its place
def transform(self):
self.game.cards.all_cards.append(self.transform_to)
self.game.cards.update_list.append(self.transform_to)
self.transform_to.rect.x = self.rect.x
self.transform_to.rect.y = self.rect.y - 20
# allows same cards to stack
def update_accepted_cards(self):
for card in self.game.cards.update_list:
if type(card) == ConsumableCard and card.name == self.name:
if card not in self.accepted_cards:
self.accepted_cards.append(card)
def update(self):
super().update()
self.update_accepted_cards()