WinFrame App C# 游戏设计:左右移动图片框
WinFrame App C# game design: Moving pictureboxes left and right
我希望我的图片框左右移动,到目前为止它们只是向右移动并在到达边缘时消失。代码中有3个敌人,他们都可以向右移动,我怎样才能让他们在击中"the wall"时向左移动?玩家可以双向移动。
{
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;
}
使用你的代码我做了这个,首先制作 3 个全局布尔值:bool LargeGoLeft = true, MediumGoLeft = true, SmallGoLeft = true;
然后像这样将代码放入您的计时器中:
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;
}
}
这是我能够测试的版本,它与图片框完美配合
我希望我的图片框左右移动,到目前为止它们只是向右移动并在到达边缘时消失。代码中有3个敌人,他们都可以向右移动,我怎样才能让他们在击中"the wall"时向左移动?玩家可以双向移动。
{
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;
}
使用你的代码我做了这个,首先制作 3 个全局布尔值:bool LargeGoLeft = true, MediumGoLeft = true, SmallGoLeft = true;
然后像这样将代码放入您的计时器中:
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;
}
}
这是我能够测试的版本,它与图片框完美配合