您的位置:首页 > 编程语言 > C#

C# 窗口实现打字游戏

2015-09-08 20:04 661 查看
按照惯例 首先先看一下运行之后的结果图



按相应的键值,发射出子弹,实现打字游戏!

上代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace sss
{
public partial class sssss : Form
{
public sssss()
{
InitializeComponent();
}
SoundPlayer mp = new SoundPlayer("BLAM.WAV");
SoundPlayer bz = new SoundPlayer("GRAY2WEA.WAV");

static int flag2 = 0;  //错误数
private void panel1_Paint(object sender, PaintEventArgs e)
{
this.timer1.Start();//表示开启移动的时间事件
this.timer2.Start();//开启随即造字的时间事件
}
//bool isShowBullet = true;
private void timer1_Tick(object sender, EventArgs e)
{
foreach (Control o in this.panel1.Controls)
{

if (o.GetType().Name == "Label")//判断是否是对应的控件
{
if ( o.Tag.ToString()=="over" || o.Tag.ToString()=="NoBiu") //判断是否向字母发射子弹
{
o.Top += 3;
}
else if (o.Tag.ToString() == "Biu")
{
o.Top -=8;
foreach(Control o1 in this.panel1.Controls)
{
if (o1.GetType().Name == "Label" && o1.Tag.ToString()=="over")
{

if (o1.Bottom >= o.Top)
{
o1.Dispose();
o.Dispose();

//加爆炸
PictureBox p = new PictureBox();
p.Size = new Size(100, 100);
p.Location = new Point(o1.Left+o1.Width/2-p.Left,o1.Bottom-p.Height/2);

this.panel1.Controls.Add(p);

Timer t = new Timer();
t.Interval = 10;
t.Tag = p;
t.Tick += new EventHandler(t_Tick);
t.Start();
}

}
}
}
}

if (o.GetType().Name == "Label")
{
if (((Label)o).Top >= this.panel1.Height)
{
((Label)o).Dispose();//释放资源
flag2++;
this.Tecuo.Text = flag2.ToString();
}
}
}

}
int num = 0;
void t_Tick(object sender, EventArgs e)
{
Timer tt = (Timer)sender;
PictureBox pp = (PictureBox)tt.Tag;
pp.Image = imageList1.Images[num];
num++;
if (num == 26)
{
num = 0;
pp.Dispose();
tt.Dispose();
}
}
Random ran = new Random();
static int flag1 = 0; //总数
int randon;
private void timer2_Tick(object sender, EventArgs e) //产生随机字体
{

randon = ran.Next(0,1000);
Label la = new Label();
la.AutoSize = true;//自动适应期内容的大小
la.Font = new Font("宋体", ran.Next(30, 70));//随机产生字体大小
la.Tag = "NoBiu";

la.ForeColor = Color.FromArgb(ran.Next(0, 256), ran.Next(0, 256), ran.Next(0, 256));//
la.Location = new Point(ran.Next(0, this.panel1.Width - la.Width), ran.Next(-10, 0));
if (randon % 2 == 0)
{
la.Text = ((char)ran.Next(97, 123)).ToString();
}
else
{
la.Text = ran.Next(0, 9).ToString();
}
this.panel1.Controls.Add(la);//将Label添加到里面
flag1++;
this.number.Text = flag1.ToString();

}

private void sssss_Load(object sender, EventArgs e)
{
// comboBox1.SelectedIndex = comboBox1.SelectedIndex[0];
// comboBox1.SelectedIndex[0] = comboBox1.

}
static int flag = 0;//   ddddd正确数

private void sssss_KeyPress(object sender, KeyPressEventArgs e)
{
foreach (Control o in this.panel1.Controls)
{
if (o.GetType().Name == "Label" && o.Tag.ToString()=="NoBiu")//
{
if (e.KeyChar.ToString().ToLower() == ((Label)o).Text.ToLower())//进行小写装换
{
flag++;
this.score.Text = flag.ToString();
o.Tag = "over";
pictureBox1.Location = new Point(o.Left + o.Width / 2 - pictureBox1.Width / 2, pictureBox1.Top);//移动到相应位置
Label la = new Label();
la.Text = "D";
la.Location = new Point(pictureBox1.Left + pictureBox1.Width / 2, pictureBox1.Top - la.Height);
this.panel1.Controls.Add(la);
la.Font = new Font("宋体", 8);
la.Tag = "Biu";
mp.Play();
return;
}

}

}
}

private void button1_Click(object sender, EventArgs e)
{
//this.Close();
Application.Exit();
}

private void label2_Click(object sender, EventArgs e)
{

}

private void timer3_Tick(object sender, EventArgs e)  //用来自动的选择难度
{
foreach (Control o in this.panel1.Controls)
{
if (o.GetType().Name == "Label")//判断是否是对应的控件
{
((Label)o).Top += 3;
}

if (flag1 >= 30)
{
if (o.GetType().Name == "Label")//判断是否是对应的控件
{
((Label)o).Top += 4;
}
}
else if (flag1 >= 30 && flag1 <= 60)
{
if (o.GetType().Name == "Label")//判断是否是对应的控件
{
((Label)o).Top += 5;
}
}
else if (flag1 >= 60 && flag1 <= 180)
{
if (o.GetType().Name == "Label")//判断是否是对应的控件
{
((Label)o).Top += 7;
}
}
else if (flag1 > 180)
{
if (o.GetType().Name == "Label")//判断是否是对应的控件
{
((Label)o).Top += 10;
}
}

}

}

private void button3_Click(object sender, EventArgs e)
{
timer3.Start();
timer1.Stop();
}
}
}


上面就是整个工程的代码!没有一一讲解是由于时间原因,临近毕业时间比较紧张,还希望谅解!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: