[英]WinFrame App C# game design: Moving pictureboxes left and right
I would like my pictureboxes to move left and right, so far they're only moving right and disappearing when they reach the edge. 我希望我的图片框向左和向右移动,到目前为止,它们仅向右移动并在到达边缘时消失。 In the code there are 3 enemies, they can all move to the right, how can I make these move left when they hit "the wall"?
在代码中,有3个敌人,它们都可以向右移动,当他们碰到“墙”时,如何使它们向左移动? The Player can move both directions.
播放器可以双向移动。
{
Random _random;
public MainWindow()
{
InitializeComponent();
_random = new Random();
}
private void MainWindow_Load(object sender, EventArgs e)
{
Size s = new System.Drawing.Size(800, 600);
this.ClientSize = s;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
}
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
{
if (e.KeyCode == Keys.Left)
{
Player.Left -= 20;
}
if (e.KeyCode == Keys.Right)
{
Player.Left += 20;
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int z = _random.Next(0, 10);
int x = _random.Next(0, 20);
int y = _random.Next(0, 30);
LargeEnemy.Left += z;
MediumEnemy.Left += x;
SmallEnemy.Left += y;
}
Using your code I made this, first make 3 global bools: bool LargeGoLeft = true, MediumGoLeft = true, SmallGoLeft = true;
使用您的代码,首先创建3个全局
bool LargeGoLeft = true, MediumGoLeft = true, SmallGoLeft = true;
: bool LargeGoLeft = true, MediumGoLeft = true, SmallGoLeft = true;
Then put the code in your timer like so: 然后将代码放在您的计时器中,如下所示:
bool moveLeft = false;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if((pictureBox1.Left + pictureBox1.Width) >= this.Width)
{
moveLeft = true;
}
if(pictureBox1.Left < 0)
{
moveLeft = false;
}
if(moveLeft)
{
pictureBox1.Left -= 15;
}
if (!moveLeft)
{
pictureBox1.Left += 15;
}
}
This is the version I was able test and it works perfectly with a picturebox 这是我能够测试的版本,与Picturebox完美搭配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.