dcammue

robot,py

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