C#飛機打字游戲的代碼示例(winform版)
更新時間:2021年03月22日 15:54:37 作者:Dust_SongYunfei
這篇文章主要介紹了C#飛機打字游戲的代碼示例(winform版),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
游戲界面
程序代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Media; namespace 飛機大戰(zhàn) { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Panel BG = new Panel();// Panel 容器 游戲區(qū)對象 Panel cebainqu = new Panel(); // 按鈕區(qū)對象 PictureBox player = new PictureBox(); // 實例化飛機對象 Random rand = new Random();// 實例化隨機對象 Timer CreateTime = new Timer();// 字母生成定時器 Timer Flytime = new Timer();// 字母下落定時器 Label btn = new Label(); // 創(chuàng)建按鈕對象 Label defeng = new Label();// 得分卡對象 Label xuetiao = new Label();// 血條對象 int count = 0; // 存儲器 記錄得分 SoundPlayer baozhasound = new SoundPlayer(@"../../music/game_over.wav"); // 爆炸音樂對象 PictureBox feijwei1 = new PictureBox();// 創(chuàng)建飛機尾氣對象 PictureBox feijwei2 = new PictureBox();// 創(chuàng)建飛機尾氣對象 List<Label> labels = new List<Label>(); // 實例化泛型和對象labels用來存儲字母 List<PictureBox> picts = new List<PictureBox>(); // 實例化泛型對象picts用來存儲子彈 private void Form1_Load(object sender, EventArgs e) { this.Size = new Size(1000, 700); //this.FormBorderStyle= FormBorderStyle.FixedToolWindow; // 去掉窗體圖標 this.Text = "字母大戰(zhàn)"; this.BackColor = Color.FromArgb(80, 00, 80); // this.BackgroundImage = Image.FromStream(@""); this.Left = Screen.PrimaryScreen.WorkingArea.Width / 2 - this.Width / 2; this.Top = Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2; // 創(chuàng)建游戲區(qū) BG.Width = 800; BG.Height = 600; BG.BackColor = Color.White; this.Controls.Add(BG); BG.Location = new Point(20, 20); // 字母生成 CreateTime.Interval = 1000; // 設(shè)置生成毫秒數(shù) CreateTime.Tick += CreateTime_Tick; // 控制下落 Flytime Flytime.Interval = 40; Flytime.Tick += Flytime_Tick; //創(chuàng)建的飛機 player.Size=new Size(80, 80); player.Top = BG.Height - player.Height-50; player.Left = BG.Width / 2 - player.Width / 2; player.Image = Image.FromFile(@"../../img/YP03.png"); player.SizeMode = PictureBoxSizeMode.StretchImage; // 自適應(yīng)大小 player.Tag = "player"; BG.Controls.Add(player); // 創(chuàng)建尾氣 兩個對象 feijwei1.Size = new Size(15, 30); feijwei1.Tag = 0; feijwei1.Top = player.Top + player.Height+feijwei1.Height/2-15; feijwei1.Left = player.Left + player.Width / 2 -feijwei1.Width-5; feijwei1.Image = imageList2.Images[0]; BG.Controls.Add(feijwei1); feijwei2.Size = new Size(15, 30); feijwei2.Tag = 0; feijwei2.Top= player.Top + player.Height + feijwei1.Height / 2 -15; feijwei2.Left = player.Left + player.Width / 2 + feijwei1.Width-5; feijwei2.Image = imageList3.Images[0]; BG.Controls.Add(feijwei2); // 尾氣1定時器 Timer weiqitimer1 = new Timer(); weiqitimer1.Tag = feijwei1; weiqitimer1.Tick += Weiqitimer1_Tick; weiqitimer1.Start(); //尾氣2定時器 Timer weiqitimer2 = new Timer(); weiqitimer2.Tag = feijwei2; weiqitimer2.Tick += Weiqitimer2_Tick; weiqitimer2.Start(); //添加鍵盤事件 this.KeyPress += Form1_KeyPress; // 創(chuàng)建按鈕區(qū) cebainqu.Width = 160; cebainqu.Height = 600; cebainqu.Location = new Point(820,20); cebainqu.BackColor = Color.FromArgb(180, 15, 123); this.Controls.Add(cebainqu); // 創(chuàng)建按鈕 btn.Location = new Point(20, 20); btn.BorderStyle = BorderStyle.FixedSingle; btn.AutoSize = true; btn.Text = "游戲開始"; btn.Cursor = Cursors.Hand; // 鼠標移入到變?yōu)槭中? btn.Font = new Font("", 15); btn.ForeColor = Color.FromArgb(97,177,48); btn.BackColor = Color.FromArgb(191,143,243); cebainqu.Controls.Add(btn); btn.Click += Btn_Click; // 得分卡 defeng.Font = new Font("", 15); defeng.Location = new Point(20, 50); defeng.AutoSize = true; defeng.Text = "得分: "+count+"分"; cebainqu.Controls.Add(defeng); //血條字體 Label xueTiao = new Label(); xueTiao.Text = " 能量"; xueTiao.Size = new Size(100, 30); xueTiao.Font = new Font("楷體",20); xueTiao.ForeColor = Color.Yellow; xueTiao.Location = new Point(20, 70); cebainqu.Controls.Add(xueTiao); // 血條 xuetiao.Size = new Size(100, 20); xuetiao.BackColor = Color.Red; xuetiao.Location = new Point(20, 100); cebainqu.Controls.Add(xuetiao); // 血條底部 Label xuetdi = new Label(); xuetdi.Size = new Size(100, 20); xuetdi.BackColor = Color.White; xuetdi.Location = new Point(20, 100); cebainqu.Controls.Add(xuetdi); } //飛機尾氣定時器1 private void Weiqitimer1_Tick(object sender, EventArgs e) { Timer weiqi1 = sender as Timer; PictureBox weiQi = weiqi1.Tag as PictureBox; weiQi.Image = imageList2.Images[(int)weiQi.Tag]; weiQi.Tag = (int)weiQi.Tag + 1; if ((int)weiQi.Tag > 1) { weiQi.Tag = 0; } } //飛機尾氣定時器2 private void Weiqitimer2_Tick(object sender, EventArgs e) { Timer weiqi2 = sender as Timer; PictureBox weiQi = weiqi2.Tag as PictureBox; weiQi.Image = imageList3.Images[(int)weiQi.Tag]; weiQi.Tag = (int)weiQi.Tag + 1; if ((int)weiQi.Tag>1) { weiQi.Tag = 0; } } // 游戲開始/暫停 private void Btn_Click(object sender, EventArgs e) { //Label btn = (Label)sender; // 獲取始發(fā)者 if (btn.Text=="游戲開始") { CreateTime.Start(); // 字母生成定時器啟動 Flytime.Start(); // 字母下落定時器啟動 btn.BackColor = Color.Red; btn.ForeColor = Color.White; btn.Text = "游戲暫停"; } else if(btn.Text=="游戲暫停") { CreateTime.Stop(); // 字母生成定時器關(guān)閉 Flytime.Stop(); // 字母下落定時器關(guān)閉 btn.BackColor = Color.FromArgb(191, 143, 243); btn.ForeColor = Color.FromArgb(97, 177, 48); btn.Text = "游戲開始"; } } private void CreateTime_Tick(object sender, EventArgs e) { // 生成字母在label中 Label lb = new Label(); lb.Text = ((char)rand.Next(97, 123)).ToString(); // 97-123 隨機ASCLL字母 lb.Font = new Font("", rand.Next(20, 30)); // 字體隨機15-20 lb.ForeColor =Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256)); lb.Top = 0; lb.Left = rand.Next(0, BG.Width - lb.Width); // 隨機到游戲區(qū)的寬度 lb.AutoSize = true; // 自適應(yīng)大小 lb.BackColor = Color.Transparent; // 透明 lb.Tag = "zimu"; BG.Controls.Add(lb); // 字母添加到游戲區(qū) labels.Add(lb); // 字母添加到labels中 } // 控件字母下落,子彈上升 private void Flytime_Tick(object sender, EventArgs e) { foreach (Control item in BG.Controls) { if (item.Tag.ToString()=="zimu" || item.Tag.ToString() == "biaoji") { item.Top += 5; if (item.Top > BG.Height) // 字母超過bg高度 字母刪除 { item.Dispose(); xuetiao.Width -= 10; if (xuetiao.Width==0) { CreateTime.Stop(); // 字母生成定時器關(guān)閉 Flytime.Stop(); // 字母下落定時器關(guān)閉 qing();// 調(diào)用清除字母方法 zdqing(); // 調(diào)用清除子彈方法 xuetiao.Size = new Size(100, 20);// 顯示血條 defeng.Text = "得分: " + count + "分"; btn.Text = "游戲開始"; // MessageBox.Show彈框第一個參數(shù)為彈框內(nèi)容,第二參數(shù)為標題,第三個參數(shù)為按鈕,第四個參數(shù)為圖標 MessageBox.Show("游戲結(jié)束"+"得分為:"+count+"分","游戲結(jié)束",MessageBoxButtons.YesNo,MessageBoxIcon.Information); count = 0; defeng.Text = "得分: " + count + "分"; } } } //判斷子彈 if (item.Tag.ToString()=="zidan") { item.Top -= 5; if (item.Top<0) // 當子彈超出bg高度 子彈刪除 { item.Dispose(); } foreach (Control zm in BG.Controls) { // 子彈碰到字母 if (zm.Tag.ToString()=="biaoji") { if (item.Top<=zm.Top+zm.Height && item.Left+item.Width/2==zm.Left+zm.Width/2) // 子彈的位置小于字母的位置 子彈穿過字母 { // 子彈,字母消失 item.Dispose(); zm.Dispose(); // 播放動畫 PictureBox bombBox = new PictureBox(); // 動畫對象 bombBox.Tag = 0; // 給bombBox的屬性Tag 設(shè)置為0 是為遍歷動畫圖片 bombBox.Size = new Size(50, 50); // 設(shè)置爆炸圖片在字母位置 bombBox.Location = new Point(zm.Left + zm.Width / 2 - bombBox.Width / 2, zm.Top - zm.Height / 2 + bombBox.Height / 2); BG.Controls.Add(bombBox); // 添加到游戲區(qū) // 動畫添加定時器 Timer bombTimer = new Timer(); bombTimer.Tag = bombBox; // 將bombBox存儲到bombTimer的Tag屬性中 bombTimer.Interval = 10; bombTimer.Tick += BombTimer_Tick; bombTimer.Start();// 開啟定時器 // 記錄得分 count++; defeng.Text = "得分: " + count.ToString() + "分"; // 開啟爆炸聲音 baozhasound.Play(); } } } } } } // 爆炸定時器 private void BombTimer_Tick(object sender, EventArgs e) { Timer bombtimer = (Timer)sender; // BombTimer的發(fā)起者 即bombTimer,重新bombtimer名 PictureBox picture = (PictureBox)bombtimer.Tag;// 獲取bombTimer的Tag屬性,即bombBox picture.Image = imageList1.Images[(int)picture.Tag];// 添加圖片 picture.Tag = (int)picture.Tag + 1; // 索引加1 if ((int)picture.Tag>31) // 超過索引為31時,定時器清除,圖片清除 { bombtimer.Dispose(); picture.Dispose(); } } // 鍵盤事件 private void Form1_KeyPress(object sender, KeyPressEventArgs e) { // 事件 e參數(shù) foreach (Control item in BG.Controls) { // 判斷按鍵值和字母對應(yīng) if (item.Text==e.KeyChar.ToString()&& item.Tag.ToString()=="zimu") { item.Tag = "biaoji"; //飛機的移動 飛機的left=字母的left+字母的width-飛機的width/2; player.Left = item.Left + item.Width / 2 - player.Width / 2; // 尾氣移動 feijwei1.Left = player.Left + player.Width / 2 - feijwei1.Width - 5; feijwei2.Left = player.Left + player.Width / 2 + feijwei1.Width - 5; // 創(chuàng)建子彈 PictureBox bullet = new PictureBox(); bullet.Size = new Size(8,28); bullet.Tag = "zidan"; bullet.Image = Image.FromFile(@"../../img/Ammo1.png"); bullet.SizeMode = PictureBoxSizeMode.StretchImage; // 自適應(yīng)大小 bullet.Left = player.Left + player.Width / 2 - bullet.Width / 2; // 在飛機上面 bullet.Top = player.Top - bullet.Height; BG.Controls.Add(bullet); picts.Add(bullet); // 子彈添加到picts中 return; // 跳出創(chuàng)建 只創(chuàng)建一顆子彈 } } } // 字母清除方法 private void qing() { foreach (Label item in labels) { item.Dispose(); } } // 子彈清除方法 private void zdqing() { foreach (PictureBox item in picts) { item.Dispose(); } } // 任務(wù)欄中右鍵點擊關(guān)閉事件 private void 關(guān)閉ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close();// 關(guān)閉窗體 } } }
到此這篇關(guān)于C#飛機打字游戲的代碼示例(winform版)的文章就介紹到這了,更多相關(guān)C#飛機打字游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
C#實現(xiàn)讀取二維數(shù)組集合并輸出到Word預(yù)設(shè)表格
這篇文章主要為大家詳細介紹了如何使用C#實現(xiàn)讀取二維數(shù)組集合并輸出到Word預(yù)設(shè)表格,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03LINQ基礎(chǔ)之Intersect、Except和Distinct子句
這篇文章介紹了LINQ使用Intersect、Except和Distinct子句的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04C#?實例解釋面向?qū)ο缶幊讨械膯我还δ茉瓌t(示例代碼)
本文我介紹了?SOLID?原則中的單一功能原則(single-responsibility?principle),并通過?C#?代碼示例簡明地詮釋了它的含意和實現(xiàn),對C#?面向?qū)ο缶幊淘瓌t感興趣的朋友跟隨小編一起看看吧2022-02-02