Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Cyber Battle Game
Game elements
Main Game Loop
Final game
25. Simple modification

Instruction

Great! We've made a modification of the code, so now you can decide how robot A moves! See how simple it is. We've only:

  • added a choose_move() function which lets you choose a move for the robot:
    def choose_move():
      move = -1
      while move not in {0, 1, 2}:
        try: move = int(input('Choose an action for robot A (0 - turn right, 1 - shoot, 2 - move forward): '))
        except: move = -1 
      return move
    
  • modified the act_robot() function:
    def act_robot(board, robot, lives_left):
      if robot['name'] == 'A':
        action = choose_move()
      else:
        action = random.randint(0, 2)
      if action == 0:
        turn_right(robot)
      elif action == 1:
        laser_shoot(board, robot, lives_left)
      elif action == 2:
        move_forward(board, robot)
    

Exercise

Play the game as many times as you like. This time you should type 0, 1 or 2 each time when robot A chooses a move. Try to win :) When you're done, click I'm done. Next exercise