This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
112 lines (98 loc) · 4.61 KB
/
menu.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
import pygame
from settings import *
from timer import Timer
class Menu:
def __init__(self, player, toggle_menu):
# general setup
self.player = player
self.toggle_menu = toggle_menu
self.display_surface = pygame.display.get_surface()
self.font = pygame.font.Font('./font/LycheeSoda.ttf', 30)
# options
self.width = 400
self.space = 10
self.padding = 8
# entries
self.options = list(self.player.item_inventory.keys()) + list(self.player.seed_inventory.keys())
self.sell_border = len(self.player.item_inventory) - 1
self.setup()
# movement
self.index = 0
self.timer = Timer(200)
def setup(self):
# create the text surfaces
self.text_surfs = []
self.total_height = 0
for item in self.options:
text_surf = self.font.render(item, False, 'Black')
self.text_surfs.append(text_surf)
self.total_height += text_surf.get_height() + (self.padding * 2)
self.total_height += (len(self.text_surfs) - 1) * self.space
self.menu_top = SCREEN_HEIGHT / 2 - self.total_height / 2
self.main_rect = pygame.Rect(SCREEN_WIDTH / 2 - self.width / 2, self.menu_top, self.width, self.total_height)
# buy / sell text surface
self.buy_text = self.font.render('buy', False, 'Black')
self.sell_text = self.font.render('sell', False, 'Black')
def display_money(self):
text_surf = self.font.render(f'${self.player.money}', False, 'Black')
text_rect = text_surf.get_rect(midbottom=(SCREEN_WIDTH / 2, SCREEN_HEIGHT - 20))
pygame.draw.rect(self.display_surface, 'White', text_rect.inflate(10, 10), 0, 6) # radio
self.display_surface.blit(text_surf, text_rect)
def input(self):
keys = pygame.key.get_pressed()
self.timer.update()
if keys[pygame.K_ESCAPE]:
self.toggle_menu()
if not self.timer.active:
if keys[pygame.K_UP]:
self.index -= 1
self.timer.activate()
if keys[pygame.K_DOWN]:
self.index += 1
self.timer.activate()
if keys[pygame.K_SPACE]:
self.timer.activate()
# get item
current_item = self.options[self.index]
# sell
if self.index <= self.sell_border:
if self.player.item_inventory[current_item] > 0:
self.player.item_inventory[current_item] -= 1
self.player.money += SALE_PRICES[current_item]
# buy
elif self.index > self.sell_border:
if self.player.money - PURCHASE_PRICES[current_item] >= 0:
self.player.money -= PURCHASE_PRICES[current_item]
self.player.item_inventory[current_item] += 1
# clamo the values
if self.index < 0:
self.index = len(self.options) - 1
elif self.index > len(self.options):
self.index = 0
def show_entry(self, text_surf, amount, top, selected):
# background
bg_rect = pygame.Rect(self.main_rect.left, top, self.width, text_surf.get_height() + (self.padding * 2))
pygame.draw.rect(self.display_surface, 'White', bg_rect, 0, 4)
# text
text_rect = text_surf.get_rect(midleft=(self.main_rect.left + 20, bg_rect.centery))
self.display_surface.blit(text_surf, text_rect)
# amount
amount_surf = self.font.render(str(amount), False, 'Black')
amount_rect = amount_surf.get_rect(midright=(self.main_rect.right - 20, bg_rect.centery))
self.display_surface.blit(amount_surf, amount_rect)
# selected
if selected:
pygame.draw.rect(self.display_surface, 'black', bg_rect, 4, 4) # draw a border
pos_rect = self.sell_text.get_rect(midleft=(self.main_rect.left + 150, bg_rect.centery))
if self.index <= self.sell_border: # sell
self.display_surface.blit(self.sell_text, pos_rect)
else: # buy
self.display_surface.blit(self.buy_text, pos_rect)
def upate(self):
self.input()
self.display_money()
for index, text_surf in enumerate(self.text_surfs):
top = self.main_rect.top + index * (text_surf.get_height() + (self.padding * 2) + self.space)
amount_list = list(self.player.item_inventory.values()) + list(self.player.seed_inventory.values())
amount = amount_list[index]
self.show_entry(text_surf, amount, top, self.index == index)