Tron - Week 1 Beginner Project

    Tron - Week 1 Beginner Project

    Hello AI club members, we're so excited to kick off the tron project!

    By AI Club on 9/16/2024
    2

    Welcome to the AI Tron AI project, we're so glad you've decided to join us! This week, we're setting up everything you need to start building an AI that plays Tron. Don't worry if you're new to programming, we'll be going through each step together for this first week.

    1. Understanding Tron

    Before we dive into coding, let's understand the game we're working with.

    Task: Play Tron online to familiarize yourself with the game mechanics.

    1. Go to https://www.crazygames.com/game/fl-tron

    2. Play a few rounds against the computer or a friend

    3. Pay attention to:

    • How the light trails work

    • How collisions are handled

    • The strategies you use to avoid collisions and trap opponents

    2. Setting Up Your Development Environment

    Now, let's set up your computer for Python development. We will be using Pygame, OpenAI Gym, and Stable-Baselines3. Again don't worry if you don't know what these are, for today we'll only be using the Pygame library.

    2.1 Installing Python

    1. Go to https://www.python.org/downloads/

    2. Download Python 3.8 or later for your operating system

    3. Run the installer

      • On Windows, make sure to check "Add Python to PATH"

    4. Verify the installation:

      • Open a command prompt or terminal

      • Type python --version and press enter

      • You should see the Python version number

    2.2 Setting Up Your Virtual Environment

    Virtual environments help keep your projects organized. It allows you to install libraries and packages specific to this project without affecting your system's Python installation.

    1. Open a command prompt or terminal in the folder where your repo is cloned.

    2. Create a virtual environment:

      • On Windows:

        • python -m venv venv

      • On MacOS / Linux:

        • python3 -m venv venv

    3. Activate the virtual environment:

      • On Windows:

        • venv/Scripts/activate

      • On MacOS / Linux:

        • source venv/bin/activate

    2.3 Installing Required Libraries

    Now that your virtual environment is active, let's install the necessary libraries.

    1. Ensure you're in your project directory with the virtual environment activated

    2. Install the libraries using pip: pip install pygame gym stable-baselines3

    3. Verify the installations: pip list

    You should see pygame, gym, and stable-baselines3 in the list

    3. Creating Your First Pygame Window

    Let's write some code to create a basic Pygame window.

    1. In the week1 folder of your project directory, create a file named tron_game.py

    2. Copy and paste from the code below this list.

    3. Save the file

    4. Run the script:

      • Make sure you're in the week1 folder in your terminal:

        • cd week1

      • In the terminal (with your virtual environment activated):

      • You should see a window with a black background and a red rectangle

    tron_game.py

    4. Understanding The Code

    Let's break down what each part of the code does:

    • pygame.init(): This initializes all pygame modules.

    • pygame.display.set_mode((WIDTH, HEIGHT)): This creates the game window.

    • The main game loop:

      • Checks for the quit event (closing the window)

      • Fills the screen with black

      • Draws a red rectangle

      • Updates the display

      • pygame.quit(): This closes pygame when we exit the loop

    5. Testing Your Code

    Let's make sure you environment is set up correctly to ensure the coming weeks go smoothly

    1. Make sure you're in the week1 folder in your terminalcd week1

    2. In the week1 folder of your project directory, make a new file called test.py

    3. Copy and paste from the following filetest.py

    4. Run the command python test.py

    5. You should see the following output

      • pygame 2.5.2 (SDL 2.28.3, Python 3.11.4)

      • Hello from the pygame community. https://www.pygame.org/contribute.

      • Test 1 passed: Pygame is installed and initialized successfully.

      • Test 2 passed: Pygame window created successfully.

      • Test 3 passed: OpenAI Gym and Stable-Baselines3 are installed.

      • Tests passed: 3/3

      • All tests passed! Your environment is set up correctly.

    • Common Troubleshooting Tips

    1. Make sure you've activated your virtual environment before running the script.

    2. Make sure you're in the week1 folder in your terminal.

    3. Make sure you've installed the libraries correctly, you can test this by running pip list in your terminal.

    4. Please reach out in the MSU AI Club Tron project Discord channel if you're having further trouble!

    Wrapping Up

    By the end of this week, you should have:

    1. Played Tron and understood it's basic mechanics

    2. Set up a Python development environment with a virtual environment

    3. Installed Pygame, OpenAI Gym, and Stable-Baselines3

    4. Created and run a basic Pygame window with a simple shape

    Bonus Challenge

    Try modifying the Pygame window code to:

    1. Change the window size

    2. Change the background color

    3. Draw a different shape or multiple shapes

    4. Make a shape move across the screen

    Next week, we'll start building the actual Tron game board and implementing game logic. Great job on getting everything set up!

    Comments

    9/18/2024, 2:54:08 AM

    I have done all the Bonuses but do not know why on adding multiple shapes the output screen goes black and cannot be closed.

    9/18/2024, 8:31:23 PM

    Hey there, Aidan here, send a message in the discord and I can help out!