游戏界面

程序代码

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; // 添加音乐需引入此命名空间
using System.Reflection;
namespace 飞机大战__
{public partial class Form1 : Form{public Form1(){InitializeComponent();}Random ran = new Random(); // 创建随机对象PictureBox youxiqtupian = new PictureBox(); // 创建游戏开始图片Label youxiqu = new Label();// 创建游戏区对象Label kaiguanqu = new Label();// 创建开关区对象PictureBox player = new PictureBox();// 创建飞机对象PictureBox weiqi1 = new PictureBox();// 尾气对象1PictureBox weiqi2 = new PictureBox(); // 尾气对象2Timer dijiTimer = new Timer(); // 敌机生成定时器Timer xiaLuoTimer = new Timer();// 敌机下落定时器Timer zidanTimer = new Timer(); // 子弹生成定时器Timer xiangShangTimer = new Timer(); // 子弹向上定时器SoundPlayer baozhasound = new SoundPlayer(); // 爆炸音乐对象SoundPlayer jiemiansound = new SoundPlayer(); // 游戏界面音乐对象int num = 1; // 敌机随机初始飞机名int num1 = 10; // 飞机移动的数,子弹移动的数int feng = 0;//分数存储器Label kaiguan = new Label(); //开关对象Label defeng = new Label();// 得分对象Label xuetiao = new Label();//血条对象List<PictureBox> zdList = new List<PictureBox>();// 存储生成的子弹List<PictureBox> feijiList = new List<PictureBox>();// 存储飞机和尾气List<PictureBox> dijiList = new List<PictureBox>();// 存储生成敌机private void Form1_Load(object sender, EventArgs e){this.Size = new Size(1080, 700);this.Text = "飞机大战";this.BackColor = Color.FromArgb(80,00,80);this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2-this.Width/2, Screen.PrimaryScreen.WorkingArea.Height / 2-this.Height/2);// 创建游戏区图片,同样的位置youxiqtupian.Size = new Size(800, 600);youxiqtupian.Tag = "jiemianTu";youxiqtupian.Image = Image.FromFile(@"../../img/youxikaishi.bmp");youxiqtupian.SizeMode = PictureBoxSizeMode.StretchImage;youxiqtupian.Location = new Point(20, 20);this.Controls.Add(youxiqtupian);// 创建游戏区youxiqu.Size = new Size(800, 600);youxiqu.BackColor = Color.White;youxiqu.Location = new Point(20, 20);this.Controls.Add(youxiqu);  // 创建开关区kaiguanqu.Size = new Size(200, 600);kaiguanqu.BackColor = Color.FromArgb(180, 15, 123);kaiguanqu.Location = new Point(840, 20);this.Controls.Add(kaiguanqu);// 创建飞机对象playerChuan();// 调用创建方法youxiqu.Controls.Add(player);feijiList.Add(player);//敌机生成定时器dijiTimer.Interval = 1500;dijiTimer.Tick += DijiTimer_Tick;// 敌机下落定时器xiaLuoTimer.Interval = 20;xiaLuoTimer.Tick += XiaLuoTimer_Tick;// 子弹生成定时器zidanTimer.Interval = 150;zidanTimer.Tick += ZidanTimer_Tick;// 子弹向上移动xiangShangTimer.Interval = 10;xiangShangTimer.Tick += XiangShangTimer_Tick;baozhasound.SoundLocation = @"../../music/game_over.wav";// 爆炸音乐路径jiemiansound.SoundLocation = @"../../music/game_music.wav";//界面音乐路径jiemiansound.PlayLooping(); //游戏界面音乐开启// 创建飞机尾气 两个// 尾气对象1weiqi1.Size = new Size(15,30);weiqi1.Tag = 0;weiqi1.Location =new Point(player.Left + player.Width / 2 - weiqi1.Width / 2 - 10,player.Top + player.Height + weiqi1.Height / 2 - 15);weiqi1.Image = imageList1.Images[0];weiqi1.SizeMode = PictureBoxSizeMode.StretchImage;youxiqu.Controls.Add(weiqi1);feijiList.Add(weiqi1);// 尾气对象2weiqi2.Size = new Size(15, 30);weiqi2.Tag = 0;weiqi2.Location = new Point(player.Left + player.Width / 2 + weiqi2.Width / 2 - 5, player.Top + player.Height + weiqi2.Height / 2 - 15);weiqi2.Image = imageList1.Images[0];weiqi2.SizeMode = PictureBoxSizeMode.StretchImage;youxiqu.Controls.Add(weiqi2);feijiList.Add(weiqi2);//创建尾气定时器Timer weiqiTimer = new Timer();weiqiTimer.Interval = 10;weiqiTimer.Tick += WeiqiTimer_Tick;weiqiTimer.Start();//创建开始游戏,暂停游戏按钮kaiguan.Size = new Size(150, 30);kaiguan.Location = new Point(30, 20);kaiguan.AutoSize = true;kaiguan.Cursor =Cursors.Hand; //鼠标变为手型kaiguan.Text = "开始游戏";kaiguan.Font = new Font("楷体",18);kaiguan.ForeColor = Color.FromArgb(97, 177, 48);kaiguan.BackColor = Color.FromArgb(191, 143, 243);kaiguan.Click += Kaiguan_Click1;kaiguanqu.Controls.Add(kaiguan);// 创建得分对象defeng.Size = new Size(150, 30);defeng.Location = new Point(30, 60);defeng.Text = "得分:" +feng + "分";defeng.Font = new Font("宋体", 20);defeng.ForeColor = Color.Yellow;kaiguanqu.Controls.Add(defeng);// 血量字体Label xlZi = new Label();xlZi.Size = new Size(150, 30);xlZi.Location = new Point(30, 90);xlZi.Text = "飞机血量";xlZi.Font = new Font("楷体", 20);xlZi.ForeColor = Color.Red;kaiguanqu.Controls.Add(xlZi);// 创建血条对象xuetiao.Size = new Size(160, 15);xuetiao.Location = new Point(30, 120);xuetiao.BackColor = Color.Red;kaiguanqu.Controls.Add(xuetiao);//创建血条底部对象Label xueTiaodi = new Label();xueTiaodi.Size = new Size(160,15);xueTiaodi.Location = new Point(30, 120);xueTiaodi.BackColor = Color.White;kaiguanqu.Controls.Add(xueTiaodi);/*// 在游戏区添加鼠标移动事件youxiqu.MouseMove += Form1_MouseMove;*/// 键盘事件this.KeyDown += Form1_KeyDown;}//游戏开关,暂停点击private void Kaiguan_Click1(object sender, EventArgs e){if (kaiguan.Text == "开始游戏"){jiemiansound.Stop(); //游戏界面音乐关闭youxiqtupian.Dispose(); //游戏开始图片消失dijiTimer.Start(); //敌机生成xiaLuoTimer.Start();//敌机下落zidanTimer.Start();// 子弹生成xiangShangTimer.Start(); // 子弹上升kaiguan.BackColor = Color.Red;kaiguan.ForeColor = Color.White;kaiguan.Text = "暂停游戏";}else if (kaiguan.Text == "暂停游戏"){dijiTimer.Stop(); //敌机暂停生成xiaLuoTimer.Stop();//敌机暂停下落zidanTimer.Stop();// 子弹暂停生成xiangShangTimer.Stop(); // 子弹暂停上升ziDqing(); //子弹清除kaiguan.BackColor = Color.FromArgb(191, 143, 243);kaiguan.ForeColor = Color.FromArgb(97, 177, 48);kaiguan.Text = "开始游戏";}}// 尾气动画int weiQi1 = 0,weiQi2 = 0; // 存储图片数private void WeiqiTimer_Tick(object sender, EventArgs e){   // 尾气1添加图片weiqi1.Image = imageList1.Images[weiQi1];weiQi1++;if (weiQi1>=imageList1.Images.Count){weiQi1 = 0;}// 尾气2添加图片weiqi2.Image = imageList1.Images[weiQi2];weiQi2++;if (weiQi2 >=imageList1.Images.Count){weiQi2 = 0;}}/*//游戏区鼠标移动事件private void Form1_MouseMove(object sender, MouseEventArgs e){player.Left = MousePosition.X - this.Left - youxiqu.Left - player.Width / 2;player.Top = MousePosition.Y - this.Top - youxiqu.Top - player.Height / 2;}*///敌机生成的定时器private void DijiTimer_Tick(object sender, EventArgs e){//创建敌机对象PictureBox diji = new PictureBox(); // 创建敌机对象  diji.Size = new Size(70, 60);diji.Tag = "diji";num = ran.Next(1, 5);diji.Image = Image.FromFile(@"../../img/diji/Enemy" + num + ".png");if (num==4){diji.Size =new Size(100, 80);}diji.SizeMode = PictureBoxSizeMode.StretchImage;// 图片自适应大小diji.Location = new Point(ran.Next(youxiqu.Width - diji.Width), 0); //敌机初始位置youxiqu.Controls.Add(diji);dijiList.Add(diji);}// 敌机下落的定时器private void XiaLuoTimer_Tick(object sender, EventArgs e){foreach (Control diji in youxiqu.Controls){if (diji.Tag.ToString()=="diji"){diji.Top += 5;if (diji.Top>youxiqu.Height) // 敌机超过游戏区高度{diji.Dispose(); //敌机释放(删除)xuetiao.Width -= 20;if (xuetiao.Width<=0){diJiqing();// 敌机清除dijiTimer.Stop();// 敌机创建关闭ziDqing();//子弹清除zidanTimer.Stop();// 子弹创建关闭//创建弹框if ((MessageBox.Show("游戏结束,你的得分:" + feng, "Game Over", MessageBoxButtons.RetryCancel) == DialogResult.Retry)){  feng = 0;defeng.Text = "得分" + feng + "分";xuetiao.Width = 160;kaiguan.Text = "游戏开始";dijiTimer.Start(); //敌机生成xiaLuoTimer.Start();//敌机下落zidanTimer.Start();// 子弹生成xiangShangTimer.Start(); // 子弹上升playerChuan();// 飞机创建weiqi1.Location = new Point(player.Left + player.Width / 2 - weiqi1.Width / 2 - 10, player.Top + player.Height + weiqi1.Height / 2 - 15);weiqi2.Location = new Point(player.Left + player.Width / 2 + weiqi2.Width / 2 - 5, player.Top + player.Height + weiqi2.Height / 2 - 15);ziDqing(); // 子弹清除diJiqing(); //敌机清除}else{this.Close();}}}foreach (Control player in youxiqu.Controls){if (player.Tag.ToString() == "player"){   //敌机碰到飞机if (diji.Left >= player.Left && diji.Left <= player.Left + player.Width && diji.Top >= player.Top && diji.Top <= player.Top + player.Height){diJiqing();// 调用清除敌机方法ziDqing(); // 调用子弹清除方法zidanTimer.Stop();// 子弹暂停生成xiangShangTimer.Stop(); // 子弹暂停上升dijiTimer.Stop();// 敌机生成清除kaiguan.Text = "游戏开始";DialogResult jieshukuang = MessageBox.Show("总得分为:" + feng + "分," + "是否重新开始", "游戏结束", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);if (jieshukuang == DialogResult.Retry) // 点击确定按钮{feng = 0;defeng.Text = "得分" + feng + "分";xuetiao.Width = 160;kaiguan.Text = "游戏开始";dijiTimer.Start(); //敌机生成xiaLuoTimer.Start();//敌机下落zidanTimer.Start();// 子弹生成xiangShangTimer.Start(); // 子弹上升playerChuan();// 飞机创建weiqi1.Location = new Point(player.Left + player.Width / 2 - weiqi1.Width / 2 - 10, player.Top + player.Height + weiqi1.Height / 2 - 15);weiqi2.Location = new Point(player.Left + player.Width / 2 + weiqi2.Width / 2 - 5, player.Top + player.Height + weiqi2.Height / 2 - 15);ziDqing(); // 子弹清除diJiqing(); //敌机清除}else if (jieshukuang == DialogResult.Cancel)//点击取消按钮{this.Close();// 关闭窗体}}}}}}}// 子弹生成定时器private void ZidanTimer_Tick(object sender, EventArgs e){PictureBox zidan = new PictureBox();// 创建子弹zidan.Tag = "zd";zidan.Size = new Size(8, 30);zidan.Left = player.Left +player.Width / 2 - zidan.Width / 2;zidan.Top = player.Top - zidan.Height;zidan.Image = Image.FromFile(@"../../img/Ammo1.png");zidan.SizeMode = PictureBoxSizeMode.StretchImage; // 图片自适应大小youxiqu.Controls.Add(zidan);zdList.Add(zidan); //子弹添加到子弹泛型对象中}// 子弹向上private void XiangShangTimer_Tick(object sender, EventArgs e){foreach (Control zidan  in youxiqu.Controls){if (zidan.Tag.ToString()=="zd") // 获取子弹{zidan.Top -= num1;if (zidan.Top<0) // 子弹超过游戏区{zidan.Dispose();}foreach (Control diji in youxiqu.Controls){ if (diji.Tag.ToString() == "diji")  // 获取敌机{// 子弹碰撞敌机if (zidan.Top >= diji.Top && zidan.Top <= diji.Top + diji.Height && zidan.Left>=diji.Left && zidan.Left<=diji.Left+diji.Width){zidan.Dispose(); // 子弹清除diji.Dispose(); // 敌机清除baozhasound.Play(); // 开启爆炸声if (diji.Width==100){feng += 10;}else{feng += 1;}feng++;defeng.Text = "得分:" + feng + "分";// 创建爆炸对象PictureBox baozha = new PictureBox();baozha.Tag = 0;baozha.Size = new Size(50, 50);baozha.Image = imageList2.Images[0];baozha.SizeMode = PictureBoxSizeMode.StretchImage; // 图片自适应大小baozha.Location = new Point(diji.Left + diji.Width / 2 - baozha.Width / 2, diji.Top - diji.Height / 2 + baozha.Height / 2);youxiqu.Controls.Add(baozha);// 爆炸定时器Timer baozhaTimer = new Timer();baozhaTimer.Tag = baozha;baozhaTimer.Interval = 10;baozhaTimer.Tick += BaozhaTimer_Tick; ;baozhaTimer.Start(); // 爆炸开启}}}}}}// 爆炸动画private void BaozhaTimer_Tick(object sender, EventArgs e){Timer baozhaTimer = sender as Timer;PictureBox baoZha = baozhaTimer.Tag as PictureBox;baoZha.Image = imageList2.Images[(int)baoZha.Tag];baoZha.Tag = (int)baoZha.Tag + 1;if ((int)baoZha.Tag>31){baozhaTimer.Dispose();baoZha.Dispose();}}//键盘事件private void Form1_KeyDown(object sender, KeyEventArgs e){//飞机向左if (e.KeyCode == Keys.A || e.KeyCode == Keys.Left){player.Left -= num1;weiqi1.Left -= num1; // 尾气1向左weiqi2.Left -= num1; // 尾气2向左  if (player.Left == 0){player.Left = num1;weiqi1.Left = player.Left + player.Width / 2 - weiqi1.Width / 2 - 10;weiqi2.Left = player.Left + player.Width / 2 + weiqi2.Width / 2 - 5;}}// 飞机向右if (e.KeyCode==Keys.D || e.KeyCode==Keys.Right){player.Left += num1;weiqi1.Left += num1;weiqi2.Left += num1;if (player.Left + player.Width >= youxiqu.Width){player.Left = youxiqu.Width - player.Width;weiqi1.Left = player.Left + player.Width / 2 - weiqi1.Width / 2 - 10;weiqi2.Left = player.Left + player.Width / 2 + weiqi2.Width / 2 - 5;}}// 飞机向上if (e.KeyCode==Keys.W|| e.KeyCode==Keys.Up){player.Top -= num1;weiqi1.Top -= num1;weiqi2.Top -= num1;if (player.Top == 0){player.Top = num1;weiqi1.Top = player.Top + player.Height + weiqi1.Height / 2 - 15;weiqi2.Top = player.Top + player.Height + weiqi2.Height / 2 - 15;}}// 飞机向下if (e.KeyCode==Keys.S || e.KeyCode==Keys.Down){player.Top += num1;weiqi1.Top += num1;weiqi2.Top += num1;if (player.Top + player.Height + weiqi1.Height >= youxiqu.Height){player.Top = youxiqu.Height - player.Height - weiqi1.Height;weiqi1.Top = player.Top + player.Height + weiqi1.Height / 2 - 15;weiqi2.Top = player.Top + player.Height + weiqi2.Height / 2 - 15;}}}//子弹消除方法private void ziDqing(){foreach (PictureBox zidan in zdList){zidan.Dispose();}}//飞机消除方法private void feiJiqing(){foreach (PictureBox feiji in feijiList){feiji.Dispose();}}// 敌机消除方法private void diJiqing(){foreach (PictureBox diji in dijiList){diji.Dispose();}}private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e){this.Close();}// 飞机创建方法private void playerChuan(){player.Size = new Size(70, 60);player.Tag = "player";player.Left = youxiqu.Width / 2 - player.Width / 2; // 飞机初始位置player.Top = youxiqu.Height - player.Height - 20;player.Image = Image.FromFile(@"../../img/RP03.png");player.SizeMode = PictureBoxSizeMode.StretchImage; // 图片自适应大小}}
}

基于C# WinForms窗体——飞机大战相关推荐

  1. 【java毕业设计】基于java+Eclipse的飞机大战游戏设计与实现(毕业论文+程序源码)——飞机大战游戏

    基于java+Eclipse的飞机大战游戏设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于java+Eclipse的飞机大战游戏设计与实现,文章末尾附有本毕业设计的论文和源码下载地址哦. ...

  2. 基于pygame实现的飞机大战游戏

    目录 1.引言 1.1 背景 1.2 意义 1.3 功能 2.系统结构 2.1 整体框架 2.2 精灵与精灵组 2.3 功能介绍 2.3.1 玩家飞机 2.3.2 敌机类型和关卡设定 2.3.3 敌机 ...

  3. 基于C#制作一个飞机大战小游戏

    此文主要基于C#制作一个飞机大战游戏,重温经典的同时亦可学习. 实现流程 1.创建项目 2.界面绘制 3.我方飞机 4.敌方飞机 5.子弹及碰撞检测 实现流程 1.创建项目 打开Visual Stud ...

  4. 基于IntelliJ IDEA的飞机大战游戏设计与实现

    目 录 1 引言 1 1.1 项目背景 1 2 国内外研究现状 3 1.3 项目主要工作 4 1.4 本文组织结构 6 2 开发平台与开发技术 7 1 IntelliJ IDEA简介 7 2. 2 I ...

  5. 基于Python/Tkinter的飞机大战单机小游戏

    这是很早之前课余时间写的基于Python/Tkinter单机小游戏,用来练手,今天将代码贴出来,方便大家一起学习,通过Py/Tk对于学习GUI作为一个入口,其实是个不错入口,在这里推荐一下Tcl/Tk ...

  6. 树莓派基于PS2操纵杆的飞机大战小游戏

    击中病毒LED灯闪烁 PS2操纵杆.PCF8591 PCF8591: SDA - SDA SCL - SCL VCC - 5V GND - GND AIN1.AIN2.AIN3找空插即可 PS2操纵杆 ...

  7. 无聊消遣——基于pygame库的飞机大战

    前一段在数据分析中突然感觉到了一阵阵的空虚寂寞冷,所以我决定小小放松一下,于是萌生出了写一个小游戏的想法.所以在pygame中摸索了2天,终于完成了无聊的飞机大战代码.之前从来没写过游戏,所以感觉还蛮 ...

  8. c语言基于easyx库的 飞机大战游戏(鼠标控制飞机移动,武器升级,boss发射散弹,boss血条等功能)

    课设题目 实现功能: 飞机鼠标控制–飞机武器升级–敌机发射子弹–boss发射散弹–boss血条记录–我方多条生命 图片资源和源码在下面 链接:https://pan.baidu.com/s/1uTQV ...

  9. [内附完整源码和文档] 基于C++的空中飞机大战游戏

    一.问题描述 项目选题为做一个飞行射击游戏.游戏中有各总各样的敌机和武器,敌机按一定的几率出现,玩家消灭敌机后可得分数奖励,达到一定分数后会升级,游戏难度随着玩家级数的增加而增大.游戏有很多爆炸场面, ...

  10. 飞机大战游戏python_基于Python的飞机大战游戏

    基于 Python 的飞机大战游戏 杨铠 [期刊名称] <电脑迷> [年 ( 卷 ), 期] 2017(000)021 [摘要] 我们每天都享受到科技带来的好处 , 了解计算机编程对每个人 ...

最新文章

  1. java的异常抛出throws和throw的简单使用
  2. 重装系统找不到固态_90%的人重装系统后,感觉电脑变快了
  3. Open NI for Kinect安装测试
  4. Python 列表 list() 方法
  5. python字符集_PYTHON 中的字符集
  6. 安装numpy,setuptools的时候,不能再注册表中识别出来python2.7
  7. iframe打印excel bold_搭载君正X1000E芯片 中盈SP7080激光打印机现已开售!
  8. 【载誉】致远互联荣获“2017最佳协同管理解决方案”殊荣
  9. zabbix简介及部署
  10. 奔向成功,有志人士创业的十大策略
  11. Winform使用FTP实现自动更新
  12. 加载gif图片html,JS实现的自定义显示加载等待图片插件(loading.gif)
  13. VMware 8安装Mac OS X 10.8 Lion
  14. 二进制炸弹——拆弹实验
  15. CISCO 关闭4786端口解决方法 cisco IOS and IOS XE software Smart Install protocol Misuse
  16. Unity3D数字孪生开发笔记——网络篇
  17. 试图运行项目时出错:未能加载文件或程序集 或它的某一个依赖项。该模块应包含一个程序集清单 .
  18. 淘宝活动应该怎么参加吗?报名活动怎样容易通过?
  19. hcna学习小结第一天
  20. 我国计算机发展优势,浅析我国计算机应用发展.doc

热门文章

  1. 【C#】利用读卡器对IC卡进行读写、加密等操作,源码下载
  2. OKI系列针式打印机更换色带图解教程
  3. Python实现简易图形用户界面计算器
  4. SGX Architectural Encalve(AE)及SGX密钥
  5. xapp1025仿真
  6. linux下动态库的符号冲突、隐藏和强制优先使用库内符号
  7. 什么是对象存储OSS,看完你就懂了
  8. linux查看显卡温度cpu温度,怎样从指令提示符窗口查看cpu温度
  9. 度量学习中的马氏距离
  10. 三维马氏距离_马氏距离2