C# Windows Form Game : Movement

In summary: I would recommend you try it this way first, and if you can get something simple like a bubble box moving back and forth, then you can try to adapt that code to your project.In summary, the conversation was about creating a game using C# Windows Form and implementing movement for a block. The code shown included a timer and KeyDown/KeyUp events, but it was missing the code to actually make something move. The solution was to start the timer and override the form's OnPaint method to redraw the form and handle movement.
  • #1
TheDemx27
Gold Member
169
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;
                }
            }
    }
}
 
Technology news on Phys.org
  • #2
TheDemx27 said:
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
You need to start the timer. You can do it like:

Code:
private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
 
  • #4
Mark44 said:
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
DavidSnider, I saw that code, but not knowing what type a player object is, I couldn't tell that the code did anything.
 
  • #6
Sorry: "player" is just a picture box in form1.
 
  • #7
Psinter said:
You need to start the timer. You can do it like:

Code:
private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

Thanks!
 
  • #8
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 to C# Windows Form Game : Movement

1. How do I enable movement in my C# Windows Form game?

To enable movement in a C# Windows Form game, you will need to use the Keydown event to detect when a key is pressed. Then, you can use code to update the position of your game objects based on the key that was pressed.

2. Can I use the arrow keys for movement in my C# Windows Form game?

Yes, you can use the arrow keys for movement in your C# Windows Form game. You will need to use the Keydown event and check for the Keys.Up, Keys.Down, Keys.Left, and Keys.Right keys to determine which direction to move your game object.

3. How can I make my game character move at a consistent speed?

To make your game character move at a consistent speed, you can use a Timer control to update the position of your game object at a set interval. This will ensure that the movement is smooth and consistent.

4. Is it possible to have multiple game objects moving at the same time in a C# Windows Form game?

Yes, it is possible to have multiple game objects moving at the same time in a C# Windows Form game. You will need to use separate Timer controls for each game object and update their positions accordingly.

5. How can I limit the movement of my game object within the game window?

To limit the movement of your game object within the game window, you can use conditional statements to check the position of the game object and prevent it from moving beyond a certain point. You can also use the Size property of the game window to determine the boundaries for movement.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
1
Views
5K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
17
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Programming and Computer Science
Replies
11
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
Back
Top