By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,025 Members | 2,118 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Python based artificial intelligence for a flash game

kudos
Expert 100+
P: 126
"dogfight 2" is a flash based biplane game at rock solid arcade.

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
  1. import subprocess
  2. import time
  3.  
  4. # get focus and click
  5. subprocess.call(["xdotool","mousemove","440","661"]) # use subprocess to call xdotool from python
  6. subprocess.call(["xdotool","click","1"])
  7. time.sleep(3)
  8.  
  9. # press fire (space) to start the program
  10. subprocess.call(["xdotool","keydown","space"])
  11. time.sleep(1)
  12. subprocess.call(["xdotool","keyup","space"]) 
  13.  
  14.  
  15. # key pushing the up key to make the plane roll
  16. while(True):
  17.  # each second press the up cursor key
  18.  subprocess.call(["xdotool","keydown","Up"])
  19.  time.sleep(1)
  20.  subprocess.call(["xdotool","keyup","Up"])
  21.  
  22. # runs until you press Ctrl-Z
  23.  
That's it! It doesn't do anything more than looping for maybe two times, before it crashes into the ground. It could be extended, for instance we could figure out the position of the plane, make it fire all the time, etc, etc.
Attached Images
File Type: jpg point1b.jpg (43.4 KB, 1220 views)
May 19 '13 #1
Share this Article
Share on Google+