Reputation: 33
im trying to Program my first game in c#, but currently im stuck at getting the Player moving..
I see no Logical issue in this...
When W is pressed Down the bool for down in the instance of player changes to true so when the tickevent happens and the player.move() is triggered it should go up...
I hope u can help me here!
I have the class Player:
class Player : PictureBox
{
private int _playerspeed = 5;
public bool up = false;
public bool down = false;
public bool left = false;
public bool right = false;
public bool pshoot = false;
Game _game;
public Player(Game game)
{
_game = game;
}
public void spawn()
{
this.BackColor = Color.DarkGreen;
this.Size = new Size(20, 20);
this.Left = 540;
this.Top = 580;
this.Tag = "player";
this.Visible = true;
_game.Controls.Add(this);
this.BringToFront();
}
public void move()
{
if (up)
{
this.Top -= _playerspeed;
}
if (down)
{
this.Top += _playerspeed;
}
}
}
And a Class Game which is the Form:
public partial class Game : Form
{
Player player;
bool playerUp = false;
bool playerDown = false;
bool playerLeft = false;
bool playerRight = false;
bool playerShoot = false;
public Game()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
player = new Player(this);
btnStart.Visible = false;
player.spawn();
tmrGameloop.Start();
}
private void tmrGameloop_Tick(object sender, EventArgs e)
{
player.move();
txtTick.Text = Convert.ToString(Convert.ToDecimal(txtTick.Text)+ 1);
}
private void Game_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case (Keys.W):
player.up = true;
break;
case (Keys.S):
player.down = true;
break;
}
}
private void Game_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case (Keys.W):
player.up = false;
break;
case (Keys.S):
player.down = false;
break;
}
}
}
Upvotes: 0
Views: 816
Reputation: 975
As far as I understand, you want to move your player regularly as long as the W or S key is pressed. (like a key repeat)
But, in your code the player.up
and player.down
booleans are never set to true! So it seems calling move()
will have no effect.
Your should correct this and set to true on the keyDown event :
private void Game_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case (Keys.W):
player.up = true;
break;
case (Keys.S):
player.down = true;
break;
}
}
Upvotes: 1
Reputation: 885
I see several problems. First of all for players direction use enum it will save you a lot of work.
like:
public enum Direction{
up,
down,
left,
right,
notMoving
}
You are using picture box in wrong way. Why you dont have picturebox inside form you can draw the player to the picture box. picture box have a lot of logic inside.
The player shodn't have reference to the Form (Game). look he can add him self to the controls. The problem is he can remove somethig, change some properties of other players/ mpc's and thats something you want to avoid, becose its realy hard(nearly inposible) to prihibit the the Player object that
my solution:
class Game{
private Player player;
// constructor etc.
public void picturebox1_paint(object sender, PaintEventArgs e){
Graphics g = e.Graphics;
player.DrawYourSelf(g);
}
}
picturebox1_paint is paint event of the picturebox. if you are using Visual studio you can create in event tab select Picturebox -> Properties -> events (thunder icon) -> double click on Paint.
class Player{
private Direction direction = Direction.notMoving;
private Point position;
// contructor etc.
public void DrawYourSelf(Graphics g){
// here use g to draw to player to picture box
}
}
Upvotes: 0