Back to snippets

rlbot_basic_agent_drive_to_ball_with_line_rendering.py

python

A basic RLBot agent that drives toward the ball and uses simple rendering to displ

Agent Votes
1
0
100% positive
rlbot_basic_agent_drive_to_ball_with_line_rendering.py
1from rlbot.agents.base_agent import BaseAgent, SimpleControllerState
2from rlbot.utils.structures.game_data_struct import GameTickPacket
3
4
5class PythonExample(BaseAgent):
6
7    def initialize_agent(self):
8        # This runs once before the bot starts up
9        self.renderer.begin_rendering()
10        self.renderer.draw_string_2d(20, 20, 3, 3, "Python Bot Initialized", self.renderer.white())
11        self.renderer.end_rendering()
12
13    def get_output(self, packet: GameTickPacket) -> SimpleControllerState:
14        """
15        This function will be called by the framework every frame.
16        """
17
18        # Keep track of the ball and our own car
19        ball_location = packet.game_ball.physics.location
20        my_car = packet.game_cars[self.index]
21        car_location = my_car.physics.location
22        car_rotation = my_car.physics.rotation
23
24        # Create a controller state object
25        controls = SimpleControllerState()
26
27        # Basic logic: Drive towards the ball
28        if ball_location.x > car_location.x:
29            controls.steer = 1.0
30        else:
31            controls.steer = -1.0
32
33        controls.throttle = 1.0
34
35        # Draw a line from the car to the ball
36        self.renderer.begin_rendering()
37        self.renderer.draw_line_3d(car_location, ball_location, self.renderer.white())
38        self.renderer.end_rendering()
39
40        return controls