Advertisement
jelkner

robots_moving_player

Sep 17th, 2022
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. from gasp import *
  2. from random import randint
  3.  
  4.  
  5. def place_player():
  6.     global player_x, player_y, player_shape
  7.  
  8.     player_x =  randint(0, 63)
  9.     player_y =  randint(0, 47)
  10.     player_shape = Circle((10 * player_x + 5, 10 * player_y + 5), 5)
  11.  
  12.  
  13. def move_player():
  14.     global player_x, player_y, player_shape
  15.  
  16.     key = update_when('key_pressed')
  17.  
  18.     # Use vim keys for left, down, up, and right
  19.     if key == 'h' and player_x > 0:
  20.         player_x -= 1
  21.     elif key == 'j' and player_y > 0:
  22.         player_y -= 1
  23.     elif key == 'k' and player_y < 47:
  24.         player_y += 1
  25.     elif key == 'l' and player_x < 63:
  26.         player_x += 1
  27.     # Custom diagonal keys for laptops without numeric keypad
  28.     elif key == 'u':                   # move left and up
  29.         if player_x > 0:
  30.             player_x -= 1
  31.         if player_y < 47:
  32.             player_y += 1
  33.     elif key == 'i':                   # move right and up
  34.         if player_x < 63:
  35.             player_x += 1
  36.         if player_y < 47:
  37.             player_y += 1
  38.     elif key == 'n':                   # move left and down
  39.         if player_x > 0:
  40.             player_x -= 1
  41.         if player_y > 0:
  42.             player_y -= 1
  43.     elif key == 'm':                   # move right and down
  44.         if player_x < 63:
  45.             player_x += 1
  46.         if player_y > 0:
  47.             player_y -= 1
  48.  
  49.     move_to(player_shape, (10 * player_x + 5, 10 * player_y + 5))
  50.  
  51.  
  52. begin_graphics()
  53. finished = False
  54.  
  55. place_player()
  56.  
  57. while not finished:
  58.     move_player()
  59.  
  60. end_graphics()
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement