inventory menu

This commit is contained in:
Waylon Walker 2023-01-02 17:17:10 -06:00
parent 12f4af2b5e
commit f4b483f5bf
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4

View file

@ -188,8 +188,29 @@ class HotBarItem:
) )
# class Menu(): class Menu:
# def __init__(self, game:Game): def __init__(
self, game: Game, title: str = "menu", scale: int = 32, margin: int = 16
):
self.game = game
self.is_open = False
self.title = title
self.scale = scale
self.margin = margin
def draw(self):
if self.is_open:
surf = pygame.Surface(self.game.screen.get_size()).convert_alpha()
surf.fill((0, 0, 0, 225))
font = pygame.font.SysFont(None, self.scale)
img = font.render(self.title, True, (255, 255, 255))
surf.blit(
img,
(100, 100)
# (100 * self.scale + self.margin, self.margin),
)
self.game.screen.blit(surf, (0, 0))
class LightSource: class LightSource:
@ -312,6 +333,8 @@ class Creeper(Game):
self.joysticks = {} self.joysticks = {}
self.hotbar_back_debounce = 0 self.hotbar_back_debounce = 0
self.hotbar_forward_debounce = 0 self.hotbar_forward_debounce = 0
self.inventory_open_debounce = 0
self.inventory_menu = Menu(self, title="inventory")
def attack(self): def attack(self):
@ -335,6 +358,72 @@ class Creeper(Game):
# killing two items in the same frame will error # killing two items in the same frame will error
... ...
def normal_keys(self):
keys = self.keys
if keys[pygame.K_a]:
self.x -= 10
if keys[pygame.K_d]:
self.x += 10
if keys[pygame.K_k]:
self.hotbar.next(1)
if keys[pygame.K_j]:
self.hotbar.next(-1)
for event in self.events:
if event.type == pygame.MOUSEWHEEL:
self.hotbar.next(event.y)
if event.type == pygame.MOUSEBUTTONDOWN:
self.attack()
if event.type == pygame.JOYDEVICEADDED:
# This event will be generated when the program starts for every
# joystick, filling up the list without needing to create them manually.
joy = pygame.joystick.Joystick(event.device_index)
self.joysticks[joy.get_instance_id()] = joy
if event.type == pygame.JOYDEVICEREMOVED:
del self.joysticks[event.instance_id]
for joystick in self.joysticks.values():
if joystick.get_button(4) and self.hotbar_back_debounce:
self.hotbar.next(-1)
self.hotbar_back_debounce = 0
elif not joystick.get_button(4):
self.hotbar_back_debounce = 1
if joystick.get_button(5) and self.hotbar_forward_debounce:
self.hotbar.next(1)
self.hotbar_forward_debounce = 0
elif not joystick.get_button(5):
self.hotbar_forward_debounce = 1
if (
keys[pygame.K_e] or joystick.get_button(2)
) and self.inventory_open_debounce:
self.inventory_menu.is_open = not self.inventory_menu.is_open
self.inventory_open_debounce = 0
elif not (keys[pygame.K_e] or joystick.get_button(2)):
self.inventory_open_debounce = 1
hats = joystick.get_numhats()
for i in range(hats):
hat = joystick.get_hat(i)
if hat[0] == 1:
self.x += 10
if hat[0] == -1:
self.x -= 10
def inventory_keys(self):
keys = self.keys
for joystick in self.joysticks.values():
if (
keys[pygame.K_e] or joystick.get_button(2)
) and self.inventory_open_debounce:
self.inventory_menu.is_open = not self.inventory_menu.is_open
self.inventory_open_debounce = 0
elif not (keys[pygame.K_e] or joystick.get_button(2)):
self.inventory_open_debounce = 1
def game(self): def game(self):
creeper = next(self.creepers) creeper = next(self.creepers)
self.screen.blit(self.background, (0, 0)) self.screen.blit(self.background, (0, 0))
@ -370,57 +459,17 @@ class Creeper(Game):
) )
self.hotbar.draw() self.hotbar.draw()
self.inventory_menu.draw()
self.mouse_box = MouseSprite(self.screen, hotbar=self.hotbar) self.mouse_box = MouseSprite(self.screen, hotbar=self.hotbar)
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
self.x -= 10
if keys[pygame.K_d]:
self.x += 10
if keys[pygame.K_k]:
self.hotbar.next(1)
if keys[pygame.K_j]:
self.hotbar.next(-1)
for event in self.events:
if event.type == pygame.MOUSEWHEEL:
self.hotbar.next(event.y)
if event.type == pygame.MOUSEBUTTONDOWN:
self.attack()
if event.type == pygame.JOYDEVICEADDED:
# This event will be generated when the program starts for every
# joystick, filling up the list without needing to create them manually.
joy = pygame.joystick.Joystick(event.device_index)
self.joysticks[joy.get_instance_id()] = joy
if event.type == pygame.JOYDEVICEREMOVED:
del self.joysticks[event.instance_id]
for joystick in self.joysticks.values():
if joystick.get_button(4) and self.hotbar_back_debounce:
self.hotbar.next(-1)
self.hotbar_back_debounce = 0
elif not joystick.get_button(4):
self.hotbar_back_debounce = 1
if joystick.get_button(5) and self.hotbar_forward_debounce:
self.hotbar.next(1)
self.hotbar_forward_debounce = 0
elif not joystick.get_button(5):
self.hotbar_forward_debounce = 1
hats = joystick.get_numhats()
for i in range(hats):
hat = joystick.get_hat(i)
if hat[0] == 1:
self.x += 10
if hat[0] == -1:
self.x -= 10
self.mouse_box.draw() self.mouse_box.draw()
if self.inventory_menu.is_open:
self.inventory_keys()
else:
self.normal_keys()
if __name__ == "__main__": if __name__ == "__main__":
creeper = Creeper() creeper = Creeper()