[C# Snippet] Trajectory example

Discussion in 'Programming General' started by Swan, Aug 23, 2008.

[C# Snippet] Trajectory example
  1. Unread #1 - Aug 23, 2008 at 11:17 AM
  2. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    [C# Snippet] Trajectory example

    Here is just a basic example using GDI+ which demonstrates simple trajectory.

    You will need to compile the following class in to a new project:
    Code:
        class canvas : System.Windows.Forms.Control
        {
            public canvas()
            {
                this.DoubleBuffered = true;
                this.ResizeRedraw = true;
            }
    
            public void refresh()
            {
                this.Invalidate();
                this.Update();
            }
        }
    This is just the control we will be "drawing" on to.

    After rebuilding your project, it should appear in the toolbox. Add it to your form, call it "canvas1" (default) and set it's dock property to "full" and it's backcolor property to white.

    Add a timer called timer1, set it's interval to 25ms, and leave it's enabled property on false.

    Inside your code for form1.cs, put in these globally scoped variables:
    Code:
            bool running = false;
            float xVel = 10;
            const float yVelDefault = -25.0f;
            float yVel = yVelDefault;
            float gravity = 2.0f;
            RectangleF ball;
    and inside your form's constructor (public form1() { /*...*/ }), put this:
    Code:
    ball = new RectangleF(10, this.Height - 55, 25, 25);
    Now, open up the canvas control's paint event, and add this:
    Code:
            private void canvas1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    
                if (running)
                {
                    yVel += gravity;
    
                    ball.X += xVel;
                    ball.Y += yVel;
    
    
                    if (ball.Y + ball.Height >= canvas1.Height - 20)
                    {
                        //Console.WriteLine("Floor collision.");
                        yVel = yVelDefault;
                    }
                    if (ball.X + ball.Width >= canvas1.Width - 20)
                    {
                        //Console.WriteLine("Right side collision.");
                        xVel *= -1;
                    }
                    if (ball.X < 5)
                    {
                        //Console.WriteLine("Left side collision.");
                        xVel *= -1;
                    }
                }
                e.Graphics.FillEllipse(Brushes.Black, ball);            
            }
    Open up your canvas's click event and add this:
    Code:
            private void canvas1_Click(object sender, EventArgs e)
            {
                switch (timer1.Enabled)
                {
                    case true:
                        timer1.Stop();
                        running = false;
                        break;
                    default:
                        timer1.Start();
                        running = true;
                        break;
                }
            }
    And finally, open up your timer's tick event and add this:
    Code:
            private void canvas1_Click(object sender, EventArgs e)
            {
                switch (timer1.Enabled)
                {
                    case true:
                        timer1.Stop();
                        running = false;
                        break;
                    default:
                        timer1.Start();
                        running = true;
                        break;
                }
            }
    Have fun watching that little black ball dance around your form. You might like to edit the variables "xVel", "yVelDefault" and "gravity" to change the speed, and so on.

    Form1.cs:
    Code:
    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;
    
    namespace Trajectory
    {
        public partial class Form1 : Form
        {
            bool running = false;
            float xVel = 10;
            const float yVelDefault = -25.0f;
            float yVel = yVelDefault;
            float gravity = 2.0f;
            RectangleF ball;
    
            public Form1()
            {
                InitializeComponent();
                ball = new RectangleF(10, this.Height - 55, 25, 25);
    
            }
    
            private void canvas1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    
                if (running)
                {
                    yVel += gravity;
    
                    ball.X += xVel;
                    ball.Y += yVel;
    
    
                    if (ball.Y + ball.Height >= canvas1.Height - 20)
                    {
                        //Console.WriteLine("Floor collision.");
                        yVel = yVelDefault;
                    }
                    if (ball.X + ball.Width >= canvas1.Width - 20)
                    {
                        //Console.WriteLine("Right side collision.");
                        xVel *= -1;
                    }
                    if (ball.X < 5)
                    {
                        //Console.WriteLine("Left side collision.");
                        xVel *= -1;
                    }
                }
                e.Graphics.FillEllipse(Brushes.Black, ball);            
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                canvas1.refresh();
            }
    
            private void canvas1_Click(object sender, EventArgs e)
            {
                switch (timer1.Enabled)
                {
                    case true:
                        timer1.Stop();
                        running = false;
                        break;
                    default:
                        timer1.Start();
                        running = true;
                        break;
                }
            }
        }
    }
    
     
< Need help with something | Awesome Tutorial's List >

Users viewing this thread
1 guest


 
 
Adblock breaks this site