all repos — Legends-RPG @ d42cfd5a960d68a90490ac950b309d27b477ce30

A fantasy mini-RPG built with Python and Pygame.

data/shopgui.py (view raw)

  1"""
  2This class controls the textbox GUI for any shop state.
  3A Gui object is created and updated by the shop state.
  4"""
  5
  6import pygame as pg
  7from . import setup
  8from . components import textbox
  9from . import constants as c
 10
 11
 12class Gui(object):
 13    """Class that controls the GUI of the shop state"""
 14    def __init__(self, level):
 15        self.level = level
 16        self.game_data = self.level.game_data
 17        self.level.game_data['last direction'] = 'down'
 18        self.sellable_items = level.sell_items
 19        self.player_inventory = level.game_data['player inventory']
 20        self.name = level.name
 21        self.state = 'dialogue'
 22        self.no_selling = ['Inn', 'Magic Shop']
 23        self.font = pg.font.Font(setup.FONTS[c.MAIN_FONT], 22)
 24        self.index = 0
 25        self.timer = 0.0
 26        self.allow_input = False
 27        self.items = level.items
 28        self.item_to_be_sold = None
 29        self.item_to_be_purchased = None
 30        self.dialogue = level.dialogue
 31        self.accept_dialogue = level.accept_dialogue
 32        self.accept_sale_dialogue = level.accept_sale_dialogue
 33        self.arrow = textbox.NextArrow()
 34        self.selection_arrow = textbox.NextArrow()
 35        self.arrow_pos1 = (50, 475)
 36        self.arrow_pos2 = (50, 515)
 37        self.arrow_pos3 = (50, 555)
 38        self.arrow_pos4 = (50, 495)
 39        self.arrow_pos5 = (50, 535)
 40        self.arrow_pos_list = [self.arrow_pos1, self.arrow_pos2, self.arrow_pos3]
 41        self.two_arrow_pos_list = [self.arrow_pos4, self.arrow_pos5]
 42        self.arrow_index = 0
 43        self.selection_arrow.rect.topleft = self.arrow_pos1
 44        self.dialogue_box = self.make_dialogue_box(self.dialogue, self.index)
 45        self.gold_box = self.make_gold_box()
 46        if self.name in self.no_selling:
 47            choices = self.items[0]['dialogue']
 48        else:
 49            choices = ['Buy', 'Sell', 'Leave']
 50        self.selection_box = self.make_selection_box(choices)
 51        self.state_dict = self.make_state_dict()
 52
 53
 54
 55    def make_dialogue_box(self, dialogue_list, index):
 56        """Make the sprite that controls the dialogue"""
 57        image = setup.GFX['dialoguebox']
 58        rect = image.get_rect()
 59        surface = pg.Surface(rect.size)
 60        surface.set_colorkey(c.BLACK)
 61        surface.blit(image, rect)
 62        dialogue = self.font.render(dialogue_list[index],
 63                                    True,
 64                                    c.NEAR_BLACK)
 65        dialogue_rect = dialogue.get_rect(left=50, top=50)
 66        surface.blit(dialogue, dialogue_rect)
 67        sprite = pg.sprite.Sprite()
 68        sprite.image = surface
 69        sprite.rect = rect
 70        self.check_to_draw_arrow(sprite)
 71
 72        return sprite
 73
 74
 75    def check_to_draw_arrow(self, sprite):
 76        """Blink arrow if more text needs to be read"""
 77        if self.index < len(self.dialogue) - 1:
 78            sprite.image.blit(self.arrow.image, self.arrow.rect)
 79
 80
 81    def make_gold_box(self):
 82        """Make the box to display total gold"""
 83        image = setup.GFX['goldbox']
 84        rect = image.get_rect(bottom=608, right=800)
 85
 86        surface = pg.Surface(rect.size)
 87        surface.set_colorkey(c.BLACK)
 88        surface.blit(image, (0, 0))
 89        gold = self.player_inventory['GOLD']['quantity']
 90        text = 'Gold: ' + str(gold)
 91        text_render = self.font.render(text, True, c.NEAR_BLACK)
 92        text_rect = text_render.get_rect(x=80, y=60)
 93
 94        surface.blit(text_render, text_rect)
 95
 96        sprite = pg.sprite.Sprite()
 97        sprite.image = surface
 98        sprite.rect = rect
 99
100        return sprite
101
102
103    def make_selection_box(self, choices):
104        """Make the box for the player to select options"""
105        image = setup.GFX['shopbox']
106        rect = image.get_rect(bottom=608)
107
108        surface = pg.Surface(rect.size)
109        surface.set_colorkey(c.BLACK)
110        surface.blit(image, (0, 0))
111
112        if len(choices) == 2:
113            choice1 = self.font.render(choices[0], True, c.NEAR_BLACK)
114            choice1_rect = choice1.get_rect(x=200, y=35)
115            choice2 = self.font.render(choices[1], True, c.NEAR_BLACK)
116            choice2_rect = choice2.get_rect(x=200, y=75)
117
118            surface.blit(choice1, choice1_rect)
119            surface.blit(choice2, choice2_rect)
120
121        elif len(choices) == 3:
122            choice1 = self.font.render(choices[0], True, c.NEAR_BLACK)
123            choice1_rect = choice1.get_rect(x=200, y=15)
124            choice2 = self.font.render(choices[1], True, c.NEAR_BLACK)
125            choice2_rect = choice2.get_rect(x=200, y=55)
126            choice3 = self.font.render(choices[2], True, c.NEAR_BLACK)
127            choice3_rect = choice3.get_rect(x=200, y=95)
128
129            surface.blit(choice1, choice1_rect)
130            surface.blit(choice2, choice2_rect)
131            surface.blit(choice3, choice3_rect)
132
133        sprite = pg.sprite.Sprite()
134        sprite.image = surface
135        sprite.rect = rect
136
137        return sprite
138
139
140    def make_state_dict(self):
141        """Make the state dictionary for the GUI behavior"""
142        state_dict = {'dialogue': self.control_dialogue,
143                      'select': self.make_selection,
144                      'confirmpurchase': self.confirm_purchase,
145                      'confirmsell': self.confirm_sell,
146                      'reject': self.reject_insufficient_gold,
147                      'accept': self.accept_purchase,
148                      'acceptsell': self.accept_sale,
149                      'hasitem': self.has_item,
150                      'buysell': self.buy_sell,
151                      'sell': self.sell_items,
152                      'cantsell': self.cant_sell}
153
154        return state_dict
155
156
157    def control_dialogue(self, keys, current_time):
158        """Control the dialogue boxes"""
159        self.dialogue_box = self.make_dialogue_box(self.dialogue, self.index)
160
161        if self.index < (len(self.dialogue) - 1) and self.allow_input:
162            if keys[pg.K_SPACE]:
163                self.index += 1
164                self.allow_input = False
165
166                if self.index == (len(self.dialogue) - 1):
167                    self.state = self.begin_new_transaction()
168
169        if not keys[pg.K_SPACE]:
170            self.allow_input = True
171
172
173    def begin_new_transaction(self):
174        """Set state to buysell or select, depending if the shop
175        is a Inn/Magic shop or not"""
176        if self.level.name in self.no_selling:
177            state = 'select'
178        else:
179            state = 'buysell'
180
181        return state
182
183
184    def make_selection(self, keys, current_time):
185        """Control the selection"""
186        choices = []
187        for item in self.items:
188            choices.append(item['dialogue'])
189        if self.name in self.no_selling:
190            choices.append('Leave')
191        else:
192            choices.append('Cancel')
193        self.dialogue_box = self.make_dialogue_box(self.dialogue, self.index)
194        self.selection_box = self.make_selection_box(choices)
195        self.gold_box = self.make_gold_box()
196
197        if len(choices) == 2:
198            arrow_list = self.two_arrow_pos_list
199        elif len(choices) == 3:
200            arrow_list = self.arrow_pos_list
201        else:
202            arrow_list = None
203            AssertionError('Only two items supported')
204
205        self.selection_arrow.rect.topleft = arrow_list[self.arrow_index]
206
207
208        if keys[pg.K_DOWN] and self.allow_input:
209            if self.arrow_index < (len(choices) - 1):
210                self.arrow_index += 1
211                self.allow_input = False
212        elif keys[pg.K_UP] and self.allow_input:
213            if self.arrow_index > 0:
214                self.arrow_index -= 1
215                self.allow_input = False
216        elif keys[pg.K_SPACE] and self.allow_input:
217            if self.arrow_index == 0:
218                self.state = 'confirmpurchase'
219                self.item_to_be_purchased = self.items[0]
220
221            elif self.arrow_index == 1 and len(choices) == 3:
222                self.state = 'confirmpurchase'
223                self.item_to_be_purchased = self.items[1]
224
225            else:
226                if self.level.name in self.no_selling:
227                    self.level.done = True
228                    self.game_data['last state'] = self.level.name
229                else:
230                    self.state = 'buysell'
231
232            self.arrow_index = 0
233            self.allow_input = False
234
235        if not keys[pg.K_SPACE] and not keys[pg.K_UP] and not keys[pg.K_DOWN]:
236            self.allow_input = True
237
238
239
240    def confirm_purchase(self, keys, current_time):
241        """Confirm selection state for GUI"""
242        dialogue = ['Are you sure?']
243        choices = ['Yes', 'No']
244        self.selection_box = self.make_selection_box(choices)
245        self.gold_box = self.make_gold_box()
246        self.dialogue_box = self.make_dialogue_box(dialogue, 0)
247        self.selection_arrow.rect.topleft = self.two_arrow_pos_list[self.arrow_index]
248
249        if keys[pg.K_DOWN] and self.allow_input:
250            if self.arrow_index < (len(choices) - 1):
251                self.arrow_index += 1
252                self.allow_input = False
253        elif keys[pg.K_UP] and self.allow_input:
254            if self.arrow_index > 0:
255                self.arrow_index -= 1
256                self.allow_input = False
257        elif keys[pg.K_SPACE] and self.allow_input:
258            if self.arrow_index == 0:
259                self.buy_item()
260            elif self.arrow_index == 1:
261                self.state = self.begin_new_transaction()
262            self.arrow_index = 0
263            self.allow_input = False
264
265        if not keys[pg.K_SPACE] and not keys[pg.K_DOWN] and not keys[pg.K_UP]:
266            self.allow_input = True
267
268
269    def buy_item(self):
270        """Attempt to allow player to purchase item"""
271        item = self.item_to_be_purchased
272
273        self.player_inventory['GOLD']['quantity'] -= item['price']
274
275        if self.player_inventory['GOLD']['quantity'] < 0:
276            self.player_inventory['GOLD']['quantity'] += item['price']
277            self.state = 'reject'
278        else:
279            if (item['type'] in self.player_inventory
280                and self.name == 'Magic Shop'):
281                    self.state = 'hasitem'
282                    self.player_inventory['GOLD']['quantity'] += item['price']
283            else:
284                self.state = 'accept'
285                self.add_player_item(item)
286
287
288    def add_player_item(self, item):
289        """Add item to player's inventory"""
290        item_type = item['type']
291        quantity = item['quantity']
292        value = item['price']
293        player_items = self.level.game_data['player inventory']
294        player_health = self.level.game_data['player stats']['Health']
295        player_magic = self.level.game_data['player stats']['Magic Points']
296
297        item_to_add = {'quantity': quantity,
298                       'value': value}
299
300        if item_type in player_items:
301            player_items[item_type]['quantity'] += quantity
302        elif quantity > 0:
303            player_items[item_type] = item_to_add
304        elif item_type == 'room':
305            player_health['current'] = player_health['maximum']
306            player_magic['current'] = player_magic['maximum']
307
308
309    def confirm_sell(self, keys, current_time):
310        """Confirm player wants to sell item"""
311        dialogue = ['Are you sure?']
312        choices = ['Yes', 'No']
313        self.dialogue_box = self.make_dialogue_box(dialogue, 0)
314        self.selection_box = self.make_selection_box(choices)
315        self.selection_arrow.rect.topleft = self.two_arrow_pos_list[self.arrow_index]
316
317        if keys[pg.K_DOWN] and self.allow_input:
318            if self.arrow_index < (len(choices) - 1):
319                self.arrow_index += 1
320                self.allow_input = False
321        elif keys[pg.K_UP] and self.allow_input:
322            if self.arrow_index > 0:
323                self.arrow_index -= 1
324                self.allow_input = False
325        elif keys[pg.K_SPACE] and self.allow_input:
326            if self.arrow_index == 0:
327                self.sell_item_from_inventory()
328            elif self.arrow_index == 1:
329                self.state = self.begin_new_transaction()
330            self.allow_input = False
331            self.arrow_index = 0
332
333        if not keys[pg.K_SPACE] and not keys[pg.K_UP] and not keys[pg.K_DOWN]:
334            self.allow_input = True
335
336
337    def sell_item_from_inventory(self):
338        """Allow player to sell item to shop"""
339        item_price = self.item_to_be_sold['price']
340        item_name = self.item_to_be_sold['type']
341        self.player_inventory['gold'] += (item_price / 2)
342        self.state = 'acceptsell'
343        if self.player_inventory[item_name]['quantity'] > 1:
344            self.player_inventory[item_name]['quantity'] -= 1
345        else:
346            del self.player_inventory[self.item_to_be_sold['type']]
347
348
349    def reject_insufficient_gold(self, keys, current_time):
350        """Reject player selection if they do not have enough gold"""
351        dialogue = ["You don't have enough gold!"]
352        self.dialogue_box = self.make_dialogue_box(dialogue, 0)
353
354        if keys[pg.K_SPACE] and self.allow_input:
355            self.state = self.begin_new_transaction()
356            self.selection_arrow.rect.topleft = self.arrow_pos1
357            self.allow_input = False
358
359        if not keys[pg.K_SPACE]:
360            self.allow_input = True
361
362
363    def accept_purchase(self, keys, current_time):
364        """Accept purchase and confirm with message"""
365        self.dialogue_box = self.make_dialogue_box(self.accept_dialogue, 0)
366        self.gold_box = self.make_gold_box()
367
368        if keys[pg.K_SPACE] and self.allow_input:
369            self.state = self.begin_new_transaction()
370            self.selection_arrow.rect.topleft = self.arrow_pos1
371            self.allow_input = False
372
373        if not keys[pg.K_SPACE]:
374            self.allow_input = True
375
376
377    def accept_sale(self, keys, current_time):
378        """Confirm to player that item was sold"""
379        self.dialogue_box = self.make_dialogue_box(self.accept_sale_dialogue, 0)
380        self.gold_box = self.make_gold_box()
381
382        if keys[pg.K_SPACE] and self.allow_input:
383            self.state = self.begin_new_transaction()
384            self.selection_arrow.rect.topleft = self.arrow_pos1
385            self.allow_input = False
386
387        if not keys[pg.K_SPACE]:
388            self.allow_input = True
389
390
391    def has_item(self, keys, current_time):
392        """Tell player he has item already"""
393        dialogue = ["You have that item already."]
394        self.dialogue_box = self.make_dialogue_box(dialogue, 0)
395
396        if keys[pg.K_SPACE] and self.allow_input:
397            self.state = self.begin_new_transaction()
398            self.selection_arrow.rect.topleft = self.arrow_pos1
399            self.allow_input = False
400
401        if not keys[pg.K_SPACE]:
402            self.allow_input = True
403
404
405    def buy_sell(self, keys, current_time):
406        """Ask player if they want to buy or sell something"""
407        dialogue = ["Would you like to buy or sell an item?"]
408        choices = ['Buy', 'Sell', 'Leave']
409        self.dialogue_box = self.make_dialogue_box(dialogue, 0)
410        self.selection_box = self.make_selection_box(choices)
411        self.selection_arrow.rect.topleft = self.arrow_pos_list[self.arrow_index]
412
413        if keys[pg.K_DOWN] and self.allow_input:
414            if self.arrow_index < (len(self.arrow_pos_list) - 1):
415                self.arrow_index += 1
416                self.allow_input = False
417
418        elif keys[pg.K_UP] and self.allow_input:
419            if self.arrow_index > 0:
420                self.arrow_index -= 1
421                self.allow_input = False
422        elif keys[pg.K_SPACE] and self.allow_input:
423            if self.arrow_index == 0:
424                self.state = 'select'
425                self.allow_input = False
426                self.arrow_index = 0
427            elif self.arrow_index == 1:
428                if self.check_for_sellable_items():
429                    self.state = 'sell'
430                    self.allow_input = False
431                    self.arrow_index = 0
432                else:
433                    self.state = 'cantsell'
434                    self.allow_input = False
435                    self.arrow_index = 0
436
437            else:
438
439                self.level.done = True
440                self.game_data['last state'] = self.level.name
441
442            self.arrow_index = 0
443
444        if not keys[pg.K_SPACE] and not keys[pg.K_DOWN] and not keys[pg.K_UP]:
445            self.allow_input = True
446
447
448    def check_for_sellable_items(self):
449        """Check for sellable items"""
450        for item in self.player_inventory:
451            if item in self.sellable_items:
452                return True
453        else:
454            return False
455
456
457    def sell_items(self, keys, current_time):
458        """Have player select items to sell"""
459        dialogue = ["What would you like to sell?"]
460        choices = []
461        item_list = []
462        for item in self.items:
463            if item['type'] in self.player_inventory:
464                name = item['type']
465                price = " (" + str(item['price'] / 2) + " gold)"
466                choices.append(name + price)
467                item_list.append(name)
468        choices.append('Cancel')
469        self.dialogue_box = self.make_dialogue_box(dialogue, 0)
470        self.selection_box = self.make_selection_box(choices)
471
472        if len(choices) == 2:
473            self.selection_arrow.rect.topleft = self.two_arrow_pos_list[self.arrow_index]
474        elif len(choices) == 3:
475            self.selection_arrow.rect.topleft = self.arrow_pos_list[self.arrow_index]
476
477        if keys[pg.K_DOWN] and self.allow_input:
478            if self.arrow_index < (len(self.arrow_pos_list) - 1):
479                self.arrow_index += 1
480                self.allow_input = False
481        elif keys[pg.K_UP] and self.allow_input:
482            if self.arrow_index > 0:
483                self.arrow_index -= 1
484                self.allow_input = False
485        elif keys[pg.K_SPACE] and self.allow_input:
486            if self.arrow_index == 0:
487                self.state = 'confirmsell'
488                self.allow_input = False
489                for item in self.items:
490                    if item['type'] == item_list[0]:
491                        self.item_to_be_sold = item
492
493            elif self.arrow_index == 1 and len(choices) == 3:
494                self.state = 'confirmsell'
495                self.allow_input = False
496                for item in self.items:
497                    if item['type'] == choices[1]:
498                        self.item_to_be_sold = item
499            else:
500                self.state = 'buysell'
501                self.allow_input = False
502            self.arrow_index = 0
503
504        if not keys[pg.K_SPACE] and not keys[pg.K_DOWN] and not keys[pg.K_UP]:
505            self.allow_input = True
506
507
508    def cant_sell(self, keys, current_time):
509        """Do not allow player to sell anything"""
510        dialogue = ["You don't have anything to sell!"]
511        self.dialogue_box = self.make_dialogue_box(dialogue, 0)
512
513        if keys[pg.K_SPACE] and self.allow_input:
514            self.state = 'buysell'
515            self.allow_input = False
516
517
518        if not keys[pg.K_SPACE]:
519            self.allow_input = True
520
521
522    def update(self, keys, current_time):
523        """Updates the shop GUI"""
524        state_function = self.state_dict[self.state]
525        state_function(keys, current_time)
526
527
528    def draw(self, surface):
529        """Draw GUI to level surface"""
530        state_list1 = ['dialogue', 'reject', 'accept', 'hasitem']
531        state_list2 = ['select', 'confirmpurchase', 'buysell', 'sell', 'confirmsell']
532
533        surface.blit(self.dialogue_box.image, self.dialogue_box.rect)
534        surface.blit(self.gold_box.image, self.gold_box.rect)
535        if self.state in state_list2:
536            surface.blit(self.selection_box.image, self.selection_box.rect)
537            surface.blit(self.selection_arrow.image, self.selection_arrow.rect)
538