http://www.rocksolidarcade.com/games/dogfight2/
Wouldn't it be cool to make a python program, an Artificial intelligence (AI), that could play the dogfight game for us?
Aware that making an efficient AI for playing such a game is a very difficult task, I settle for creating a simple framework that starts the game and looping the plane until it crashes into the ground.
First, some words about the environment. I have used a Ubuntu vmware image, with google chrome installed.
The first thing I do start the chrome browser with the address :
http://www.rocksolidarcade.com/games/dogfight2/
We want to make a program that clicks on the dogfight game with the left mouse button to gain focus, then wait for a second or so. To figure out what the coordinates for the dogfight game is, we use the xdotool from a terminal window (See image below)
You might have to install xdotool, as it might not come with your linux distribution by default, to do this, type:
sudo apt-get install xdotool
Once it is installed, move your mouse over the dogfight game. Don't click! In fact don't move the mouse at all!
Type the following on your keyboard in the terminal window which still should have focus.
xdotool getmouselocation
Dogfight tell us to start the game by pressing "fire". On your computer, "fire" would be "space", but you will not be pressing fire yourself, the python progam should do that for you.
Conceptually the program would be simple: First your program would click on the Dogfight game to gain focus, then it would press space to start the game, then it would wait for a second before it would press the up key to lead the plane into a loop. The last part would be an infinite loop
Below is an example python program that does just that
Expand|Select|Wrap|Line Numbers
- import subprocess
- import time
- # get focus and click
- subprocess.call(["xdotool","mousemove","440","661"]) # use subprocess to call xdotool from python
- subprocess.call(["xdotool","click","1"])
- time.sleep(3)
- # press fire (space) to start the program
- subprocess.call(["xdotool","keydown","space"])
- time.sleep(1)
- subprocess.call(["xdotool","keyup","space"])
- # key pushing the up key to make the plane roll
- while(True):
- # each second press the up cursor key
- subprocess.call(["xdotool","keydown","Up"])
- time.sleep(1)
- subprocess.call(["xdotool","keyup","Up"])
- # runs until you press Ctrl-Z