C#: Programmatically Playing Prince of Persia.

In this post I will programmatically pass level 1 in Prince of Persia using c#.

Prince of Persia
This game came out in 1989, it is now freeware.

In order to run MS DOS Prince of Persia(In 2013 win7 era), I had to download DOSBOX.
Dos Box Website: http://www.dosbox.com/

I used this input simulator in my application:
Windows input Simulator http://inputsimulator.codeplex.com/.

Here is a fun video I uploaded to youtube:

C# Code used.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using WindowsInput;
namespace Prince
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool AllocConsole();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            AllocConsole();
        }
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName,
            string lpWindowName);
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        private void button1_Click(object sender, EventArgs e)
        {
            Process[] localAll = Process.GetProcesses();
            IntPtr calculatorHandle = FindWindow(null, "DOSBox 0.74, Cpu speed:     3000 cycles, Frameskip  0, Program:   PRINCE");
             if (calculatorHandle == IntPtr.Zero)
                 return;
             SetForegroundWindow(calculatorHandle);
             Thread.Sleep(500);
             if (SetForegroundWindow(calculatorHandle))
             {
                    commandPrince(VirtualKeyCode.NUMPAD6, 3700);
                    commandPrince(VirtualKeyCode.SHIFT, 2000);
                    commandPrince(VirtualKeyCode.NUMPAD4, 4200);
                    Thread.Sleep(500);
                    commandPrince(VirtualKeyCode.NUMPAD8, 2000);
                    Thread.Sleep(1400);
 
                    //....  This ^ is a small sample of commands I give to Prince
                    //....  Full level 1 commands are below in the post.
 
             } 
        }
        public void standingJumpForw(VirtualKeyCode key, VirtualKeyCode key2, int ms)
        {
            Console.WriteLine("Standing Forward Jump");
            InputSimulator.SimulateKeyDown(key);
            Thread.Sleep(100);
            InputSimulator.SimulateKeyDown(key2);
            Thread.Sleep(ms);
            InputSimulator.SimulateKeyUp(key2);
            InputSimulator.SimulateKeyUp(key);
            Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " up ");
        }
        public void commandClibUp(VirtualKeyCode key, VirtualKeyCode key2, int ms)
        {
            Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " down ");
            InputSimulator.SimulateKeyDown(key);
            Thread.Sleep(ms);
            InputSimulator.SimulateKeyDown(key2);
            Thread.Sleep(500);
            InputSimulator.SimulateKeyUp(key2);
            InputSimulator.SimulateKeyUp(key);
            Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " up ");
        }
        public void commandPrince(VirtualKeyCode key, int ms)
        {
            Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " down ");
            InputSimulator.SimulateKeyDown(key);
            Thread.Sleep(ms);
            InputSimulator.SimulateKeyUp(key);
            Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " up ");
        }
 
        public void runningJump(VirtualKeyCode key, VirtualKeyCode key2, int ms)
        {
            Console.WriteLine("Running Jump ");
            InputSimulator.SimulateKeyDown(key);
            Thread.Sleep(ms);
            InputSimulator.SimulateKeyDown(key2);
            Thread.Sleep(550);
            InputSimulator.SimulateKeyUp(key2);
            InputSimulator.SimulateKeyUp(key);
        }
        public void runningJumpFast(VirtualKeyCode key, VirtualKeyCode key2, int ms)
        {
            Console.WriteLine("Running Jump Fast");
            InputSimulator.SimulateKeyDown(key);
            Thread.Sleep(ms);
            InputSimulator.SimulateKeyDown(key2);
            Thread.Sleep(550);
            InputSimulator.SimulateKeyUp(key2);
            InputSimulator.SimulateKeyUp(key);
        }
    }
}

Complete Level 1 Commands:

commandPrince(VirtualKeyCode.NUMPAD6, 3700);
commandPrince(VirtualKeyCode.SHIFT, 2000);
commandPrince(VirtualKeyCode.NUMPAD4, 4200);
Thread.Sleep(500);
commandPrince(VirtualKeyCode.NUMPAD8, 2000);
Thread.Sleep(1400);
runningJump(VirtualKeyCode.NUMPAD4, VirtualKeyCode.NUMPAD8, 1000);
commandClibUp(VirtualKeyCode.SHIFT, VirtualKeyCode.NUMPAD8, 2000);
commandPrince(VirtualKeyCode.NUMPAD4, 4200);
commandPrince(VirtualKeyCode.NUMPAD6, 2500);
Thread.Sleep(500);
runningJump(VirtualKeyCode.NUMPAD4, VirtualKeyCode.NUMPAD8, 1000);
Thread.Sleep(1200);
commandPrince(VirtualKeyCode.NUMPAD8, 2000);
commandPrince(VirtualKeyCode.NUMPAD4, 7000);
runningJump(VirtualKeyCode.NUMPAD4, VirtualKeyCode.NUMPAD8, 500);
commandPrince(VirtualKeyCode.NUMPAD4, 1000);
Thread.Sleep(1200);
commandPrince(VirtualKeyCode.NUMPAD8, 2000);
commandPrince(VirtualKeyCode.NUMPAD4, 500);
runningJump(VirtualKeyCode.NUMPAD4, VirtualKeyCode.NUMPAD8, 1000);
Thread.Sleep(500);
commandPrince(VirtualKeyCode.NUMPAD4, 1500);
runningJump(VirtualKeyCode.NUMPAD4, VirtualKeyCode.NUMPAD8, 1000);
commandPrince(VirtualKeyCode.SHIFT, 2000);
Thread.Sleep(500);
commandPrince(VirtualKeyCode.NUMPAD6, 1100);
Thread.Sleep(3000);
commandPrince(VirtualKeyCode.NUMPAD6, 1100);
commandPrince(VirtualKeyCode.NUMPAD8, 2100);
commandPrince(VirtualKeyCode.NUMPAD6, 5100);
Thread.Sleep(500);
commandPrince(VirtualKeyCode.NUMPAD8, 2100);
Thread.Sleep(1100);
commandPrince(VirtualKeyCode.NUMPAD8, 2100);
Thread.Sleep(1100);
standingJumpForw(VirtualKeyCode.NUMPAD6, VirtualKeyCode.NUMPAD8, 2000);
commandPrince(VirtualKeyCode.SHIFT, 2000);
commandPrince(VirtualKeyCode.NUMPAD6, 2000);
Thread.Sleep(1100);
runningJump(VirtualKeyCode.NUMPAD6, VirtualKeyCode.NUMPAD8, 1000);
commandPrince(VirtualKeyCode.NUMPAD6, 1200);
Thread.Sleep(1100);
commandPrince(VirtualKeyCode.NUMPAD8, 2100);
Thread.Sleep(500);
runningJumpFast(VirtualKeyCode.NUMPAD6, VirtualKeyCode.NUMPAD8, 600);
commandPrince(VirtualKeyCode.NUMPAD6, 2000);
Thread.Sleep(1000);
commandPrince(VirtualKeyCode.NUMPAD6, 500);
Thread.Sleep(1000);
standingJumpForw(VirtualKeyCode.NUMPAD6, VirtualKeyCode.NUMPAD8, 1500);
Thread.Sleep(1000);
commandPrince(VirtualKeyCode.NUMPAD8, 2000);
Thread.Sleep(1000);
commandPrince(VirtualKeyCode.NUMPAD6, 1000);
runningJumpFast(VirtualKeyCode.NUMPAD6, VirtualKeyCode.NUMPAD8, 600);
commandPrince(VirtualKeyCode.NUMPAD6, 8200);
 
//BATTLE!!!  Here is where you would start implementing A.I.
Thread.Sleep(1500);
commandPrince(VirtualKeyCode.SHIFT, 500);
Thread.Sleep(500);
commandPrince(VirtualKeyCode.SHIFT, 500);
Thread.Sleep(500);
commandPrince(VirtualKeyCode.SHIFT, 500);
 
commandPrince(VirtualKeyCode.SHIFT, 500);
Thread.Sleep(500);
commandPrince(VirtualKeyCode.SHIFT, 500);
Thread.Sleep(1000);
commandPrince(VirtualKeyCode.SHIFT, 500);
commandPrince(VirtualKeyCode.SHIFT, 500);
commandPrince(VirtualKeyCode.SHIFT, 500);
Thread.Sleep(500);
commandPrince(VirtualKeyCode.SHIFT, 500);
Thread.Sleep(1000);
commandPrince(VirtualKeyCode.SHIFT, 500);
commandPrince(VirtualKeyCode.SHIFT, 500);
Thread.Sleep(500);
commandPrince(VirtualKeyCode.SHIFT, 500);
Thread.Sleep(1000);
commandPrince(VirtualKeyCode.SHIFT, 500);
commandPrince(VirtualKeyCode.SHIFT, 500);
Thread.Sleep(1000);
commandPrince(VirtualKeyCode.SHIFT, 500);
commandPrince(VirtualKeyCode.SHIFT, 500);
commandPrince(VirtualKeyCode.NUMPAD6, 10200);
Thread.Sleep(1000);
commandPrince(VirtualKeyCode.NUMPAD4, 3000);
Thread.Sleep(1000);
commandPrince(VirtualKeyCode.NUMPAD8, 2000);
Thread.Sleep(1000);
commandPrince(VirtualKeyCode.NUMPAD6, 2900);
Thread.Sleep(4000);
commandPrince(VirtualKeyCode.NUMPAD8, 1000);

Leave a Reply

Your email address will not be published. Required fields are marked *