Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C# Windows Form Game : Movement

  1. Mar 16, 2014 #1

    TheDemx27

    User Avatar
    Gold Member

    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 (Text):
    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;
                    }
                }
        }
    }
     
     
  2. jcsd
  3. Mar 16, 2014 #2

    Mark44

    Staff: Mentor

    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.
     
  4. Mar 16, 2014 #3
    You need to start the timer. You can do it like:

    Code (Text):
    private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Start();
            }
     
  5. Mar 16, 2014 #4

    DavidSnider

    User Avatar
    Gold Member

    I think that code is here:

    Code (Text):

            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.
     
  6. Mar 17, 2014 #5

    Mark44

    Staff: Mentor

    DavidSnider, I saw that code, but not knowing what type a player object is, I couldn't tell that the code did anything.
     
  7. Mar 18, 2014 #6

    TheDemx27

    User Avatar
    Gold Member

    Sorry: "player" is just a picture box in form1.
     
  8. Mar 19, 2014 #7

    TheDemx27

    User Avatar
    Gold Member

    Thanks!
     
  9. Mar 27, 2014 #8

    harborsparrow

    User Avatar
    Gold Member

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C# Windows Form Game : Movement
  1. C# Window Resizing (Replies: 5)

Loading...