- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Today we are going to make a simple game using Pygame for the purpose of Q-learning an AI to play it.
We will be making the 1973 Atari game "Space Race" which is a cross between asteroids and froggers.
The link to the code can be found here.
Overview
The gameplay is simple: avoid the asteroids and get to the top of the screen as fast as you can for points! The player who gets the most points by the end of the time limit wins. Player 1 uses WASD and Player 2 uses Arrow keys.
Getting Started
We are going to start by creating a star class and 2 ships.
Main Function Loop
Now, let's go into the main function loop.
The overarching function is this: while the game is running, check for inputs and special events, calculate updated positions, and display the new positions.
Not too hard, but it can be a bit difficult if you're not used to pygame. We start by checking if the game is over and breaking if it is:
if time.time() - game_start_time > GAME_TIME:
running = False
print("Game Over!")
print("Player 1 Score: " + str(score1))
print("Player 2 Score: " + str(score2))
if score1 > score2:
print("Player 1 Wins!")
if score2 > score1:
print("Player 2 Wins!")
if score1 == score2:
print("Tie Game!")
Okay, that's pretty easy. Let's go ahead and try the loop.
We first have to look at the updated positions for both the ships and the stars. For the ships, we need to find which keys are pressed down.
Luckily, for stars, we already made our class for it so all we have to do is call star.move()
Acceleration, stars, and special events.
I decided to include acceleration for the ship because it made it feel more natural. In space, there's no friction either, but... it's no fun if you keep going forward after you stop pressing the gas... I added friction as well.
- Get link
- X
- Other Apps
Comments
Post a Comment