[英]WinFrame App C# game design: Moving pictureboxes left and right
我希望我的圖片框向左和向右移動,到目前為止,它們僅向右移動並在到達邊緣時消失。 在代碼中,有3個敵人,它們都可以向右移動,當他們碰到“牆”時,如何使它們向左移動? 播放器可以雙向移動。
{
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 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;
}
}
這是我能夠測試的版本,與Picturebox完美搭配
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.