C# Windows Form Game : Movement

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 6K views
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;
                }
            }
    }
}
 
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.
 
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.