def on_mouse_press(x, y, button, modifiers): mouse_position = Position(x, window.get_size()[1] - y) if button == pyglet.window.mouse.LEFT: if x >= grid_offset.x and y >= grid_offset.y and ui_state[ 'waiting_for_position'] is not None: ui_state['waiting_for_position'].destination = grid.get_position( grid_offset, window.get_size(), x, y) ui_state['waiting_for_position'] = None ui_state['tab_entities_focus'] = is_position_in_rectangle( mouse_position, 32, 30, 106, 71) ui_state['tab_enchantments_focus'] = is_position_in_rectangle( mouse_position, 141, 30, 106, 71) ui_state['tab_spells_focus'] = is_position_in_rectangle( mouse_position, 250, 30, 106, 71) ui_state['tab_settings_focus'] = is_position_in_rectangle( mouse_position, 359, 30, 106, 71) for button in buttons.copy(): button.focus = is_position_in_rectangle(mouse_position, button.last_position.x, button.last_position.y, button.image.width, button.image.height)
def on_mouse_release(x, y, button, modifiers): mouse_position = Position(x, window.get_size()[1] - y) if button == pyglet.window.mouse.LEFT: if ui_state['tab_entities_focus']: ui_state['buy_button_slime'] = Button('acheter_slime', 'acheter_slime_hover', 'acheter_slime_focus', on_click_buy_slime) ui_state['buy_button_goblin'] = Button('acheter_goblin', 'acheter_goblin_hover', 'acheter_goblin_focus', on_click_buy_goblin) ui_state['buy_button_orc'] = Button('acheter_orc', 'acheter_orc_hover', 'acheter_slime_focus', on_click_buy_orc) ui_state['current_tab'] = 0 elif ui_state['tab_enchantments_focus']: generate_enchantments() ui_state['current_tab'] = 1 elif ui_state['tab_spells_focus']: ui_state['current_tab'] = 2 elif ui_state['tab_settings_focus']: ui_state['current_tab'] = 3 ui_state['tab_entities_focus'] = False ui_state['tab_enchantments_focus'] = False ui_state['tab_spells_focus'] = False ui_state['tab_settings_focus'] = False for button in buttons.copy(): if button.focus: button.focus = False button.on_click()
def on_drag(current_spell_box, x, y, dx, dy): for j, spell_box in enumerate(state.spell_boxes): mouse_position = Position(x, window.get_size()[1] - y) if spell_box.index != current_spell_box.index and \ is_position_in_rectangle(mouse_position, spell_box.last_position.x, spell_box.last_position.y, spell_box.image.width, spell_box.image.height): ui_state['spells_order'][ current_spell_box. index], ui_state['spells_order'][ spell_box. index] = ui_state['spells_order'][ spell_box. index], ui_state['spells_order'][ current_spell_box.index] spell_box.index, current_spell_box.index = current_spell_box.index, spell_box.index return
def on_mouse_motion(x, y, dx, dy): mouse_position = Position(x, window.get_size()[1] - y) ui_state['tab_entities_hover'] = is_position_in_rectangle( mouse_position, 32, 30, 106, 71) ui_state['tab_enchantments_hover'] = is_position_in_rectangle( mouse_position, 141, 30, 106, 71) ui_state['tab_spells_hover'] = is_position_in_rectangle( mouse_position, 250, 30, 106, 71) ui_state['tab_settings_hover'] = is_position_in_rectangle( mouse_position, 359, 30, 106, 71) for button in buttons.copy(): button.hover = is_position_in_rectangle(mouse_position, button.last_position.x, button.last_position.y, button.image.width, button.image.height)
def draw(self, batch, group, position, top_group=None): self.last_position = Position(position.x, window.get_size()[1] - position.y) if self.focus: self.sprite = pyglet.sprite.Sprite(self.focus_image, x=position.x, y=position.y, batch=batch, group=group) elif self.hover: self.sprite = pyglet.sprite.Sprite(self.hover_image, x=position.x, y=position.y, batch=batch, group=group) else: self.sprite = pyglet.sprite.Sprite(self.image, x=position.x, y=position.y, batch=batch, group=group)
def __init__(self, image, hover_image, focus_image, on_click, draggable=False): global buttons self.hover = False self.focus = False self.image = resources.images[image] self.hover_image = resources.images[hover_image] self.focus_image = resources.images[focus_image] self.draggable = draggable self.on_click = on_click buttons.add(self) self.last_position = Position(0, 0)
def on_draw(): global spell_boxes not_edible = [] window.clear() main_batch = pyglet.graphics.Batch() background_group = pyglet.graphics.OrderedGroup(0) resources_group = pyglet.graphics.OrderedGroup(1) entities_group = pyglet.graphics.OrderedGroup(2) ui_background_group = pyglet.graphics.OrderedGroup(3) ui_group = pyglet.graphics.OrderedGroup(4) ui_top_group = pyglet.graphics.OrderedGroup(5) ui_new_window_group = pyglet.graphics.OrderedGroup(6) ui_on_window_group = pyglet.graphics.OrderedGroup(7) ui_on_on_window_group = pyglet.graphics.OrderedGroup(8) ui_tabs_y = window.get_size()[1] - 30 ui_header = pyglet.sprite.Sprite(resources.images['ui_header'], x=0, y=window.get_size()[1], batch=main_batch, group=ui_background_group) ui_footer = pyglet.sprite.Sprite(resources.images['ui_footer'], x=0, y=resources.images['ui_footer'].height, batch=main_batch, group=ui_background_group) if ui_state['tab_entities_focus']: ui_tab_entities = pyglet.sprite.Sprite( resources.images['ui_tab_entities_focus'], x=32, y=ui_tabs_y, batch=main_batch, group=ui_group) elif ui_state['tab_entities_hover']: ui_tab_entities = pyglet.sprite.Sprite( resources.images['ui_tab_entities_hover'], x=32, y=ui_tabs_y, batch=main_batch, group=ui_group) else: ui_tab_entities = pyglet.sprite.Sprite( resources.images['ui_tab_entities'], x=32, y=ui_tabs_y, batch=main_batch, group=ui_group) if ui_state['tab_enchantments_focus']: ui_tab_enchantments = pyglet.sprite.Sprite( resources.images['ui_tab_enchantments_focus'], x=141, y=ui_tabs_y, batch=main_batch, group=ui_group) elif ui_state['tab_enchantments_hover']: ui_tab_enchantments = pyglet.sprite.Sprite( resources.images['ui_tab_enchantments_hover'], x=141, y=ui_tabs_y, batch=main_batch, group=ui_group) else: ui_tab_enchantments = pyglet.sprite.Sprite( resources.images['ui_tab_enchantments'], x=141, y=ui_tabs_y, batch=main_batch, group=ui_group) if ui_state['tab_spells_focus']: ui_tab_spells = pyglet.sprite.Sprite( resources.images['ui_tab_spells_focus'], x=250, y=ui_tabs_y, batch=main_batch, group=ui_group) elif ui_state['tab_spells_hover']: ui_tab_spells = pyglet.sprite.Sprite( resources.images['ui_tab_spells_hover'], x=250, y=ui_tabs_y, batch=main_batch, group=ui_group) else: ui_tab_spells = pyglet.sprite.Sprite(resources.images['ui_tab_spells'], x=250, y=ui_tabs_y, batch=main_batch, group=ui_group) if ui_state['tab_settings_focus']: ui_tab_settings = pyglet.sprite.Sprite( resources.images['ui_tab_settings_focus'], x=359, y=ui_tabs_y, batch=main_batch, group=ui_group) elif ui_state['tab_settings_hover']: ui_tab_settings = pyglet.sprite.Sprite( resources.images['ui_tab_settings_hover'], x=359, y=ui_tabs_y, batch=main_batch, group=ui_group) else: ui_tab_settings = pyglet.sprite.Sprite( resources.images['ui_tab_settings'], x=359, y=ui_tabs_y, batch=main_batch, group=ui_group) ui_background_height = window.get_size()[1] - resources.images[ 'ui_header'].height - resources.images['ui_footer'].height + 38 ui_background = [] for y in range(ui_background_height): ui_background.append( pyglet.sprite.Sprite(resources.images['ui_background'], x=0, y=resources.images['ui_footer'].height + y + 1, batch=main_batch, group=ui_background_group)) header_height = 125 if ui_state['current_tab'] == 1: if ui_state['current_enchantment'] is None: for i, (enchantment, button) in enumerate( zip(enchantments, state.enchantment_boxes)): position = Position( 50, window.get_size()[1] - (header_height + i * (resources.images['ui_enchantment_box'].height + 5))) button.draw(main_batch, ui_group, position) not_edible.append( pyglet.text.Label(enchantment.name, font_name='04b_03b', font_size=18, x=position.x + 70, y=position.y - 20, batch=main_batch, group=ui_top_group, anchor_x='left', anchor_y='top')) else: enchantment = enchantments[ui_state['current_enchantment']] ui_state['return_button'].draw( main_batch, ui_top_group, Position(35, window.get_size()[1] - header_height + 17)) ui_state['add_button'].draw(main_batch, ui_top_group, Position(395, 75)) if ui_state['window']: modal_x = window.get_size( )[0] // 2 - resources.images['fenetre'].width // 2 modal_y = window.get_size( )[1] // 2 - resources.images['fenetre'].height // 2 not_edible.append( pyglet.sprite.Sprite(resources.images['fenetre'], x=modal_x, y=window.get_size()[1] - modal_y, batch=main_batch, group=ui_new_window_group)) ui_state['quit_button'].draw( main_batch, ui_on_window_group, Position(modal_x + 550, window.get_size()[1] - modal_y - 30)) for i, button in enumerate(ui_state['spell_buttons']): button.draw( main_batch, ui_on_window_group, Position( modal_x + 100, window.get_size()[1] - modal_y - 30 - (button.image.height + 5) * i)) position = Position( modal_x + 100, window.get_size()[1] - modal_y - 30 - (button.image.height + 5) * i) not_edible.append( pyglet.sprite.Sprite( resources.images['spell_' + spells[i].__name__[:-5].lower()], x=position.x + 30, y=position.y - 25, batch=main_batch, group=ui_on_on_window_group)) not_edible.append( pyglet.sprite.Sprite(resources.images['ui_enchantment_cost'], x=402, y=window.get_size()[1] - header_height + 22, batch=main_batch, group=ui_group)) not_edible.append( pyglet.text.Label(str(enchantment.cost), font_name='04b_03b', font_size=20, x=418, y=window.get_size()[1] - header_height + 15, batch=main_batch, group=ui_top_group, anchor_y='top', anchor_x='left')) for j, i in enumerate(ui_state['spells_order']): spell = enchantment.spells[i] button = state.spell_boxes[i] position = Position( 50, window.get_size()[1] - (header_height + 30 + j * (resources.images['ui_spell_box'].height + 5))) button.draw(main_batch, ui_group, position, ui_top_group) not_edible.append( pyglet.text.Label(str(spell.cost), font_name='04b_03b', font_size=20, x=position.x + 275, y=position.y - 37, batch=main_batch, group=ui_top_group)) not_edible.append( pyglet.sprite.Sprite( resources.images['spell_' + type(spell).__name__[:-5].lower()], x=position.x + 30, y=position.y - 25, batch=main_batch, group=ui_top_group)) if ui_state['current_tab'] == 0: not_edible.append( pyglet.sprite.Sprite(resources.images['boite_a_bois'], x=50, y=window.get_size()[1] - 120, batch=main_batch, group=ui_group)) not_edible.append( pyglet.text.Label(str(state.wood_count), font_name='04b_03b', font_size=12, x=95, y=window.get_size()[1] - 180, batch=main_batch, group=ui_top_group, anchor_x='left', anchor_y='top')) not_edible.append( pyglet.sprite.Sprite(resources.images['boite_a_slime'], x=150, y=window.get_size()[1] - 120, batch=main_batch, group=ui_group)) not_edible.append( pyglet.text.Label(str( len( list(filter(lambda e: isinstance(e, SlimeEntity), entities)))), font_name='04b_03b', font_size=12, x=195, y=window.get_size()[1] - 180, batch=main_batch, group=ui_top_group, anchor_x='left', anchor_y='top')) not_edible.append( pyglet.sprite.Sprite(resources.images['boite_a_goblin'], x=250, y=window.get_size()[1] - 120, batch=main_batch, group=ui_group)) not_edible.append( pyglet.text.Label(str( len( list( filter(lambda e: isinstance(e, GoblinEntity), entities)))), font_name='04b_03b', font_size=12, x=295, y=window.get_size()[1] - 180, batch=main_batch, group=ui_top_group, anchor_x='left', anchor_y='top')) not_edible.append( pyglet.sprite.Sprite(resources.images['boite_a_orc'], x=350, y=window.get_size()[1] - 120, batch=main_batch, group=ui_group)) not_edible.append( pyglet.text.Label(str( len(list(filter(lambda e: isinstance(e, OrcEntity), entities)))), font_name='04b_03b', font_size=12, x=395, y=window.get_size()[1] - 180, batch=main_batch, group=ui_top_group, anchor_x='left', anchor_y='top')) ui_state['buy_button_slime'].draw( main_batch, ui_top_group, Position(35, window.get_size()[1] - 230)) ui_state['buy_button_goblin'].draw( main_batch, ui_top_group, Position(190, window.get_size()[1] - 230)) ui_state['buy_button_orc'].draw( main_batch, ui_top_group, Position(35, window.get_size()[1] - 390)) if ui_state['game_over']: not_edible.append( pyglet.text.Label('Game over', font_name='04b_03b', font_size=60, x=window.get_size()[0] // 2, y=window.get_size()[1] // 2, anchor_x='center', anchor_y='center', group=ui_on_window, batch=main_batch)) grid.draw(main_batch, background_group, resources_group, entities_group, grid_offset, window.get_size(), entities) main_batch.draw()
from game.utils import Position from game.spell import MoveSpell, HarvestSpell, DropSpell from game.enchantment import SimpleEnchantment import game.resources as resources window = pyglet.window.Window(visible=False, resizable=True) window.set_caption('Legend Of Wizard') window.set_visible() pyglet.resource.path = ['resources'] pyglet.resource.reindex() pyglet.resource.add_font('04b_30.ttf') pyglet.resource.add_font('04b_03.ttf') grid_offset = Position(500, 0) grid = Grid() e0 = SimpleEnchantment("IA stupide") enchantments = [e0] entities = [] spells = [MoveSpell, HarvestSpell, DropSpell] class GameState: def __init__(self): self.enchantment_boxes = [] self.spell_boxes = [] self.wood_count = 0