using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Threading; namespace PS9_Q1 { // // Problem Set 9 // Written by Paul Hudak // November 28, 2006 // public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; // instance variables private int x, y; // position of ball private int dx=3, dy=4; // controls velocity of ball private const int initdx=3, initdy=4; // initial velocity of ball private int score = 0; // player's score // Suspend, Resume, Abort, and Reset buttons private Button suspend = new Button(); private Button resume = new Button(); private Button abort = new Button(); private Button reset = new Button(); // Display for score private Label display = new Label(); private Pen black = new Pen(Color.Black, 2); // Random number generator and animation thread private Random r = new Random(); Thread t; public Form1() { InitializeComponent(); // initialize the Form // BackColor = Color.White; // I don't like the white background Size = new Size(400,600); Text = "My Very Own Video Game"; SetStyle(ControlStyles.DoubleBuffer | // use double-buffering ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); // initialize the button and display text abort.Text = "Abort"; suspend.Text = "Suspend"; resume.Text = "Resume"; reset.Text = "Reset"; display.TextAlign = ContentAlignment.MiddleCenter; display.BorderStyle = BorderStyle.Fixed3D; display.Size = new Size(100,75); display.Font = new Font("Comic Sans MS", 20, FontStyle.Bold); display.Text = score.ToString(); // set component locations suspend.Location = new Point(35,450); resume.Location = new Point(35,500); abort.Location = new Point(280,450); reset.Location = new Point(280,500); display.Location = new Point(145,450); // register event handlers abort.Click += new EventHandler(Abort_Click); suspend.Click += new EventHandler(Suspend_Click); resume.Click += new EventHandler(Resume_Click); reset.Click += new EventHandler(Reset_Click); // add controls to Form Controls.Add(suspend); Controls.Add(resume); Controls.Add(abort); Controls.Add(reset); Controls.Add(display); // initialize instance variables x = 100; y = 100; // initial position of ball t = new Thread(new ThreadStart(Run)); // create thread t.Start(); // enable thread to run } // three of the button event handlers are the same except // for the operation to be performed protected void Abort_Click(object sender, EventArgs e) { try { t.Abort(); } catch { } } protected void Suspend_Click(object sender, EventArgs e) { try { t.Suspend(); } catch { } } protected void Resume_Click(object sender, EventArgs e) { try { t.Resume(); } catch { } } // reset event hanlder is more complex protected void Reset_Click(object sender, EventArgs e) { // reset score and "harder" count score = 0; display.Text = score.ToString(); // randomly create new position and direction of motion x = r.Next(350); y = r.Next(350); dx = (r.Next(2)==1) ? initdx : -initdx; dy = (r.Next(2)==1) ? initdy : -initdy; } // mouse clicks update the score if they are a "hit" protected override void OnMouseDown(MouseEventArgs e) { int delx = e.X - x - 20; int dely = e.Y - y - 20; if (delx*delx + dely*dely <= 400) // 400 = ball-radius squared { // if a "hit", then update score score = score+1; display.Text = score.ToString(); // every five hits, increase ball speed if (score%5 == 0) { dx = (dx<0) ? dx-1 : dx+1; dy = (dy<0) ? dy-1 : dy+1; } } } protected override void OnPaint(PaintEventArgs e) { // draw line and ball Graphics g = e.Graphics; g.FillEllipse(Brushes.Red, x, y, 40, 40); g.DrawLine(black, 0, 400, 400, 400); base.OnPaint(e); } public void Run() { // infinite loop to update instance variables that // keep track of ball location while (true) { x += dx; y += dy; if (x<0 || x>350) dx = -dx; // hit a vertical wall if (y<0 || y>360) dy = -dy; // hit a horizontal wall Invalidate(); Thread.Sleep(40); } } // protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } t.Abort(); // kill running thread when app terminates base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } #endregion // [STAThread] static void Main() { Application.Run(new Form1()); } } }