C#實(shí)現(xiàn)打字游戲
本文實(shí)例為大家分享了C#實(shí)現(xiàn)打字游戲的具體代碼,供大家參考,具體內(nèi)容如下
思路:
1、有一個(gè)游戲界面,我用panel作為游戲界面
2、開(kāi)始生成字母
打字游戲的字母是不斷生成的,所以用計(jì)時(shí)器timer來(lái)生成字母
所有生成的字母設(shè)置tag方便尋找
3、字母下落
字母下落是一個(gè)持續(xù)的動(dòng)作,所以也在計(jì)時(shí)器里做
在計(jì)時(shí)器里通過(guò)foreach遍歷panel中的所有控件,同時(shí)通過(guò)tag找到字母,讓字母下降
4、生成子彈
通過(guò)獲取鍵盤(pán)事件生成子彈
5、子彈與字母相碰
代碼:
private void Form1_Load(object sender, EventArgs e)
{
this.panel1.BackColor = Color.White;
timer1.Start();
timer2.Start();
timer1.Interval = 1000;
timer2.Interval = 100;
fj.Tag = "feiji";
fj.Size = new Size(30, 40);
fj.BackColor = Color.Black;
fj.Text = "飛機(jī)";
fj.TextAlign = ContentAlignment.MiddleCenter;
fj.ForeColor = Color.White;
fj.Location = new Point(panel1.Width / 2 - fj.Width / 2, panel1.Height - fj.Height);
panel1.Controls.Add(fj);
}
Label fj = new Label();
Random r = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
Label zm = new Label();
zm.Tag = "zimu";
zm.Text = ((char)r.Next(97, 123)).ToString();
zm.Font = new Font("", r.Next(20, 30));
zm.AutoSize = true;
zm.Location = new Point(r.Next(0, panel1.Width - zm.Width), 0);
zm.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
panel1.Controls.Add(zm);
}
private void timer2_Tick(object sender, EventArgs e)
{
foreach (Control item in panel1.Controls)
{
if (item.Tag.ToString() == "zimu"||item.Tag.ToString()=="zzm")
{
item.Top += 5;
if (item.Top >= panel1.Height)
{
item.Dispose();
}
}else if (item.Tag.ToString() == "zidan")
{
item.Top -= 9;
foreach (Control con in panel1.Controls)
{
if (con.Tag.ToString() == "zzm")
{
if (con.Top + con.Height >= item.Top)
{
con.Dispose();
item.Dispose();
SoundPlayer ply = new SoundPlayer();
ply.SoundLocation = ".../.../Sound/MyBomb.wav";
ply.Play();
}
}
}
}
}
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
foreach (Control item in panel1.Controls)
{
if (item.Tag.ToString() == "zimu")
{
if (item.Text == e.KeyChar.ToString())
{
Label zd = new Label();
zd.Tag = "zidan";
zd.Size = new Size(20, 20);
item.Tag = "zzm";
zd.BackColor = Color.Red;
zd.Location = new Point(item.Left + item.Width / 2 - zd.Width / 2, fj.Top - fj.Height);
fj.Left = item.Left + item.Width / 2 - fj.Width / 2;
panel1.Controls.Add(zd);
return;
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#獲取目錄最后訪(fǎng)問(wèn)時(shí)間的方法
這篇文章主要介紹了C#獲取目錄最后訪(fǎng)問(wèn)時(shí)間的方法,涉及C#中LastAccessTime方法的使用技巧,需要的朋友可以參考下2015-04-04
C#反射之基礎(chǔ)應(yīng)用實(shí)例總結(jié)
這篇文章主要介紹了C#反射之基礎(chǔ)應(yīng)用實(shí)例總結(jié),包括了反射的基本原理與用法實(shí)例,需要的朋友可以參考下2014-10-10
WinForm實(shí)現(xiàn)最小化到系統(tǒng)托盤(pán)方法實(shí)例詳解
這篇文章主要介紹了WinForm實(shí)現(xiàn)最小化到系統(tǒng)托盤(pán)方法,實(shí)例分析了C#中實(shí)現(xiàn)WinForm最小化到系統(tǒng)托盤(pán)所需的相關(guān)控件與使用技巧,需要的朋友可以參考下2015-05-05
C#通過(guò)反射打開(kāi)相應(yīng)窗體方法分享
本文章來(lái)給各位同學(xué)介紹關(guān)于C#單擊菜單欄或工具欄時(shí)通過(guò)反射打開(kāi)窗體的方法,有需要了解的朋友可進(jìn)入?yún)⒖紖⒖肌?/div> 2015-05-05
C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別
這篇文章主要給大家介紹了關(guān)于C#中out參數(shù)、ref參數(shù)與值參數(shù)的用法及區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
c#數(shù)據(jù)綁定之向查詢(xún)中添加參數(shù)(.Net連接外部數(shù)據(jù)庫(kù))
本實(shí)例主要練習(xí)了ADO.Net連接到外部數(shù)據(jù)庫(kù)的基礎(chǔ)上,向查詢(xún)中添加參數(shù)。使用的是ACCESS數(shù)據(jù)庫(kù)2014-04-04
C#實(shí)現(xiàn)設(shè)置電腦顯示器參數(shù)
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)設(shè)置電腦顯示器參數(shù),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12最新評(píng)論

