interface.py (7887B)
1 import pygame 2 from .gamelib import * 3 4 ################ 5 # interface.py # 6 ################ 7 8 # This file contains: 9 # 1. The Interface class, which handles user input and other pygame events. 10 11 ################################### 12 # Section 1 - The Interface Class # 13 ################################### 14 15 class Interface(object): 16 """ 17 Interface is a handler for pygame events. It is mostly 18 concerned with handling user input events such as 19 button clicks. 20 """ 21 22 def __init__(self, game): 23 24 # Saved values 25 self.game = game 26 27 # Interface values 28 self.left_double_clicking = False 29 self.left_double_click_timer = 0 30 self.left_double_click_mousepos = None 31 self.right_double_clicking = False 32 self.right_double_click_timer = 0 33 self.right_double_click_mousepos = None 34 self.old_mousepos = None 35 self.key_bools = { n: False for k in CONTROLS for n in CONTROLS[k] } 36 self.keys_just_pressed = { n: False for k in CONTROLS for n in CONTROLS[k] } 37 self.keys_just_released = { n: False for k in CONTROLS for n in CONTROLS[k] } 38 39 def handle_events(self): 40 """ 41 Handle pygame events generated in the course of 42 gameplay. 43 """ 44 for event in pygame.event.get(): 45 46 # Handle closing the game 47 if event.type == pygame.QUIT: 48 self.game.quit_game() 49 50 # Handle button events 51 elif event.type == pygame.KEYDOWN: 52 self.handle_key_press(event) 53 elif event.type == pygame.KEYUP: 54 self.handle_key_release(event) 55 56 def handle_key_press(self, event): 57 """ 58 React to a key being pressed. 59 """ 60 if event.key in self.key_bools.keys(): 61 self.key_bools[event.key] = True 62 self.keys_just_pressed[event.key] = True 63 64 def handle_key_release(self, event): 65 """ 66 React to a key being released. 67 """ 68 if event.key in self.key_bools.keys(): 69 self.key_bools[event.key] = False 70 self.keys_just_released[event.key] = True 71 72 def react_to_keys(self): 73 """ 74 React to certain pressed/not-pressed statuses 75 of keys on a mode-by-mode basis. Called during 76 update. 77 """ 78 for k in self.key_bools: 79 if self.key_bools[k]: 80 if k in CONTROLS["up"]: 81 if self.game.state_mode in OVERHEAD_MODES: 82 self.game.move_player_on_board((0, -1)) 83 elif self.game.state_mode == STATE_MODES.Dungeon_Mode: 84 if self.game.current_dungeon != None: 85 self.game.current_dungeon.move_forward() 86 self.game.current_dungeon.take_dungeon_turn() 87 for j in CONTROLS["up"]: 88 self.key_bools[j] = False 89 return 90 elif k in CONTROLS["down"]: 91 if self.game.state_mode in OVERHEAD_MODES: 92 self.game.move_player_on_board((0, 1)) 93 elif self.game.state_mode == STATE_MODES.Dungeon_Mode: 94 if self.game.current_dungeon != None: 95 self.game.current_dungeon.move_forward(True) 96 self.game.current_dungeon.take_dungeon_turn() 97 for j in CONTROLS["down"]: 98 self.key_bools[j] = False 99 return 100 elif k in CONTROLS["left"]: 101 if self.game.state_mode in OVERHEAD_MODES: 102 self.game.move_player_on_board((-1, 0)) 103 elif self.game.state_mode == STATE_MODES.Dungeon_Mode: 104 if self.game.current_dungeon != None: 105 self.game.current_dungeon.rotate_direction(-1) 106 self.game.current_dungeon.take_dungeon_turn() 107 for j in CONTROLS["left"]: 108 self.key_bools[j] = False 109 return 110 elif k in CONTROLS["right"]: 111 if self.game.state_mode in OVERHEAD_MODES: 112 self.game.move_player_on_board((1, 0)) 113 elif self.game.state_mode == STATE_MODES.Dungeon_Mode: 114 if self.game.current_dungeon != None: 115 self.game.current_dungeon.rotate_direction() 116 self.game.current_dungeon.take_dungeon_turn() 117 for j in CONTROLS["right"]: 118 self.key_bools[j] = False 119 return 120 elif k in CONTROLS["wait"]: 121 if self.game.state_mode in OVERHEAD_MODES: 122 self.game.post_message("$PLAYERNAME waits.") 123 self.game.pass_time() 124 elif self.game.state_mode == STATE_MODES.Dungeon_Mode: 125 if self.game.current_dungeon != None: 126 self.game.post_message("$PLAYERNAME waits.") 127 self.game.current_dungeon.take_dungeon_turn() 128 for j in CONTROLS["wait"]: 129 self.key_bools[j] = False 130 return 131 132 def handle_mouse_click(self, event): 133 """ 134 React to a mousebutton being clicked. 135 """ 136 # First, get important mouse positional info, namely unoffset mouse position 137 mouseraw = pygame.mouse.get_pos() 138 139 # Handle left-click 140 if event.button == 1: 141 142 # Set up for double click 143 if self.left_double_click_timer == 0 and not self.left_double_clicking: 144 self.left_double_click_timer = 15 145 self.left_double_click_mousepos = mousepos 146 elif not self.left_double_clicking and mousepos == self.left_double_click_mousepos: 147 self.left_double_clicking = True 148 self.left_double_click_timer = 0 149 150 # Handle right-click 151 elif event.button == 3: 152 153 # Set up for double click 154 if self.right_double_click_timer == 0 and not self.right_double_clicking: 155 self.right_double_click_timer = 15 156 self.right_double_click_mousepos = mousepos 157 elif not self.right_double_clicking and mousepos == self.right_double_click_mousepos: 158 self.right_double_clicking = True 159 self.right_double_click_timer = 0 160 161 # Keepover 162 self.old_mousepos = mousepos 163 164 def handle_mouse_release(self, event): 165 """ 166 React to a mousebutton being released. 167 """ 168 # First, get important mouse positional info, namely unoffset mouse position 169 mouseraw = pygame.mouse.get_pos() 170 171 # Handle left-release 172 if event.button == 1: 173 pass 174 175 # Handle right-release 176 elif event.button == 3: 177 pass 178 179 def update_interface(self): 180 """ 181 Update interface elements (such as the cursor) once 182 per frame. This is not the same as a drawing update for 183 e.g. an Entity, and is logic-only. 184 """ 185 # UNIVERSAL PRE-REACT UPDATES 186 # Doubleclick countdown 187 if self.left_double_click_timer > 0: 188 self.left_double_click_timer -= 1 189 else: 190 self.left_double_clicking = False 191 192 # React to keys 193 self.react_to_keys() 194 195 # UNIVERSAL POST-REACT UPDATES 196 # Just-pressed/just-released toggle 197 for pk in self.keys_just_pressed: 198 if self.keys_just_pressed[pk] == True: 199 self.keys_just_pressed[pk] = False 200 for rk in self.keys_just_released: 201 if self.keys_just_released[rk] == True: 202 self.keys_just_released[rk] = False