Monday 14 July 2014

Hi Guys!!
    In this post you will learn how to control a sprite's movement in C# with Arrow Keys
          Below  is the  Code for arrow controlled object motion. Its easier than it sounds check it out guys.

1:  using System;  
2:  using System.Collections.Generic;  
3:  using System.ComponentModel;  
4:  using System.Data;  
5:  using System.Drawing;  
6:  using System.Linq;  
7:  using System.Text;  
8:  using System.Windows.Forms;  
9:  namespace KeyControlledMotion  
10:  {  
11:    public partial class Form1 : Form  
12:    {  
13:      static int xpos , ypos , circleWidth , circleHeight;  
14:      static int panelWidth,panelHeight;  
15:      static SolidBrush bgBrush , circleBrush;  
16:      static Rectangle backgroundRect;  
17:      public Form1()  
18:      {  
19:        xpos = ypos = 0;  
20:        circleWidth = circleHeight = 50;  
21:        panelWidth = 315;  
22:        panelHeight = 295;  
23:        backgroundRect = new Rectangle(0, 0, panelWidth, panelHeight);  
24:        circleBrush = new SolidBrush(Color.SteelBlue);  
25:        bgBrush = new SolidBrush(Color.Black);  
26:        InitializeComponent();  
27:      }  
28:      private void Form1_Load(object sender, EventArgs e){ }  
29:      private void Form1_MouseHover(object sender, EventArgs e){}  
30:      private void panel1_MouseHover(object sender, EventArgs e)  
31:      {  
32:        Cursor.Hide();  
33:      }  
34:      private void panel1_Paint(object sender, PaintEventArgs e)  
35:      {  
36:        Graphics g = panel1.CreateGraphics();  
37:        g.FillRectangle(bgBrush, backgroundRect);  
38:        g.FillEllipse(circleBrush,xpos,ypos,circleWidth,circleHeight);  
39:      }  
40:      void reDrawBG(Graphics gr) {  
41:        gr.FillRectangle(bgBrush,backgroundRect);  
42:      }  
43:      public void move(SolidBrush sb,int x,int y) {  
44:        Graphics gr = panel1.CreateGraphics();  
45:        reDrawBG(gr);  
46:        gr.FillEllipse(sb,x,y,50,50);  
47:      }  
48:      private void Form1_KeyDown(object sender, KeyEventArgs e)  
49:      {  
50:        if (e.KeyCode == Keys.Left)  
51:        {  
52:          if(xpos-2 >= 0)  
53:          xpos -= 2;  
54:          move(circleBrush,xpos,ypos);  
55:        }  
56:        else if (e.KeyCode == Keys.Right)  
57:        {  
58:          if(xpos < panel1.Width-circleWidth)  
59:          xpos += 2;  
60:          move(circleBrush, xpos, ypos);  
61:        }  
62:        else if (e.KeyCode == Keys.Up)  
63:        {  
64:          if(ypos >= 0)  
65:          ypos -= 2;  
66:          move(circleBrush, xpos, ypos);  
67:        }  
68:        else if (e.KeyCode == Keys.Down)  
69:        {  
70:          if(ypos < panel1.Height-circleHeight)  
71:          ypos += 2;  
72:          move(circleBrush, xpos, ypos);  
73:        } 
74:      }//keyDown event  
75:    }//class  
76:  }//namespace

 Explanation : 

                      In keyDown Event i have used if else  conditions to check which key is pressed down after  certain statements are executed  if a condition is true. For Example if the down key is pressed down the circle will move 2 pixels down and same  is the case with left , right and up keys.

                      The method reDrawBG is just used to redraw the background  after every keyDown(left,down,right,up).

                      The method move is used  to move or update the circle's position after each keyDown.
All the global variables are initialized  inside the constructor.


Output:



Download the Complete Project Arrow controlled object
Subscribe to RSS Feed Follow me on Twitter!