C# C# Windows Form Game : Movement

Click For Summary
The discussion revolves around creating a simple block movement in a C# Windows Forms application using Visual Studio 2010. The code provided sets up key event handlers for moving a player object (a PictureBox) left and right. However, the application does not respond to input because the timer responsible for updating the player's position is not started. To resolve this, the timer should be initiated in the Form1_Load method with `timer1.Start()`. Additionally, it's noted that for proper animation, the form should be invalidated to trigger a redraw, and overriding the form's OnPaint method is recommended for better control over rendering. The importance of ensuring the player object is visible and the timer is active is emphasized for the movement to function correctly.
TheDemx27
Gold Member
Messages
169
Reaction score
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
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.
 
You need to start the timer. You can do it like:

Code:
private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
 
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.
 
DavidSnider, I saw that code, but not knowing what type a player object is, I couldn't tell that the code did anything.
 
Sorry: "player" is just a picture box in form1.
 
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!
 
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.
 

Similar threads

Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
6K
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K