C# Windows Form Game : Movement

Click For Summary

Discussion Overview

The discussion focuses on implementing movement for a block in a C# Windows Forms application. Participants explore the mechanics of handling key events and using a timer to animate the movement of a player object, which is represented as a picture box. The scope includes coding practices and troubleshooting within the context of game development.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the code sets the right or left variable based on key events but does not include code that directly causes movement.
  • Another participant suggests starting the timer in the Form1_Load method to enable the movement functionality.
  • A participant clarifies that the "player" is a picture box, which may affect how movement is perceived in the code.
  • One participant references a resource that discusses basic animation techniques, emphasizing the need to invalidate the form for redrawing rather than moving objects directly in the timer routine.

Areas of Agreement / Disagreement

Participants generally agree on the need to start the timer for movement to occur, but there is some uncertainty about the implementation details and the effectiveness of the current code structure. Multiple views on best practices for animation and movement handling are present.

Contextual Notes

There are unresolved questions regarding the visibility of the player object and the specific implementation of the OnPaint method for redrawing, which may affect the overall functionality.

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
2K
  • · 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