C# Windows Form Game : Movement

  • C/++/#
  • Thread starter TheDemx27
  • Start date
  • #1
TheDemx27
Gold Member
170
13
C# Windows Form "Game": Movement

All I wanted to do was to make a block move right/left across the window. I'm using VS 2010. No compiler errors and when I run it, it doesn't respond to any input.

Code:
using System;
using System.Windows.Forms;

namespace Game
{
    public partial class Form1 : Form
    {
        bool right;
        bool left;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (right == true) 
            { 
                player.Left += 5; 
            }
            if (left == true) 
            { 
                player.Left -= 5;
            }
        }

       
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
              if (e.KeyCode == Keys.Right) 
                { 
                    right = true; 
                }
                if (e.KeyCode == Keys.Left) 
                { 
                    left = true; 
                }
                if (e.KeyCode == Keys.Left && e.KeyCode == Keys.Right) 
                {
                    right = false;
                    left = false;
                }
            }

          
        private void Form1_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Right) 
                { 
                    right = false; 
                }
                
                if (e.KeyCode == Keys.Left)  
                { 
                    left = false;
                }
            }
    }
}
 

Answers and Replies

  • #2
34,158
5,777
All I wanted to do was to make a block move right/left across the window. I'm using VS 2010. No compiler errors and when I run it, it doesn't respond to any input.

Code:
using System;
using System.Windows.Forms;

namespace Game
{
    public partial class Form1 : Form
    {
        bool right;
        bool left;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (right == true) 
            { 
                player.Left += 5; 
            }
            if (left == true) 
            { 
                player.Left -= 5;
            }
        }

       
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
              if (e.KeyCode == Keys.Right) 
                { 
                    right = true; 
                }
                if (e.KeyCode == Keys.Left) 
                { 
                    left = true; 
                }
                if (e.KeyCode == Keys.Left && e.KeyCode == Keys.Right) 
                {
                    right = false;
                    left = false;
                }
            }

          
        private void Form1_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Right) 
                { 
                    right = false; 
                }
                
                if (e.KeyCode == Keys.Left)  
                { 
                    left = false;
                }
            }
    }
}
As far as I can tell from the code that you showed, when the KeyUp or KeyDown events are raised, all that happens is that you're setting the right or left variable, depending on what key has been set. I don't seen any code that actually causes something to move.
 
  • #3
259
786
You need to start the timer. You can do it like:

Code:
private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
 
  • #4
DavidSnider
Gold Member
488
131
As far as I can tell from the code that you showed, when the KeyUp or KeyDown events are raised, all that happens is that you're setting the right or left variable, depending on what key has been set. I don't seen any code that actually causes something to move.
I think that code is here:

Code:
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (right == true) 
            { 
                player.Left += 5; 
            }
            if (left == true) 
            { 
                player.Left -= 5;
            }
        }
It should work assuming that the player is visible and the timer is on.
 
  • #5
34,158
5,777
DavidSnider, I saw that code, but not knowing what type a player object is, I couldn't tell that the code did anything.
 
  • #6
TheDemx27
Gold Member
170
13
Sorry: "player" is just a picture box in form1.
 
  • #7
TheDemx27
Gold Member
170
13
You need to start the timer. You can do it like:

Code:
private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
Thanks!
 
  • #8
harborsparrow
Gold Member
555
120
See this assignment: http://harbormist.com/cit597_11/asgn/02_bubblebox.html It's about the simplest code for animation that one can write. Note that you don't actually draw (or move) things in the timer routine itself--you only invalidate the form so that it can redraw itself--and then, to tell it how to redraw, you need to do things by overriding the form's OnPaint method.

At least, that is the way that Microsoft recommends you do it.
 

Related Threads on C# Windows Form Game : Movement

  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
3
Views
8K
  • Last Post
Replies
14
Views
3K
  • Last Post
Replies
4
Views
2K
Replies
23
Views
5K
Replies
25
Views
19K
Replies
26
Views
4K
Replies
2
Views
2K
Top