THOMASBOIMAH07778730

robot.py

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