C#飞机打字游戏的代码示例(winform版)
程序员文章站
2022-04-01 08:05:32
游戏界面程序代码using system;using system.collections.generic;using system.componentmodel;using system.data;...
游戏界面
程序代码
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#飞机打字游戏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!