468,310 Members | 1,380 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Python based artificial intelligence for a flash game

127 Expert 100+
"dogfight 2" is a flash based biplane game at rock solid arcade.


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 :


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
  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)
  9. # press fire (space) to start the program
  10. subprocess.call(["xdotool","keydown","space"])
  11. time.sleep(1)
  12. subprocess.call(["xdotool","keyup","space"]) 
  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"])
  22. # runs until you press Ctrl-Z
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, 1411 views)
May 19 '13 #1
0 3888

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Arthur T. Murray | last post: by
reply views Thread by indhu | last post: by
2 posts views Thread by rajeshkumarvm | last post: by
1 post views Thread by kadavakooti | last post: by
7 posts views Thread by sarika | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.