Advertisement
THOMASBOIMAH07778730

robot.py

Oct 14th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. from gasp import *
  2. from random import randint
  3.  
  4. def place_robot():
  5.  
  6. global robot_x, robot_y, robot_shape
  7.  
  8. robot_x = randint(0, 47)
  9. robot_y = randint(0, 63)
  10. robot_shape = Box((5, 5), 10, 10 )
  11.  
  12.  
  13. def place_player():
  14.  
  15. global player_x, player_y, player_shape
  16.  
  17. player_x = randint(0, 63)
  18. player_y = randint(0, 47)
  19. player_shape = Circle((10 * player_x + 5, 10 * player_y + 5), 5, filled = True)
  20.  
  21. def move_player():
  22. global player_x, player_y, Player_shape
  23.  
  24. key = update_when('key_pressed')
  25. if key == 'h' and player_x > 0:
  26. player_x -= 1
  27. elif key == 'j' and player_y < 47:
  28. player_y -= 1
  29. elif key == 'k' and player_y > 0:
  30. player_y += 1
  31. elif key == 'l' and player_x < 63:
  32. player_x += 1
  33. # Custom diagonal keys for laptops without numeric keypad
  34. elif key == 'u': # move left and up
  35. if player_x > 0:
  36. player_x -= 1
  37. if player_y < 47:
  38. player_y += 1
  39. elif key == 'i': # move right and up
  40. if player_x < 63:
  41. player_x += 1
  42. if player_y < 47:
  43. player_y += 1
  44. elif key == 'n': # move left and down
  45. if player_x > 0:
  46. player_x -= 1
  47. if player_y > 0:
  48. player_y -= 1
  49. elif key == 'm': # move right and down
  50. if player_x < 63:
  51. player_x += 1
  52. if player_y > 0:
  53. player_y += 1
  54. move_to( player_shape, (10 * player_x + 5, 10 * player_y + 5))
  55.  
  56. def move_robot():
  57. global robot_x, robot_y, robot_shape
  58.  
  59. key = update_when('key_pressed')
  60. if key == 'h' and robot_x > 0:
  61. robot_x += 1
  62. elif key == 'j' and robot_y < 47:
  63. robot_y += 1
  64. elif key == 'k' and robot_y > 0:
  65. robot_y -= 1
  66. elif key == 'l' and robot_x < 63:
  67. robot_x -= 1
  68. # Custom diagonal keys for laptops without numeric keypad
  69. elif key == 'u': # move left and up
  70. if robot_x > 0:
  71. robot_x += 1
  72. if robot_y < 47:
  73. robot_y -= 1
  74. elif key == 'i': # move right and up
  75. if robot_x < 63:
  76. robot_x += 1
  77. if robot_y < 47:
  78. robot_y -= 1
  79. elif key == 'n': # move left and down
  80. if robot_x > 0:
  81. robot_x -= 1
  82. if robot_y > 0:
  83. robot_y -= 1
  84. elif key == 'm': # move right and down
  85. if robot_x < 63:
  86. robot_x -= 1
  87. if robot_y > 0:
  88. robot_y -=1
  89. move_to( robot_shape, (10 * robot_x + 5, 10 * robot_y + 5))
  90.  
  91. def collided():
  92. if player_x == robot_x and player_y == robot_y:
  93. key = 'you have been caught'
  94. key_text = Text (key, (320, 240), size=28)
  95. sleep(30)
  96. while player and robot != collided:
  97. place_player = randint(0, 96)
  98. def safely_place_player():
  99. place_player()
  100.  
  101. while collided():
  102. place_player()
  103.  
  104. begin_graphics()
  105. finished = False
  106. place_player()
  107. place_robot()
  108.  
  109. while not finished:
  110. move_robot()
  111. move_player()
  112. collided()
  113.  
  114.  
  115.  
  116. end_graphics()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement