public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnS1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
playing = true;
}
private void btnS2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
playing = false;
}
int a = 0;
private void timer1_Tick(object sender, EventArgs e)
{
a = a + 10;
label2.Text = a.ToString();
pic1.Top -= 3;
if (pic1.Bottom < 0)
{
Random x1 = new Random();
pic1.Left = x1.Next(this.Width);
pic1.Top = Height + 10;
}
pic2.Top -= 3;
if (pic2.Bottom < 0)
{
Random x2 = new Random();
pic2.Left = x2.Next(this.Width);
pic2.Top = Height + 3;
}
pic3.Top -= 6;
if (pic3.Bottom < 0)
{
Random x3 = new Random();
pic3.Left = x3.Next(this.Width);
pic3.Top = Height + 11;
}
pic4.Top -= 8;
if (pic4.Bottom < 0)
{
Random x4 = new Random();
pic4.Left = x4.Next(this.Width);
pic4.Top = Height + 8;
}
picBoy.Top += 8;
if (picBoy.Top > Height)
{
timer1.Enabled = false;
playing = false;
MessageBox.Show("gameover", "GameOver", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
Application.Restart();
}
if (picBoy.Bottom >= pic1.Top && picBoy.Left <= pic1.Right && picBoy.Right >= pic1.Left)
{
picBoy.Top -= 11;
if (picBoy.Top > pic1.Top)
{
picBoy.Top += 8;
}
}
if (picBoy.Bottom >= pic2.Top && picBoy.Left <= pic2.Right && picBoy.Right >= pic2.Left)
{
picBoy.Top -= 11;
if (picBoy.Top > pic2.Top)
{
picBoy.Top += 8;
}
}
if (picBoy.Bottom >= pic3.Top && picBoy.Left <= pic3.Right && picBoy.Right >= pic3.Left)
{
picBoy.Top -= 14;
if (picBoy.Top > pic3.Top)
{
picBoy.Top += 8;
}
}
if (picBoy.Bottom >= pic4.Top && picBoy.Left <= pic4.Right && picBoy.Right >= pic4.Left)
{
picBoy.Top -= 16;
if (picBoy.Top > pic4.Top)
{
picBoy.Top += 8;
}
}
if (picBoy.Bottom < 0)
{
timer1.Enabled = false;
playing = false;
MessageBox.Show("gameover", "GameOver", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
Application.Restart();
}
}
bool playing = false;
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!playing)
{
return;
}
if (e.KeyChar == 'a')
{
picBoy.Left -= 15;
}
if (picBoy.Left < 0)
{
picBoy.Left += 15;
}
if (e.KeyChar == 'd')
{
picBoy.Left += 15;
}
if (picBoy.Right > this.Width)
{
picBoy.Left -= 15;
}
}
}
}
|