def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if self.show_msg == False:
if event.type == pygame.KEYDOWN and event.key != pygame.K_RETURN:
lhv = len(self.home_square.value)
self.changed_since_check = True
if event.key == pygame.K_BACKSPACE:
if lhv > 0:
self.home_square.value = self.home_square.value[0:lhv - 1]
elif not self.board.grid[4][18]:
char = event.unicode
if len(char) > 0 and lhv < 2 and char in self.digits:
self.home_square.value += char
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
elif event.type == pygame.MOUSEMOTION and self.drag:
if self.board.grid[4][18]:
self.home_square.value = ""
self.home_square.update_me = True
elif event.type == pygame.MOUSEBUTTONUP:
for each in self.board.units:
if each.is_door is True:
self.board.all_sprites_list.move_to_front(each)
python类K_BACKSPACE的实例源码
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if self.show_msg == False:
if event.type == pygame.KEYDOWN and event.key != pygame.K_RETURN:
lhv = len(self.home_square.value)
self.changed_since_check = True
if event.key == pygame.K_BACKSPACE:
if lhv > 0:
self.home_square.value = self.home_square.value[0:lhv - 1]
elif not self.board.grid[4][18]:
char = event.unicode
if len(char) > 0 and char in self.digits:
self.home_square.value = char
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
elif event.type == pygame.MOUSEMOTION and self.drag:
if self.board.grid[4][18]:
self.home_square.value = ""
self.home_square.update_me = True
elif event.type == pygame.MOUSEBUTTONUP:
for each in self.board.units:
if each.is_door is True:
self.board.all_sprites_list.move_to_front(each)
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if self.show_msg == False:
if event.type == pygame.KEYDOWN and event.key != pygame.K_RETURN and not self.correct:
lhv = len(self.home_square.value)
self.changed_since_check = True
if event.key == pygame.K_BACKSPACE:
if lhv > 0:
self.home_square.value = self.home_square.value[0:lhv - 1]
else:
char = event.unicode
if (len(char) > 0 and lhv < 5 and char in self.digits):
self.home_square.value += char
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
elif event.type == pygame.MOUSEBUTTONUP:
self.home_square.update_me = True
if self.board.active_ship == self.home_square.unit_id:
self.home_square.perm_outline_width = 5
self.home_square = self.ans_h
self.board.active_ship = self.home_square.unit_id
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if self.show_msg == False:
if event.type == pygame.KEYDOWN and event.key != pygame.K_RETURN:
lhv = len(self.home_square.value)
self.changed_since_check = True
if event.key == pygame.K_BACKSPACE:
if lhv > 0:
self.home_square.value = self.home_square.value[0:lhv - 1]
else:
char = event.unicode
if char in self.digits:
if len(char) > 0 and lhv < self.max_len:
self.home_square.value += char
else:
self.home_square.value = char
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
def onKeyDown(self, event):
if not self.select_item:
if event.type == pygame.KEYDOWN and event.key == pygame.K_DELETE:
pass
elif event.type == pygame.KEYDOWN and event.key != pygame.K_RETURN and event.key != pygame.K_KP_ENTER and event.key != pygame.K_TAB:
lhv = len(self.value)
if event.key == pygame.K_BACKSPACE:
if lhv > 0:
if self.ls.lang.ltr_text:
self.value = self.value[0:lhv - 1]
else:
self.value = self.value[1:lhv]
else:
char = event.unicode
if len(char) > 0 and lhv < 21:
if self.ls.lang.ltr_text:
self.value = self.value + char
else:
self.value = char + self.value
self.ls.reload_selects()
self.ls.set_scrollbar_top(self.ls.scroll_min_top)
elif event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER or event.key == pygame.K_TAB:
self.nextFocus()
self.update_trigger()
def touch_handler(self, event):
#handle keypresses...
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
self.panel.text.del_letter()
elif event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER:
self.panel.new_line()
elif event.key == pygame.K_ESCAPE:
self.close(None)
elif event.key == pygame.K_LEFT:
self.panel.text.cursor_left()
elif event.key == pygame.K_RIGHT:
self.panel.text.cursor_right()
elif len(event.unicode) > 0:
self.panel.text.add_letter(event.unicode)
super(Keyboard,self).touch_handler(event)
def _map_keys(self):
key_map = self.io.key_map
key_map[imgui.KEY_TAB] = pygame.K_TAB
key_map[imgui.KEY_LEFT_ARROW] = pygame.K_LEFT
key_map[imgui.KEY_RIGHT_ARROW] = pygame.K_RIGHT
key_map[imgui.KEY_UP_ARROW] = pygame.K_UP
key_map[imgui.KEY_DOWN_ARROW] = pygame.K_DOWN
key_map[imgui.KEY_PAGE_UP] = pygame.K_PAGEUP
key_map[imgui.KEY_PAGE_DOWN] = pygame.K_PAGEDOWN
key_map[imgui.KEY_HOME] = pygame.K_HOME
key_map[imgui.KEY_END] = pygame.K_END
key_map[imgui.KEY_DELETE] = pygame.K_DELETE
key_map[imgui.KEY_BACKSPACE] = pygame.K_BACKSPACE
key_map[imgui.KEY_ENTER] = pygame.K_RETURN
key_map[imgui.KEY_ESCAPE] = pygame.K_ESCAPE
key_map[imgui.KEY_A] = pygame.K_a
key_map[imgui.KEY_C] = pygame.K_c
key_map[imgui.KEY_V] = pygame.K_v
key_map[imgui.KEY_X] = pygame.K_x
key_map[imgui.KEY_Y] = pygame.K_y
key_map[imgui.KEY_Z] = pygame.K_z
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if self.show_msg == False:
if event.type == pygame.KEYDOWN and event.key != pygame.K_RETURN:
lhv = len(self.home_square.value)
self.changed_since_check = True
if event.key == pygame.K_BACKSPACE:
if lhv > 0:
self.home_square.value = self.home_square.value[0:lhv - 1]
else:
char = event.unicode
if len(char) > 0 and lhv < 3 and char in self.digits:
self.home_square.value += char
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if self.show_msg == False:
if event.type == pygame.KEYDOWN and event.key != pygame.K_RETURN:
lhv = len(self.home_square.value)
self.changed_since_check = True
if event.key == pygame.K_BACKSPACE:
if lhv > 0:
self.home_square.value = self.home_square.value[0:lhv - 1]
else:
char = event.unicode
if len(char) > 0 and lhv < self.sollen and char in self.digits:
self.home_square.value += char
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if self.show_msg == False:
if event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN:
self.auto_check_reset()
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
self.home_sqare_switch(self.board.active_ship + 1)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
self.home_sqare_switch(self.board.active_ship - 1)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
self.home_sqare_switch(self.board.active_ship - self.sumn1n2sl + 1)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
self.home_sqare_switch(self.board.active_ship + self.sumn1n2sl)
elif event.type == pygame.KEYDOWN and event.key != pygame.K_RETURN and not self.correct:
lhv = len(self.home_square.value)
self.changed_since_check = True
if event.key == pygame.K_BACKSPACE:
if lhv > 0:
self.home_square.value = self.home_square.value[0:lhv - 1]
else:
char = event.unicode
if (len(char) > 0 and lhv < 2 and char in self.digits):
self.home_square.value = char
if self.auto_select:
self.home_sqare_switch(self.board.active_ship + 1)
else:
self.home_square.value = ""
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
elif event.type == pygame.MOUSEBUTTONUP:
self.home_sqare_switch(self.board.active_ship)
def update(self, keyevent):
key = keyevent.key
unicode = keyevent.unicode
if key > 31 and key < 127 and (
self.maxLength == 0 or len(self.text) < self.maxLength): # only printable characters
if keyevent.mod == 1 and self.case == 1 and key >= 97 and key <= 122:
# force lowercase letters
key -= 32
self.text += chr(key)
else:
# use the unicode char
self.text += unicode
elif key == 8:
# backspace. repeat until clear
keys = pygame.key.get_pressed()
nexttime = pygame.time.get_ticks() + 200
deleting = True
while deleting:
keys = pygame.key.get_pressed()
if keys[pygame.K_BACKSPACE]:
thistime = pygame.time.get_ticks()
if thistime > nexttime:
self.text = self.text[0:len(self.text) - 1]
self.image.fill((255, 255, 255))
pygame.draw.rect(self.image, (0, 0, 0), [0, 0, self.width - 1, self.boxSize - 1], 2)
newSurface = self.font.render(self.text, True, self.fontColour)
self.image.blit(newSurface, [10, 5])
updateDisplay()
nexttime = thistime + 50
pygame.event.clear()
else:
deleting = False
self.image.fill((255, 255, 255))
pygame.draw.rect(self.image, (0, 0, 0), [0, 0, self.width - 1, self.boxSize - 1], 2)
newSurface = self.font.render(self.text, True, self.fontColour)
self.image.blit(newSurface, [10, 5])
updateDisplay()
def update(self, keyevent):
key = keyevent.key
unicode = keyevent.unicode
if key > 31 and key < 127 and (
self.maxLength == 0 or len(self.text) < self.maxLength): # only printable characters
if keyevent.mod == 1 and self.case == 1 and key >= 97 and key <= 122:
# force lowercase letters
key -= 32
self.text += chr(key)
else:
# use the unicode char
self.text += unicode
elif key == 8:
# backspace. repeat until clear
keys = pygame.key.get_pressed()
nexttime = pygame.time.get_ticks() + 200
deleting = True
while deleting:
keys = pygame.key.get_pressed()
if keys[pygame.K_BACKSPACE]:
thistime = pygame.time.get_ticks()
if thistime > nexttime:
self.text = self.text[0:len(self.text) - 1]
self.image.fill((255, 255, 255))
pygame.draw.rect(self.image, (0, 0, 0), [0, 0, self.width - 1, self.boxSize - 1], 2)
newSurface = self.font.render(self.text, True, self.fontColour)
self.image.blit(newSurface, [10, 5])
updateDisplay()
nexttime = thistime + 50
pygame.event.clear()
else:
deleting = False
self.image.fill((255, 255, 255))
pygame.draw.rect(self.image, (0, 0, 0), [0, 0, self.width - 1, self.boxSize - 1], 2)
newSurface = self.font.render(self.text, True, self.fontColour)
self.image.blit(newSurface, [10, 5])
updateDisplay()
def main(self):
"""
Start the game
:return: None
"""
logging.info("Starting game")
self.board.initUI()
winner = None
while not self.exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.exit = True
elif not self.gameOver:
currentPlayer = self.players[self.turn]
move = currentPlayer.getMove()
self.turn = 'x' if self.turn == 'o' else 'o'
winner = self.board.checkForWin(move)[0]
if winner in ['x', 'o', 't']:
logging.info("Game has ended with status: {}".format(winner))
self.gameOver = True
self.board.displayWinner(self.board.checkForWin(move))
else:
if event.type == pygame.KEYDOWN:
if event.key in [pygame.K_RETURN, pygame.K_SPACE]:
self.gameOver = False
self.board.reset()
self.board.initUI()
elif event.key in [pygame.K_ESCAPE, pygame.K_DELETE, pygame.K_BACKSPACE]:
self.exit = True
pygame.display.flip()
self.clock.tick(self.fps)
self.exit = False
self.gameOver = False
self.board.reset()
return winner
def handleKey(self, key):
# TODO handling for holding down keys -- this has to be done app-wide
if ord('0') <= key <= ord('9') or key == ord('.'):
self.text += chr(key)
self.text = self.text[:self.max_len]
elif key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
def __init__(self, main_menu):
self.parent = main_menu.parent
self.main_menu = main_menu
self.teams = self.parent.teams
self.parent.event_handler.register_key_listener(self.handle_keypress)
self.title = text_renderer.render_title("Teams", colours.COLOUR_WHITE)
self.title_position = (
(self.parent.resolution[0] - self.title.get_width())// 2,
15
)
self.directions = {
pygame.K_RIGHT : [1],
pygame.K_LEFT : [-1],
pygame.K_ESCAPE : [3],
pygame.K_BACKSPACE : [3]
}
self.animation = None
menu_region = (self.parent.resolution[0],
600
)
self.menu = TeamViewer(menu_region, self.teams)
self.menu.register_finished_callback(self.finished)
def __init__(self, main_menu):
self.parent = main_menu.parent
self.main_menu = main_menu
self.settings = self.parent.settings
self.parent.event_handler.register_key_listener(self.handle_keypress)
self.title = text_renderer.render_title("Options", colours.COLOUR_WHITE)
self.title_position = (
(self.parent.resolution[0] - self.title.get_width())// 2,
15
)
self.directions = {
pygame.K_UP : [-1, 0],
pygame.K_DOWN : [ 1, 0],
pygame.K_RIGHT : [ 0, 1],
pygame.K_LEFT : [ 0, -1],
pygame.K_SPACE : [ 0, 2],
pygame.K_RETURN : [ 0, 2],
pygame.K_ESCAPE : [ 0, 3],
pygame.K_BACKSPACE : [ 0, 3]
}
self.animation = None
self.menu = SettingsMenu(self.settings)
self.menu.register_finished_callback(self.finished)
def keyDown(self, event):
if self.selected:
if event.key == pygame.K_ESCAPE:
self.selected = False
elif event.key == pygame.K_BACKSPACE and len(self.current_string) > 0:
#self.current_string = self.current_string[:-1]
self.current_string.pop(self.curpos - 1)
self.curpos -= 1
elif event.key == pygame.K_DELETE and len(self.current_string) > 0:
#self.current_string = self.current_string[:-1]
self.current_string.pop(self.curpos)
elif event.key == pygame.K_RETURN:
if self.completeHandler != None:
self.completeHandler(self, string.join(self.current_string, ""))
self.completed = True
elif event.key >= 32 and event.key <= 127:
k = event.key
kp = pygame.key.get_pressed()
if (kp[pygame.K_LSHIFT] or kp[pygame.K_RSHIFT]) and k > 96 and k < 123:
k -= 32
elif (kp[pygame.K_LSHIFT] or kp[pygame.K_RSHIFT]) and k > 32:
k = ord(self.schars[chr(k)])
self.current_string.insert(self.curpos, chr(k))
self.curpos += 1
elif event.key == pygame.K_LEFT and self.curpos > 0:
self.curpos -= 1
elif event.key == pygame.K_RIGHT and self.curpos < len(self.current_string):
self.curpos += 1
def update(self, *args):
"""
Handles the selection
parameters: tuple arguments for the update (first argument should be an instance pygame.event.Event)
return values: -
"""
if len(args) > 0 and self.isActive():
event = args[0]
if event.type == pygame.KEYDOWN and self.isFocused():
if event.key == pygame.K_UP:
self.moveCursor(-1)
elif event.key == pygame.K_DOWN:
self.moveCursor(1)
elif event.key == pygame.K_BACKSPACE or event.key == pygame.K_DELETE:
if self.isEditable():
if self._selection == self._cursor:
self.delete(self._selection - 1, CURSOR)
self.moveCursor(-1)
else:
self.delete(SELECTION, CURSOR)
self.setCursor(self._selection)
elif event.type == pygame.MOUSEBUTTONUP:
if self.rect.collidepoint(event.pos):
self.setSelection(CURSOR, self._posToIndex(event.pos[1] - self.rect.y))
elif event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.setCursor(self._posToIndex(event.pos[1] - self.rect.y))
super(Listbox, self).update(*args)
def update(self, *args):
"""
Handles the selection and keyboard-input
parameters: tuple arguments for the update (first argument should be an instance pygame.event.Event)
return values: -
"""
if len(args) > 0 and self.isActive():
event = args[0]
if event.type == pygame.KEYDOWN and self.isFocused():
if event.key == pygame.K_LEFT:
self.moveCursor(-1)
elif event.key == pygame.K_RIGHT:
self.moveCursor(1)
elif event.key == pygame.K_BACKSPACE or event.key == pygame.K_DELETE:
if self._selection == self._cursor:
if event.key == pygame.K_DELETE:
self.delete(self._selection + 1, CURSOR)
else:
self.delete(self._selection - 1, CURSOR)
self.moveCursor(-1)
else:
self.delete(SELECTION, CURSOR)
self.setCursor(self._sort(SELECTION, CURSOR)[0])
else:
char = event.unicode.encode("ascii", "ignore")
if (char != "" and (char == " " or not char.isspace())
and self._validation(self._text + char, self._text, self)):
self.delete(SELECTION, CURSOR)
s = self._sort(SELECTION, CURSOR)[0]
self.insert(s, char)
self.setCursor(s + 1)
elif event.type == pygame.MOUSEMOTION:
if self.rect.collidepoint(event.pos) and event.buttons[0]:
self.setSelection(SELECTION, self._posToIndex(event.pos[0] - self.rect.x))
elif event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.setCursor(self._posToIndex(event.pos[0] - self.rect.x))
super(Entry, self).update(*args)
def check(self, event=None): # function to scheck and interact with the user
if self.off:
return False
if event is None: # checking for mouse input
mousepos = pygame.mouse.get_pos()
mouseclick = pygame.mouse.get_pressed() # gets button press status and position
if mouseclick[0]:
if not self.lock:
if check_mouse_round_rect(mousepos, self.rect, self.radius) and mouseclick[0]:
self.typing = True # if the user is clicking and inside the box then the user is typing
else:
self.typing = False # if the user has clicked somewhere else they are no longer typing
self.lock = True
else:
self.lock = False
else:
if not self.typing: # checking keyboard inputs and scroll wheel
return False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN: # returns text if enter is pressed
return self.text
elif event.key != pygame.K_BACKSPACE and event.key != 9 and event.key != 27:# if the event.key isnt backspace or a special button (eg ctrl)
if event.key < 256: #
if self.charLimit is None or len(self.text) < self.charLimit:
self.text += event.unicode.encode('ascii', 'replace') # inupts the character key and converts it to ascii format before adding it to the text in the box
elif event.key == pygame.K_BACKSPACE: # if the key was backspace then it deletes the last character
self.text = self.text[:len(self.text)-1]
elif event.type == pygame.MOUSEBUTTONDOWN: # if a button has been pressed and that button is a mousewheel, offset the text so it appears to scroll
if event.button == 4:
if self.offSet + 4 > self.height - self.rect[3]:
pass
else:
self.offSet += 4
elif event.button == 5:
if self.offSet - 4 < 0:
pass
else:
self.offSet -= 4
return False
def get_event(self,event, mouse_pos):
if event.type == pg.KEYDOWN and self.active:
if event.key in (pg.K_RETURN,pg.K_KP_ENTER):
self.execute()
elif event.key == pg.K_BACKSPACE:
if self.buffer:
self.buffer.pop()
elif event.unicode in self.accepted:
self.buffer.append(event.unicode)
elif event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
self.active = self.rect.collidepoint(mouse_pos)
def handle_event(self, event):
'''
Handle input events while the code editor is up.
Overrides the code editor version
'''
if event.type == pygame.KEYDOWN:
self.text_change = True
if not self.initial_edit and event.key != pygame.K_RETURN:
self.initial_edit = True
self.text = ""
if event.key == pygame.K_RETURN and self.initial_edit:
self.stop()
elif event.key == pygame.K_RETURN:
pass
elif event.key == pygame.K_DELETE:
self.k_delete()
elif event.key == pygame.K_LEFT:
self.k_left()
elif event.key == pygame.K_RIGHT:
self.k_right()
elif event.key == pygame.K_BACKSPACE:
self.k_backspace()
elif event.key == pygame.K_TAB:
pass
elif event.key == pygame.K_ESCAPE:
pass
else:
self.character_key(event.key)
return True # keep processing events
def handle_event(self, event):
'''
Handle input events while the code editor is up
'''
if event.type == pygame.KEYDOWN:
self.text_change = True
if event.key == pygame.K_ESCAPE:
self.stop()
elif event.key == pygame.K_DELETE:
self.k_delete()
elif event.key == pygame.K_LEFT:
self.k_left()
elif event.key == pygame.K_RIGHT:
self.k_right()
elif event.key == pygame.K_BACKSPACE:
self.k_backspace()
elif event.key == pygame.K_TAB:
self.k_tab()
elif event.key == pygame.K_RETURN:
self.k_return()
elif event.key == pygame.K_UP:
self.k_up()
elif event.key == pygame.K_DOWN:
self.k_down()
else:
self.character_key(event.key)
return True # keep processing events
def input_handling(self, e, KEY, max_length):
if e.key == pygame.K_BACKSPACE:
self.labels[KEY].update_text(self.labels[KEY].symbols[:len(self.labels[KEY].symbols)-1], False)
elif len(self.labels[KEY].symbols) < max_length and (KEY == 'name_MI' or e.unicode in ''.join([str(i) for i in range(10)])):
self.labels[KEY].update_text(self.labels[KEY].symbols + e.unicode, False)
if 'property_management_input' in self.labels.keys() or check_substring_in_dict_keys(self.labels, 'trading_input'):
self.create_trading_input_spec_objects(KEY)
self.make_obj_for_enter_name(KEY)
#--- Menu actions
def key_press(self, button, event, down):
if down and focus == button:
if event.key == pygame.K_BACKSPACE:
self.backspace(button)
self.cds.start("back_start")
self.bdown = True
elif event.key == pygame.K_LEFT:
self.move_cursor(True, button)
self.cds.start("left_start")
self.ldown = True
elif event.key == pygame.K_RIGHT:
self.move_cursor(False, button)
self.cds.start("right_start")
self.rdown = True
else:
char = event.unicode
if char != None:
self.text = self.text[:self.cursor] + char + self.text[self.cursor:]
self.move_cursor(False, button)
self.changed = True
elif not down and focus == button:
if event.key == pygame.K_LEFT:
self.ldown = False
self.cds.stop("left_start")
self.cds.stop("left")
elif event.key == pygame.K_RIGHT:
self.rdown = False
self.cds.stop("right_start")
self.cds.stop("right")
elif event.key == pygame.K_BACKSPACE:
self.bdown = False
self.cds.stop("back_start")
self.cds.stop("back")
#print("test")
def readChar():
if event.key == pygame.K_BACKSPACE:
return 'backspace'
elif event.key == pygame.K_PAGEUP:
return 'pageup'
elif event.key == pygame.K_PAGEDOWN:
return 'pagedown'
elif event.key == pygame.K_TAB:
return 'tab'
elif event.key == pygame.K_RETURN:
return 'enter'
elif event.key == pygame.K_ESCAPE:
return 'esc'
elif event.key in (pygame.K_RSHIFT, pygame.K_LSHIFT):
return 'shift'
elif event.key in (pygame.K_RCTRL, pygame.K_LCTRL):
return 'control'
elif event.key == pygame.K_RIGHT:
return 'kright'
elif event.key == pygame.K_LEFT:
return 'kleft'
elif event.key == pygame.K_UP:
return 'kup'
elif event.key == pygame.K_DOWN:
return 'kdown'
elif event.key == pygame.K_CAPSLOCK:
return None
elif event.key == 282:
return 'paste'
elif event.key == 283:
return 'begincur'
elif event.key == 284:
return 'endcur'
elif event.key == 285:
return 'delall'
else:
return event.unicode
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if self.show_msg == False:
if event.type == pygame.KEYDOWN:
self.auto_check_reset()
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
self.home_sqare_switch(self.board.active_ship + 1)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
self.home_sqare_switch(self.board.active_ship - 1)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
self.home_sqare_switch(self.board.active_ship - self.sumn1n2sl + 1)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
self.home_sqare_switch(self.board.active_ship + self.sumn1n2sl)
elif event.type == pygame.KEYDOWN and event.key != pygame.K_RETURN and not self.correct:
lhv = len(self.home_square.value)
self.changed_since_check = True
if event.key == pygame.K_BACKSPACE:
if lhv > 0:
self.home_square.value = self.home_square.value[0:lhv - 1]
else:
char = event.unicode
if (len(char) > 0 and lhv < 2 and char in self.digits):
if self.home_square in self.resultl:
if lhv == 1:
s = self.home_square.value + char
if s[0] == "0":
self.home_square.value = char
else:
n = int(s)
if n < 20:
self.home_square.value = str(n % 10)
try:
self.carryl[self.home_square.pos_id].value = "1"
self.carryl[self.home_square.pos_id].update_me = True
except:
pass
else:
self.home_square.value = char
else:
self.home_square.value = char
elif self.home_square in self.carryl:
if char == "1":
self.home_square.value = char
else:
self.home_square.value = ""
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
elif event.type == pygame.MOUSEBUTTONUP:
self.home_sqare_switch(self.board.active_ship)
elif event.type == pygame.MOUSEBUTTONDOWN:
self.auto_check_reset()
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if self.show_msg == False:
if event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN:
self.auto_check_reset()
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
self.home_sqare_switch(self.board.active_ship + 1)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
self.home_sqare_switch(self.board.active_ship - 1)
elif event.type == pygame.KEYDOWN and event.key != pygame.K_RETURN and not self.correct:
lhv = len(self.home_square.value)
self.changed_since_check = True
if event.key == pygame.K_BACKSPACE:
if lhv > 0:
self.home_square.value = self.home_square.value[0:lhv - 1]
else:
char = event.unicode
if (len(char) > 0 and lhv < 2 and char in self.digits):
if True:
if lhv == 1:
s = self.home_square.value + char
if s[0] == "0":
self.home_square.value = char
else:
n = int(s)
if n < 20:
self.home_square.value = str(n % 10)
try:
self.carryl[self.home_square.pos_id].value = "1"
self.carryl[self.home_square.pos_id].update_me = True
except:
pass
else:
self.home_square.value = char
else:
self.home_square.value = char
elif self.home_square in self.carryl:
if char == "1":
self.home_square.value = char
else:
self.home_square.value = ""
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
elif event.type == pygame.MOUSEBUTTONUP:
self.home_sqare_switch(self.board.active_ship)
def handle(self, event):
gd.BoardGame.handle(self, event) # send event handling up
if self.show_msg == False:
if event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN:
self.auto_check_reset()
if event.type == pygame.KEYDOWN and event.key != pygame.K_RETURN and not self.correct:
lhv = len(self.home_square.value)
self.changed_since_check = True
if event.key == pygame.K_BACKSPACE:
if lhv > 0:
self.home_square.value = self.home_square.value[0:lhv - 1]
else:
char = event.unicode
if (len(char) > 0 and lhv < 3 and char in self.digits):
if lhv == 0:
self.home_square.value += char
elif lhv == 1:
if self.home_square == self.ans_h:
if self.show_24h:
n = int(self.home_square.value + char)
if n > 23:
self.home_square.value = char
else:
self.home_square.value += char
else:
n = int(self.home_square.value + char)
if n > 12:
self.home_square.value = char
else:
self.home_square.value += char
if self.home_square == self.ans_m:
n = int(self.home_square.value + char)
if n > 59:
self.home_square.value = char
else:
self.home_square.value += char
elif lhv == 2:
self.home_square.value = char
if len(self.ans_h.value.strip()) > 0:
if self.home_square == self.ans_h and self.time[0] == int(self.ans_h.value):
self.next_field()
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
elif event.type == pygame.KEYDOWN and (
event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER) and self.board.active_ship == self.ans_h.unit_id:
if len(self.ans_h.value.strip()) > 0 and self.time[0] == int(self.ans_h.value):
self.next_field()
elif event.type == pygame.MOUSEBUTTONUP:
self.home_square.update_me = True
if self.board.active_ship == self.ans_h.unit_id:
self.home_square.perm_outline_width = 5
self.home_square = self.ans_h
self.board.active_ship = self.ans_h.unit_id
elif self.board.active_ship == self.ans_m.unit_id:
self.home_square.perm_outline_width = 5
self.home_square = self.ans_m
self.board.active_ship = self.ans_m.unit_id
self.home_square.update_me = True
self.mainloop.redraw_needed[0] = True
def get_input():
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYUP and event.key == pygame.K_s:
state.falling[0] = False
if event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
state.falling[1] = False
if event.type == pygame.KEYDOWN:
if state.state == "playing":
if event.key == pygame.K_p:
state.state = "paused"
if event.key == pygame.K_a:
if valid(state.X[0] - 1, state.Y[0], state.piece[0].form, 0):
state.X[0] -= 1
if event.key == pygame.K_d:
if valid(state.X[0] + 1, state.Y[0], state.piece[0].form, 0):
state.X[0] += 1
if event.key == pygame.K_LEFT:
if valid(state.X[1] - 1, state.Y[1], state.piece[1].form, 1):
state.X[1] -= 1
if event.key == pygame.K_RIGHT:
if valid(state.X[1] + 1, state.Y[1], state.piece[1].form, 1):
state.X[1] += 1
if event.key == pygame.K_w:
new_form = Polyomino.rotate_right(state.piece[0].form,
state.piece[0].size)
if valid(state.X[0], state.Y[0], new_form, 0):
state.piece[0].form = new_form
if event.key == pygame.K_UP:
new_form = Polyomino.rotate_right(state.piece[1].form,
state.piece[1].size)
if valid(state.X[1], state.Y[1], new_form, 1):
state.piece[1].form = new_form
if event.key == pygame.K_s:
state.falling[0] = True
if event.key == pygame.K_DOWN:
state.falling[1] = True
elif state.state == "paused":
if event.key == pygame.K_p:
state.state = "playing"
elif state.state == "entering high score":
if event.key == pygame.K_BACKSPACE and state.cursorpos > 0:
state.cursorpos -= 1
state.hiscores[state.scorepos][0] = \
state.hiscores[state.scorepos][0][:state.cursorpos]
elif event.key == pygame.K_RETURN:
scorefile = open("hiscores_duo.csv", "w")
writer = csv.writer(scorefile, dialect = csv.unix_dialect)
for row in state.hiscores:
writer.writerow(row)
scorefile.close()
state.start_game()
elif (event.unicode.isalpha() or event.key == pygame.K_SPACE) \
and state.cursorpos < INFO_WIDTH - 3 - len(str(state.score)):
state.hiscores[state.scorepos][0] = \
state.hiscores[state.scorepos][0][:state.cursorpos] \
+ event.unicode
state.cursorpos += 1
elif state.state == "lost":
if event.key in [pygame.K_SPACE, pygame.K_RETURN]:
state.start_game()