Advertisement
THOMASBOIMAH07778730

robot.py

Oct 8th, 2022
43
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.  
  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
  65.  
  66. key = update_when('key_pressed')
  67. if key == 'h' and robot_x > 0:
  68. robot_x -= 4
  69. elif key == 'j' and robot_y < 47:
  70. robot_y -= 4
  71. elif key == 'k' and robot_y > 0:
  72. robot_y += 4
  73. elif key == 'l' and robot_x < 63:
  74. robot_x += 4
  75. # Custom diagonal keys for laptops without numeric keypad
  76. elif key == 'u': # move left and up
  77. if robot_x > 0:
  78. robot_x -= 1
  79. if robot_y < 47:
  80. robot_y += 1
  81. elif key == 'i': # move right and up
  82. if robot_x < 63:
  83. robot_x += 1
  84. if robot_y < 47:
  85. robot_y += 1
  86. elif key == 'n': # move left and down
  87. if robot_x > 0:
  88. robot_x -= 1
  89. if robot_y > 0:
  90. robot_y -= 1
  91. elif key == 'm': # move right and down
  92. if robot_x < 63:
  93. robot_x += 1
  94. if robot_y > 0:
  95. robot_y -= 1
  96.  
  97. move_to( robot_shape, (10 * robot_x + 5, 10 * robot_y + 5))
  98.  
  99. def check_collisions():
  100. if player_x == robot_x and player_y == robot_y:
  101. key = ('you have been caught Game is over!!!')
  102. key_text = Text(key,(320, 240), size=20)
  103. sleep(30)
  104.  
  105. while True:
  106. check_collisions == check_collisions
  107. break
  108.  
  109.  
  110. begin_graphics()
  111. finished = False
  112. place_robot()
  113. place_player()
  114.  
  115. while not finished:
  116. move_player()
  117. move_robot()
  118. check_collisions()
  119.  
  120.  
  121. end_graphics()
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement