简体   繁体   English

C#WinForms使用另一个类移动对象

[英]C# WinForms moving Object with another Class

im trying to Program my first game in c#, but currently im stuck at getting the Player moving.. 我正在尝试用C#编写我的第一款游戏,但目前我一直无法使玩家移动。

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... 当按下W时,在播放器实例中向下播放的布尔值更改为true,因此,当滴答事件发生并且触发player.move()时,它应该上升...

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;
          }           
     }
}

As far as I understand, you want to move your player regularly as long as the W or S key is pressed. 据我了解,只要按下W或S键,您就希望定期移动播放器。 (like a key repeat) (如重复输入)

But, in your code the player.up and player.down booleans are never set to true! 但是,在您的代码中, player.upplayer.down布尔值永远不会设置为true! So it seems calling move() will have no effect. 因此,调用move()似乎无效。

Your should correct this and set to true on the keyDown event : 您应该对此进行更正,并在keyDown事件上将其设置为true:

 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;

      }           
 }

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
}
  1. 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. 图片框内部有很多逻辑。

  2. 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 问题是他可以删除somethig,更改其他玩家/ mpc的某些属性,这就是您要避免的事情,因为它的确很难(几乎不可能)以使Player对象成为可能。

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. picturebox1_paint是图片框的绘制事件。 if you are using Visual studio you can create in event tab select Picturebox -> Properties -> events (thunder icon) -> double click on Paint. 如果您使用的是Visual Studio,则可以在事件选项卡中创建,选择图片框->属性->事件(雷声图标)->双击画图。

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
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM