游戏界面 程序代码 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 Sys
游戏界面
程序代码
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 飞机大战 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Panel BG = new Panel();// Panel 容器 游戏区对象 Panel cebainqu = new Panel(); // 按钮区对象 PictureBox player = new PictureBox(); // 实例化飞机对象 Random rand = new Random();// 实例化随机对象 Timer CreateTime = new Timer();// 字母生成定时器 Timer Flytime = new Timer();// 字母下落定时器 Label btn = new Label(); // 创建按钮对象 Label defeng = new Label();// 得分卡对象 Label xuetiao = new Label();// 血条对象 int count = 0; // 存储器 记录得分 SoundPlayer baozhasound = new SoundPlayer(@"../../music/game_over.wav"); // 爆炸音乐对象 PictureBox feijwei1 = new PictureBox();// 创建飞机尾气对象 PictureBox feijwei2 = new PictureBox();// 创建飞机尾气对象 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 = "字母大战"; 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; // 创建游戏区 BG.Width = 800; BG.Height = 600; BG.BackColor = Color.White; this.Controls.Add(BG); BG.Location = new Point(20, 20); // 字母生成 CreateTime.Interval = 1000; // 设置生成毫秒数 CreateTime.Tick += CreateTime_Tick; // 控制下落 Flytime Flytime.Interval = 40; Flytime.Tick += Flytime_Tick; //创建的飞机 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; // 自适应大小 player.Tag = "player"; BG.Controls.Add(player); // 创建尾气 两个对象 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; // 创建按钮区 cebainqu.Width = 160; cebainqu.Height = 600; cebainqu.Location = new Point(820,20); cebainqu.BackColor = Color.FromArgb(180, 15, 123); this.Controls.Add(cebainqu); // 创建按钮 btn.Location = new Point(20, 20); btn.BorderStyle = BorderStyle.FixedSingle; btn.AutoSize = true; btn.Text = "游戏开始"; btn.Cursor = Cursors.Hand; // 鼠标移入到变为手型 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; // 获取始发者 if (btn.Text=="游戏开始") { CreateTime.Start(); // 字母生成定时器启动 Flytime.Start(); // 字母下落定时器启动 btn.BackColor = Color.Red; btn.ForeColor = Color.White; btn.Text = "游戏暂停"; } else if(btn.Text=="游戏暂停") { CreateTime.Stop(); // 字母生成定时器关闭 Flytime.Stop(); // 字母下落定时器关闭 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); // 随机到游戏区的宽度 lb.AutoSize = true; // 自适应大小 lb.BackColor = Color.Transparent; // 透明 lb.Tag = "zimu"; BG.Controls.Add(lb); // 字母添加到游戏区 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(); // 字母生成定时器关闭 Flytime.Stop(); // 字母下落定时器关闭 qing();// 调用清除字母方法 zdqing(); // 调用清除子弹方法 xuetiao.Size = new Size(100, 20);// 显示血条 defeng.Text = "得分: " + count + "分"; btn.Text = "游戏开始"; // MessageBox.Show弹框第一个参数为弹框内容,第二参数为标题,第三个参数为按钮,第四个参数为图标 MessageBox.Show("游戏结束"+"得分为:"+count+"分","游戏结束",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 设置为0 是为遍历动画图片 bombBox.Size = new Size(50, 50); // 设置爆炸图片在字母位置 bombBox.Location = new Point(zm.Left + zm.Width / 2 - bombBox.Width / 2, zm.Top - zm.Height / 2 + bombBox.Height / 2); BG.Controls.Add(bombBox); // 添加到游戏区 // 动画添加定时器 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的发起者 即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参数 foreach (Control item in BG.Controls) { // 判断按键值和字母对应 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; // 创建子弹 PictureBox bullet = new PictureBox(); bullet.Size = new Size(8,28); bullet.Tag = "zidan"; bullet.Image = Image.FromFile(@"../../img/Ammo1.png"); bullet.SizeMode = PictureBoxSizeMode.StretchImage; // 自适应大小 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; // 跳出创建 只创建一颗子弹 } } } // 字母清除方法 private void qing() { foreach (Label item in labels) { item.Dispose(); } } // 子弹清除方法 private void zdqing() { foreach (PictureBox item in picts) { item.Dispose(); } } // 任务栏中右键点击关闭事件 private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close();// 关闭窗体 } } }
到此这篇关于C#飞机打字游戏的代码示例(winform版)的文章就介绍到这了,更多相关C#飞机打字游戏内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!