10 examples of 'pygame key press example' in Python

Every line of 'pygame key press example' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
122def on_key_press(self, key, modifiers):
123 """
124 Called whenever the mouse moves.
125 """
126 if key == arcade.key.UP:
127 self.player.change_y = MOVEMENT_SPEED
128 elif key == arcade.key.DOWN:
129 self.player.change_y = -MOVEMENT_SPEED
130 elif key == arcade.key.LEFT:
131 self.player.change_x = -MOVEMENT_SPEED
132 elif key == arcade.key.RIGHT:
133 self.player.change_x = MOVEMENT_SPEED
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

146def key_down(self, key):
147 if key in self.map:
148 self.velocity = V2((3 / self.scale, 0) if key in self.map[:2] else (0, 3 / self.scale)).elementwise() * (
149 (self.map.index(key) not in (1, 2)) * 2 - 1)
68def on_key_press(self, key, modifiers):
69 """Called whenever a key is pressed. """
70 if key == arcade.key.F:
71 # User hits f. Flip between full and not full screen.
72 self.is_full_screen = not self.is_full_screen
73 self.set_fullscreen(self.is_full_screen)
74
75 # Get the window coordinates. Match viewport to window coordinates
76 # so there is a one-to-one mapping.
77 width, height = self.get_size()
78 self.set_viewport(0, width, 0, height)
79
80 if key == arcade.key.S:
81 # User hits s. Flip between full and not full screen.
82 self.is_full_screen = not self.is_full_screen
83 self.set_fullscreen(self.is_full_screen)
84
85 # Instead of a one-to-one mapping, stretch/squash window to match the
86 # constants. This does NOT respect aspect ratio. You'd need to
87 # do a bit of math for that.
88 self.set_viewport(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT)
168def key_handler_func(self, sourceType, sourceIndex, key, value):
169 if self.debug:
170 print("key="+str(key), pi3d.event.Event.code_to_key(key),"[",sourceIndex,"] =",value)
171
172 if DEFAULT_KEY_FORWARD == key:
173 self.is_forward_pressed = value > 0
174
175 elif DEFAULT_KEY_BACKWARD == key:
176 self.is_backward_pressed = value > 0
177
178 elif DEFAULT_KEY_STRAFE_RIGHT == key:
179 self.is_strafe_right_pressed = value > 0
180
181 elif DEFAULT_KEY_STRAFE_LEFT == key:
182 self.is_strafe_left_pressed = value > 0
183
184
185 elif ( (DEFAULT_KEY_ACTION == key) or (DEFAULT_JOYPAD_BUTTON_ACTION == key) or (DEFAULT_MOUSE_BUTTON_ACTION == key)):
186 if value == 0:
187 self.on_action_released()
188 elif value == 1:
189 self.on_action_pressed()
190
191 elif ( (DEFAULT_KEY_JUMP == key) or (DEFAULT_JOYPAD_BUTTON_JUMP == key) or (DEFAULT_MOUSE_BUTTON_JUMP == key)):
192 if value == 0:
193 self.on_jump_released()
194 elif value == 1:
195 self.on_jump_pressed()
196
197 elif DEFAULT_KEY_SCREENSHOT == key and value == 1:
198 pi3d.screenshot("vr_screenshot_"+str(self.screenshot_count)+".jpg")
199 self.screenshot_count += 1
200
201 elif DEFAULT_KEY_QUIT == key or DEFAULT_JOYPAD_BUTTON_QUIT == key:
202 self.stop()
85def key_right(self):
86 self.PLAYER.PLAYER_POS[0] += self.movement
87 self.PLAYER.DIRECTION = 'r'
88
89 self.PLAYER.SPRITE_POS = pygame.image.load(r_images[self.counter])
90 self.counter = (self.counter + 1) % len(r_images)
91
92 if self.PLAYER.TRANSFORM:
93 self.PLAYER.WOLF = pygame.image.load(wolf_r_images[self.wolf_counter_lr])
94 self.wolf_counter_lr = (self.wolf_counter_lr + 1) % len(wolf_r_images)
81def pressTest(self, key):
82 print( "-> Pressing " + key.upper() )
180def handle_input(self):
181 try:
182 event = pygame.event.wait()
183
184 if event.type == QUIT:
185 self.exit_status = 0
186 self.running = False
187
188 elif event.type == KEYDOWN:
189 if event.key == K_ESCAPE:
190 self.exit_status = 0
191 self.running = False
192 else:
193 self.running = False
194
195 elif event.type == VIDEORESIZE:
196 init_screen(event.w, event.h)
197 self.dirty = True
198
199 except KeyboardInterrupt:
200 self.exit_status = 0
201 self.running = False
189def on_key_release(self, key, modifiers):
190 """
191 Called when the user releases a key.
192 """
193 if key == arcade.key.UP or key == arcade.key.DOWN:
194 self.player.change_y = 0
195 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
196 self.player.change_x = 0
92def on_key_press(self, k, mod):
93 numbers = [key._1, key._2, key._3, key._4, key._5,
94 key._6, key._7, key._8, key._9, key._0 ]
95 if k == key.T:
96 # track if the user wants to translate origin to the current
97 # skeleton position
98 # if you run two walk left animations without translation
99 # you will see a player move left, go to the origin and move
100 # left again.
101 # if you use translation, the player will just move left twice
102 # as far
103 self.translate = not self.translate
104 if k == key.F:
105 # track if the user wants to run the animation normal or flipped
106 # if the animation is a guy walking left, when flipped it will
107 # walk right
108 self.flipped = not self.flipped
109 self.skin.flip()
110
111 if k in numbers:
112 # find which animation the user wants to run
113 n = numbers.index(k)
114 if n < len(self.anims):
115 # kill current animations
116 self.skin.stop()
117 anim = self.anims[n]
118 # if we want to run the animation flipped, we create
119 # the flipped version
120 if self.flipped:
121 anim = anim.flipped()
122 # we run the animation on the skin using the Animate action.
123 # remember that Animate is a cocos action, so you can do
124 # any action stuff you want with them.
125 # you just have to say which animation you want to use
126 # and what kind of translation
127 self.skin.do( Animate( anim , recenter_x=self.translate ) )
95def checkForKeyPress():
96 if len(pygame.event.get(QUIT)) > 0:
97 terminate()
98
99 keyUpEvents = pygame.event.get(KEYUP)
100 if len(keyUpEvents) == 0:
101 return None
102 if keyUpEvents[0].key == K_ESCAPE:
103 terminate()
104 return keyUpEvents[0].key

Related snippets