/* goblins template - C# version converted from Java 10/2003 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class goblinapp : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; public static int XBOUND = 800; // width of window public static int YBOUND = 600; // height of window public static int STEP = 4; // default movement speed public static int FDELAY = 30; // animation frame delay private Bitmap dbuffer; // double buffer private Graphics displaydc; // for screen private Graphics bufferdc; // for double buffer public SolidBrush brush = new SolidBrush( Color.Green ); public Pen pen = new Pen( Color.Green ); public Random randnum = new Random(); // random num generator public static bool stop = false; // controls game private human professor; // constructor public goblinapp() { InitializeComponent(); } private void InitializeComponent() { components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(XBOUND,YBOUND); this.Name = "goblinapp"; this.Text = "Professor versus the Goblins"; this.BackColor = Color.Black; this.Load += new System.EventHandler(init); this.KeyDown +=new KeyEventHandler(handlekeys); } [STAThread] static void Main() { goblinapp theapp = new goblinapp(); Application.Run(theapp); } // Event handlers protected override void OnPaintBackground(PaintEventArgs pe) {} // don't autopaint to control animation protected override void OnPaint(PaintEventArgs paintEvent ) { mainloop(); } private void handlekeys(object sender, KeyEventArgs ke) { ke.Handled = true; switch(ke.KeyCode) { case Keys.Left: professor.setdirection(-1*STEP,0); break; case Keys.Right: professor.setdirection(STEP,0); break; case Keys.Up: professor.setdirection(0,-1*STEP); break; case Keys.Down: professor.setdirection(0,STEP); break; case Keys.Space: professor.setdirection(0,0); break; default: break; } // switch } /* utility function you might need: (distance between two coordinates) */ public static double dist(int x1, int y1, int x2, int y2) { int dx = (x1-x2) * (x1-x2); int dy = (y1-y2) * (y1-y2); return Math.Sqrt(dx+dy); } /* ------------------------------------------------------------- */ public void init(object o, System.EventArgs e) { // get display contexts displaydc = this.CreateGraphics(); dbuffer = new Bitmap(XBOUND,YBOUND); bufferdc = Graphics.FromImage(dbuffer); bufferdc.FillRectangle(brush,0,0,XBOUND,YBOUND); // clear bkground professor = new human(XBOUND-50,YBOUND-50,bufferdc,"man15.gif",14); } // end method init private void mainloop() { if (!stop) { professor.move(); // don't touch the following, and make sure all your "moves" are above displaydc.DrawImage(dbuffer,0,0); // writes buffer to screen System.Threading.Thread.Sleep(FDELAY); // delay between frames (ms) bufferdc.FillRectangle(brush,0,0,XBOUND,YBOUND); // clear bkground Invalidate(); // recursive call to OnPaint } // !stop /* display a message when done! displaydc.DrawString("The Professor Wins!", new Font("Serif",28), new SolidBrush(Color.Red), (XBOUND/2)-135,(YBOUND/2)-30); */ } // mainloop } // end class goblinapp /* class for players */ public class human { private Image img; // animated gif private int xcord, ycord; // coordinates of center private int dx, dy; // movement vector int radius; private Graphics dc; private int XBOUND = goblinapp.XBOUND; private int YBOUND = goblinapp.YBOUND; // For animation control of gifs // ... this took me a LONG time to figure out! private System.Drawing.Imaging.FrameDimension fdim; private int framecount; private int frame; // current frame // C# "properties": when you say object.X, you are calling the "get" code, // and when you say object.X = 2, you are calling the "set code". public int X { get { return xcord; } } public int Y { get { return ycord; } } public int R { get { return radius; } } public int DX { get { return dx; } set { dx = value; } // value is a keyword in C# } public int DY { get { return dy; } set { dy = value; } // value is whatever you're trying to assign to DY } public human(int x0, int y0,Graphics b0,string gif,int r0) // constructor { xcord = x0; ycord = y0; img = Image.FromFile(gif); // load animated gif file radius = r0; // may need to fine-tune dc = b0; // divice context in Windows GDI+ allows drawing dx = dy = 0; // initial movement vector // set info for number of animation dimension and frames in gif: Guid[] fdlist = img.FrameDimensionsList; fdim = new System.Drawing.Imaging.FrameDimension(fdlist[0]); framecount = img.GetFrameCount(fdim); // assume only 1 dimension frame = 0; } // constructor public void draw() { img.SelectActiveFrame(fdim,frame); // select animation frame frame = (frame + 1) % framecount; // advance to next frame dc.DrawImage(img,xcord-radius,ycord-radius); //dc.DrawEllipse(new Pen(Color.Red),xcord-radius,ycord-radius,2*radius,2*radius); // uncomment to see circle } public void setdirection(int newdx, int newdy) { dx = newdx; dy = newdy; } public void move() // will wrap around edges of window { xcord = xcord + dx; if (xcord < radius/2) xcord = XBOUND-(radius/2); if (xcord > XBOUND-(radius/2)) xcord = radius/2; ycord = ycord + dy; if (ycord < radius/2) ycord = YBOUND-(radius/2); if (ycord > YBOUND-(radius/2)) ycord = radius/2; draw(); } } // end class human