我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pygame.K_UP。
def _handleKeypress(self, key): if self._clicked == None or self._moveEvent == None: return False column = self._clicked[0] row = self._clicked[1] target = None if key == pygame.K_LEFT: if column > 0: target = (column-1, row) elif key == pygame.K_RIGHT: if column < self._map.cols - 1: target = (column+1, row) elif key == pygame.K_UP: if row > 0: target = (column, row-1) elif key == pygame.K_DOWN: if row < self._map.rows - 1: target = (column, row+1) if target != None: self._moveEvent(self._clicked, target) self._clicked = target
def keyboard_loop(self): while not rospy.is_shutdown(): acc = 0 yaw = 0 keys = pygame.key.get_pressed() for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() if(keys[pygame.K_UP]): acc = self.acc elif(keys[pygame.K_DOWN]): acc = -self.acc if(keys[pygame.K_LEFT]): yaw = self.yaw elif(keys[pygame.K_RIGHT]): yaw = -self.yaw self.send_control(acc, yaw) self.rate.sleep()
def handleEvent(self, event): if event.type == pg.KEYDOWN: if event.key == pg.K_UP: self.arrow.moveUp() if event.key == pg.K_DOWN: self.arrow.moveDown() if event.key == pg.K_RETURN: self.audio.state = "enter" if self.arrow.index == 0: self.messageMenu("transition", "blank_menu") self.messageCutScene("transition", "office_cutscene") if self.arrow.index == 1: self.messageMenu("transition", "blank_menu") self.messageScene("start_game", "coop") if self.arrow.index == 2: self.messageMenu("transition", "options_menu") if self.arrow.index == 3: quit()
def handle_event(self, event): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.update('left') if event.key == pygame.K_RIGHT: self.update('right') if event.key == pygame.K_UP: self.update('up') if event.key == pygame.K_DOWN: self.update('down') if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: self.update('stand_left') if event.key == pygame.K_RIGHT: self.update('stand_right') if event.key == pygame.K_UP: self.update('stand_up') if event.key == pygame.K_DOWN: self.update('stand_down')
def update_cursor(self, keys): """Update the position of the cursor""" input_list = [pg.K_RETURN, pg.K_a, pg.K_s] if self.cursor.state == c.PLAYER1: self.cursor.rect.y = 358 if keys[pg.K_DOWN]: self.cursor.state = c.PLAYER2 for input in input_list: if keys[input]: self.reset_game_info() self.done = True elif self.cursor.state == c.PLAYER2: self.cursor.rect.y = 403 if keys[pg.K_UP]: self.cursor.state = c.PLAYER1
def CheckKeys(self): """ Check the keys that are evaluated on every main loop iteration. I.e., they aren't just evaluated when first pressed down """ pygame.event.pump() self.keys = keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.viewCenter -= (0.5, 0) elif keys[pygame.K_RIGHT]: self.viewCenter += (0.5, 0) if keys[pygame.K_UP]: self.viewCenter += (0, 0.5) elif keys[pygame.K_DOWN]: self.viewCenter -= (0, 0.5) if keys[pygame.K_HOME]: self.viewZoom = 1.0 self.viewCenter = (0.0, 20.0)
def showscreen(): BG() draw_text(screen, "asteroid invasion", 50, WIDTH / 2, HEIGHT / 4) draw_text(screen, "left and right arrow to move", 20,WIDTH / 2, HEIGHT / 2-12.5) draw_text(screen, "press space to shoot", 20,WIDTH / 2, HEIGHT / 2-50) draw_text(screen, "if red bar turns green ", 20,WIDTH / 2, HEIGHT / 2+22) draw_text(screen, "press arrow up to use sonicshot", 20,WIDTH / 2, HEIGHT / 2+44) draw_text(screen,'highscore: '+str(highscore),18,WIDTH/2,400) draw_text(screen, "Press up arrow to start", 18, WIDTH / 2, HEIGHT * 3 / 4) pygame.display.flip() waiting = True while waiting: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: exit() pygame.exit() if event.type == pygame.KEYUP: if event.key == pygame.K_UP: waiting = False
def notify(self, event): if self.isActivated: if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: self.optionList[self.selector.selectedIndex].deselect() self.selector.moveUp() self.scrollUp() self.optionList[self.selector.selectedIndex].select() elif event.key == pygame.K_DOWN: self.optionList[self.selector.selectedIndex].deselect() self.selector.moveDown() self.scrollDown() self.optionList[self.selector.selectedIndex].select() elif event.key == pygame.K_SPACE: self.chosenOptionIndex = self.selectedIndex() self.isActivated = False elif event.key == pygame.K_RETURN: self.chosenOptionIndex = self.selectedIndex() self.isActivated = False
def __init__(self, key_list=None): """ :param Union[list,None] key_list: the list of keys to be added right away to our keyboard_registry dict """ super().__init__() # stores the keys that we would like to be registered as important # - key: pygame keyboard code (e.g. pygame.K_ESCAPE, pygame.K_UP, etc..) # - value: True if currently pressed, False otherwise # - needs to be ticked in order to yield up-to-date information (this will be done by a GameLoop playing a Screen) self.keyboard_registry = {} self.descriptions = {} if key_list is None: key_list = ["up", "down", "left", "right"] self.update_keys(key_list)
def update_keys(self, new_key_list=None): """ Populates our registry and other dicts with the new key-list given (may be an empty list). :param Union[List,None] new_key_list: the new key list, where each item is the lower-case pygame keycode without the leading `K_` e.g. `up` for pygame.K_UP; use None for clearing out the registry (no keys assigned) """ self.unregister_events() self.keyboard_registry.clear() self.descriptions.clear() #OBSOLETE: self.desc_to_key.clear() if new_key_list: for desc in new_key_list: key = getattr(pygame, "K_" + (desc.upper() if len(desc) > 1 else desc)) self.keyboard_registry[key] = False self.descriptions[key] = desc #OBSOLETE: self.desc_to_key[desc] = key # signal that we might trigger the following events: self.register_event("key_down." + desc, "key_up." + desc)
def __init__(self, key, command, flags=0, other_command=None, animation_to_complete=None): """ :param str key: the key's description, e.g. `up` for K_UP :param str command: the `main` command's description; can be any string e.g. `fire`, `jump` :param int flags: keyboard-command behavior flags :param str other_command: a possible second command associated with the key (when key is released, e.g. `release_bow`) :param Union[list,str] animation_to_complete: animation(s) that needs to be completed in order for the other_command to be executable """ self.key = key self.command = command assert flags & (self.BLOCK_REPEAT_UNTIL_ANIM_COMPLETE | self.BLOCK_OTHER_CMD_UNTIL_ANIM_COMPLETE) == 0 or \ isinstance(animation_to_complete, str) or isinstance(animation_to_complete, set), "ERROR: animation_to_complete needs to be of type str or set!" self.flags = flags self.other_command = other_command # this could be a set of anims (one of them has to be completed) self.animation_to_complete = {animation_to_complete} if isinstance(animation_to_complete, str) else animation_to_complete self.state_other_command = 0 # the current state for the other_command (charging, charged, cmd_received) self.is_disabled = False # set to True for temporarily blocking this translation
def __init__(self, gameInstance): BaseController.__init__(self, gameInstance) keys = ( \ pygame.K_LEFT, #left pygame.K_RIGHT,#right pygame.K_UP, #acc pygame.K_DOWN, #brake pygame.K_LCTRL,#reverse pygame.K_r, #restart #pygame.K_ESC, #endGame pygame.K_RCTRL, #pygame.K_SPACE, \ ) self.keyToAction = dict() for key, action in zip(keys, self.gameInterface.gameInstance.actionsList): self.keyToAction.update({key: action}) self.keyToAction[pygame.K_RCTRL] = self.keyToAction[pygame.K_LCTRL]
def test_input(self): i = inputs.Input() test_inputs = [(pygame.KEYDOWN, {'key':pygame.K_1}), (pygame.KEYDOWN, {'key':pygame.K_2}), (pygame.KEYDOWN, {'key':pygame.K_2}), (pygame.KEYDOWN, {'key':pygame.K_UP}), (pygame.KEYDOWN, {'key':pygame.K_LEFT}), (pygame.KEYUP, {'key':pygame.K_r}), # (pygame.KEYDOWN, {'key':pygame.K_q}) ] for e in [pygame.event.Event(*x) for x in test_inputs]: i.handle_events(e) self.assertEqual(i.get_swcha(), 0xAF) self.assertEqual(i.get_swchb(), 0x7F) self.assertEqual(i.get_paddle0(), 0x1) self.assertEqual(i.get_input7(), 0xFF)
def handleEvents(self, events): for event in events: if event.type == pygame.QUIT: self.doQuit() return elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.doQuit() return elif event.key == pygame.K_DOWN: self.current_option += 1 if self.current_option >= len(self.options): self.current_option = 0 elif event.key == pygame.K_UP: self.current_option -= 1 if self.current_option < 0: self.current_option = len(self.options)-1 elif event.key == pygame.K_RETURN: self.currentOptionExecute() return
def keyboard(self, key): """ Handle keyboard events. :key: key pressed """ global SCALE_FACTOR, MIN_MASS if key == pygame.K_SPACE: self.pause = not self.pause elif key == pygame.K_UP: self.pos_shift[1] -= int(self.height / 10) elif key == pygame.K_DOWN: self.pos_shift[1] += int(self.height / 10) elif key == pygame.K_LEFT: self.pos_shift[0] -= int(self.width / 10) elif key == pygame.K_RIGHT: self.pos_shift[0] += int(self.width / 10) elif key == pygame.K_EQUALS: SCALE_FACTOR /= 1.05 elif key == pygame.K_MINUS: SCALE_FACTOR *= 1.05 MIN_MASS = SCALE_FACTOR ** 3
def handle_events(): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit(0) if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: player_one.speed = 10 if event.key == pygame.K_RIGHT: player_one.angular_speed = 5 if event.key == pygame.K_LEFT: player_one.angular_speed = -5 if event.type == pygame.KEYUP: if event.key == pygame.K_UP: player_one.speed = 0 if event.key == pygame.K_RIGHT: player_one.angular_speed = 0 if event.key == pygame.K_LEFT: player_one.angular_speed = 0
def handle_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: self.up_pressed = True elif event.key == pygame.K_DOWN: self.down_pressed = True elif event.key == pygame.K_LEFT: self.left_pressed = True elif event.key == pygame.K_RIGHT: self.right_pressed = True elif event.key == pygame.K_ESCAPE: exit() elif event.type == pygame.KEYUP: if event.key == pygame.K_UP: self.up_pressed = False elif event.key == pygame.K_DOWN: self.down_pressed = False elif event.key == pygame.K_LEFT: self.left_pressed = False elif event.key == pygame.K_RIGHT: self.right_pressed = False
def get_event(self, event): if event.type == pg.QUIT: self.quit = True elif event.type == pg.KEYUP: if event.key == pg.K_ESCAPE: self.quit = True elif event.key == pg.K_SPACE: self.running_sim = not self.running_sim elif event.key == pg.K_UP: if self.tick_index < len(self.tick_lengths) - 1: self.tick_index += 1 self.tick_length = self.tick_lengths[self.tick_index] elif event.key == pg.K_DOWN: if self.tick_index > 0: self.tick_index -= 1 self.tick_length = self.tick_lengths[self.tick_index]
def run(self): while not self.is_done: time.sleep(0.01) x, y = pygame.mouse.get_pos() events = pygame.event.get() for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: Kuai.emit('key-up', x, y) if event.key == pygame.K_DOWN: Kuai.emit('key-down', x, y) if event.key == pygame.K_LEFT: Kuai.emit('key-left', x, y) if event.key == pygame.K_RIGHT: Kuai.emit('key-right', x, y) if event.key == pygame.K_ESCAPE: Kuai.emit('key-escape') pass
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 keyboard_loop(self): while not rospy.is_shutdown(): acc = 0 yaw = 0 keys = pygame.key.get_pressed() for event in pygame.event.get(): if event.type==pygame.QUIT:sys.exit() if(keys[pygame.K_s]): self.send_highway_start(1) if(keys[pygame.K_t]): self.send_highway_start(2) if(keys[pygame.K_UP]): acc = self.acc elif(keys[pygame.K_DOWN]): acc = -self.acc if(keys[pygame.K_LEFT]): yaw = self.yaw elif(keys[pygame.K_RIGHT]): yaw = -self.yaw if(keys[pygame.K_r]): state = 1 self.send_record_state(state) elif(keys[pygame.K_q]): state = 2 self.send_record_state(state) elif(keys[pygame.K_p]): state = 0 self.send_record_state(state) self.send_control(acc, yaw) self.rate.sleep()
def handleEvent(self, event): if event.type == pg.KEYDOWN: if event.key == pg.K_ESCAPE: self.effect.timeStartDarken = self.effect.time self.effect.timeEndDarken = self.effect.time + self.dt self.audio.state = "exit" if event.key == pg.K_UP: self.arrow.moveUp() if event.key == pg.K_DOWN: self.arrow.moveDown() if self.arrow.index == 0: if event.key == pg.K_RIGHT: self.backgroundSetting.next() if event.key == pg.K_LEFT: self.backgroundSetting.previous() if event.key == pg.K_RETURN: if self.backgroundSetting.index == 0: self.render.flip(True, False) if self.backgroundSetting.index == 1: self.render.flip(False, True) if self.arrow.index == 1: if event.key == pg.K_RIGHT: self.fullscreenSetting.next() if event.key == pg.K_LEFT: self.fullscreenSetting.previous() if event.key == pg.K_RETURN: if self.fullscreenSetting.index == 0: pg.display.set_mode((settings.WIDTH, settings.HEIGHT)) if self.fullscreenSetting.index == 1: pg.display.set_mode((settings.WIDTH, settings.HEIGHT), pg.FULLSCREEN) self.messageMenu("screen") self.messageScene("screen") self.messageCutScene("screen")
def __init__(self, dims): self.position, self.velocity, self.dims, self.scale, self.map = V2(0, 0), V2(0, 0), dims, 1, [pg.K_RIGHT, pg.K_LEFT, pg.K_UP, pg.K_DOWN]
def action_from_keys(self, keys): go = np.zeros(self.action_dim) if keys[pygame.K_LEFT]: go[-1] = self.max_deg if keys[pygame.K_RIGHT]: go[-1] = -self.max_deg if keys[pygame.K_UP]: go[0] = 10 if keys[pygame.K_DOWN]: go[0] = -10 return go
def on_key_press(self, event): if event.type == pygame.KEYDOWN and event.key == pygame.K_UP: self.scroll(50) elif event.type == pygame.KEYDOWN and event.key == pygame.K_PAGEUP: self.scroll(500) elif event.type == pygame.KEYDOWN and event.key == pygame.K_HOME: self.scroll('home') elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN: self.scroll(-50) elif event.type == pygame.KEYDOWN and event.key == pygame.K_PAGEDOWN: self.scroll(-500) elif event.type == pygame.KEYDOWN and event.key == pygame.K_END: self.scroll('end')
def notify(self, event): if self.isActivated: if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: pass elif event.key == pygame.K_DOWN: pass elif event.key == pygame.K_SPACE: self.isActivated = False elif event.key == pygame.K_RETURN: pass
def notify(self, event): if self.selected: if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: self.optionList[self.selector.vPos].deselect() self.selector.moveUp() self.optionList[self.selector.vPos].select() elif event.key == pygame.K_DOWN: self.optionList[self.selector.vPos].deselect() self.selector.moveDown() self.optionList[self.selector.vPos].select() elif event.key == pygame.K_SPACE: self.optionList[self.selector.vPos].doOption() elif event.key == pygame.K_RETURN: self.optionList[self.selector.vPos].doOption()
def update(self, is_key_pressed, time, display): """ ????????????????????????????? is_key_pressed ?????????????ascii???????????? time ???? display ????????Display?? """ if (is_key_pressed(pygame.K_e)): if self.theme_changed: display.theme_index = (display.theme_index + 1) % len(self.themes) display.theme = self.themes[display.theme_index] self.theme_changed = False elif not self.theme_changed: self.theme_changed = True flag = False if (is_key_pressed(pygame.K_UP)): self._up(display, time) flag = True if (is_key_pressed(pygame.K_DOWN)): self._down(display, time) flag = True if (is_key_pressed(pygame.K_LEFT)): self._left(display, time) flag = True if (is_key_pressed(pygame.K_RIGHT)): self._right(display, time) flag = True if self.counter >= 3: self.counter = 0 return flag else: self.counter += 1 return False
def update(self, screen, event_queue, dt,clock,joystick, netmodel, vizmodel): # Logos/titles screen.blit(self.logo,(screen.get_width() / 4 - 265,screen.get_height() * 3 / 4-500)) screen.blit(self.intel,(screen.get_width() / 4 - 300,screen.get_height()-130)) screen.blit(self.activestate,(screen.get_width() - 980,screen.get_height() - 130)) nextState = self displaytext('Play', 32, screen.get_width() / 4 - 20, screen.get_height() * 3 / 4 - 80, WHITE, screen) displaytext('Train', 32, screen.get_width() / 4 - 20, screen.get_height() * 3 / 4 - 40, WHITE, screen) displaytext('Exit', 32, screen.get_width() / 4 - 20, screen.get_height() * 3 / 4, WHITE, screen) displaytext(u'\u00bb', 32, screen.get_width() / 4 - 60, screen.get_height() * 3 / 4 - 40*self.menu_selection, WHITE, screen) # Each game state processes its own input queue in its own way to avoid messy input logic for event in event_queue: if event.type == pygame.KEYDOWN or event.type == pygame.JOYBUTTONDOWN: if (event.type == pygame.KEYDOWN and (event.key == pygame.K_DOWN)) or (event.type == pygame.JOYBUTTONDOWN and (event.button == 1)) or (event.type == pygame.JOYAXISMOTION and (event.axis == 1 or event.value >= DEADZONE)): self.menu_selection -= 1 if self.menu_selection == -1: self.menu_selection = 2 if (event.type == pygame.KEYDOWN and (event.key == pygame.K_UP)) or (event.type == pygame.JOYBUTTONDOWN and (event.button == 0)) or (event.type == pygame.JOYAXISMOTION and (event.axis == 1 or event.value <= -DEADZONE)): self.menu_selection += 1 if self.menu_selection == 3: self.menu_selection = 0 if (event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN) or (event.type == pygame.JOYBUTTONDOWN and event.button == 11): if self.menu_selection == 2: nextState = Play(False) elif self.menu_selection == 1: nextState = Play(True) else: nextState = None if (event.type == pygame.KEYDOWN and event.key == pygame.K_x): self.ExportModel() if (event.type == pygame.KEYDOWN and event.key == pygame.K_d): self.DumpData() if (event.type == pygame.KEYDOWN and event.key == pygame.K_w): self.DumpWeights() return nextState
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 run(self): # Setup for event in pygame.event.get(): if (event.type == pygame.QUIT): return (False, '', None) if (event.type == pygame.KEYDOWN): if (event.key == pygame.K_UP and not self.dino.is_jumping()): self.dino.set_movement(8) self.dino.set_jumping(True) if (event.key == pygame.K_DOWN): self.dino.set_movement(-5) if (event.key == pygame.K_h): if self.dino.get_coins() > 4: self.dino.set_coins(self.dino.get_coins() - 5) self.powers.append(power(self.dino.get_position())) # Movement self.move_all_objects() # Enemies if (self.enemy.check_hitted(self.dino) and not self.enemy.get_hitted()): del self.enemy self.enemy = enemy(self.screen_size, self.ground_limit) self.dino.set_lives(1, type='decrement') if (not self.dino.get_lives()): return(True, 'game_over', None) # Prize self.check_prize_hitted() # Objects to show objects_to_show = {} objects_to_show['lives'] = self.dino.get_lives() objects_to_show['coins'] = self.dino.get_coins() objects_to_show['power'] = self.powers objects_to_show['char'] = self.dino objects_to_show['enemy'] = self.enemy objects_to_show['prize'] = self.prize self.clock.tick(self.difficulty * 60) return (True, 'match_running', objects_to_show)
def button_event(self, button): if button == 1: return pygame.K_LEFT elif button == 3: return pygame.K_RIGHT elif button == 2: return pygame.K_UP elif button == 0: return pygame.K_DOWN
def __init__(self): self.PIN_STATUS = 0 self.PINS = [16, 18] self.K_UP = pygame.K_UP self.foward = 0 self.right = 0 self.left = 0 self.DEBUG = 0
def setControls(self): """ Setups the controls for the class. setGame must be called first in order for this to run correctly """ self.controls = [] self.values = [self.game.board.callable, self.game.board.mvCurUp, self.game.board.mvCurDown, self.game.board.mvCurLeft, self.game.board.mvCurRight] if self.control_set == "arrows": self.keys = [pygame.K_RETURN, pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT] else: self.keys = [pygame.K_SPACE, pygame.K_w, pygame.K_s, pygame.K_a, pygame.K_d] for i in range(len(self.keys)): self.controls.append([self.keys[i], self.values[i]])
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 __init__(self, main_menu): self.parent = main_menu.parent self.main_menu = main_menu self.title_banner = main_menu.title_banner self.parent.event_handler.register_key_listener(self.handle_keypress) self.main_menu.menu.selected = 0 self.animation = None self.directions = { pygame.K_UP : -1, pygame.K_DOWN : 1 }
def update(self, dt, game): key = pygame.key.get_pressed() if key[pygame.K_UP]: self.rect.y -= self.vel * dt if key[pygame.K_DOWN]: self.rect.y += self.vel * dt self.collider(game) #AI Paddle, same as paddle but with pseudo AI for updating position
def run(self): """The mainloop """ #self.paint() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False if event.key==pygame.K_UP: #print(m.active_itemnumber) self.offset_y += 50 #self.sound2.play() if event.key==pygame.K_DOWN: self.offset_y -= 50 if event.key==pygame.K_LEFT: self.x += self.dy if event.key == pygame.K_RIGHT: self.x -= self.dy if event.key == pygame.K_PAGEUP: self.offset_y += self.height if event.key == pygame.K_PAGEDOWN: self.offset_y -= self.height milliseconds = self.clock.tick(self.fps) seconds = milliseconds / 1000.0 # important for python2 self.offset_y -= seconds * 10 # scroll 10 pixels / second if self.offset_y * -1 > self.text_height: running = False self.playtime += milliseconds / 1000.0 self.paint() pygame.display.flip() self.screen.blit(self.background, (0, 0)) #pygame.quit() return
def __init__(self): self._mapping = [ { # Player pygame.K_d:"right", pygame.K_s:"down", pygame.K_q:"left", pygame.K_z:"up" }, { # Player 1 pygame.K_RIGHT:"right", pygame.K_DOWN:"down", pygame.K_LEFT:"left", pygame.K_UP:"up" } ]
def on_event(self, event): print(event) if event.type == pygame.QUIT: self._running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self._running = False if event.key == pygame.K_UP: self.y -= 5 if event.key == pygame.K_DOWN: self.y += 5 if event.key == pygame.K_RIGHT: self.x += 5 if event.key == pygame.K_LEFT: self.x -= 5
def Update(self): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and self.Tile.Left != None: self.Tile = self.Tile.Left elif keys[pygame.K_RIGHT] and self.Tile.Right != None: self.Tile = self.Tile.Right if keys[pygame.K_UP] and self.Tile.Up != None: self.Tile = self.Tile.Up elif keys[pygame.K_DOWN] and self.Tile.Down != None: self.Tile = self.Tile.Down
def play(self): """Initialize the software.""" self.load_score_images() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return 0 if not self.pipes_stop: if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: self.y_increase = -5 # Initialize the bird and the background. self.screen.blit(self.background_img, (0, 0)) self.y_increase += 0.25 # Simulates gravity acceleration self.pipes() # Update pipes # Count the score. self.score() if self.bird_dies(): # Bird dies self.bird() self.gameover() pygame.display.flip() return 1 self.y_increase = 0 self.pipes_stop = True else: self.pipes_stop = False self.bird() self.y_bird += self.y_increase # Update the screen. pygame.display.flip() self.clock.tick(60)