C#实现简单打字小游戏
程序员文章站
2024-01-24 08:30:22
本文实例为大家分享了c#实现简单打字小游戏的具体代码,供大家参考,具体内容如下using system;using system.collections.generic;using system.co...
本文实例为大家分享了c#实现简单打字小游戏的具体代码,供大家参考,具体内容如下
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; namespace 打字游戏 { public partial class form1 : form { public form1() { initializecomponent(); } random r = new random(); //游戏区 panel gamearea = new panel(); //控制区 panel area = new panel(); //装鸟的盒子 picturebox bird = new picturebox(); //字母出现定时器 timer zimu = new timer(); //飞鸟与字母下落定时器 timer fly = new timer(); //开始/暂停按钮 button button = new button(); //积分器 label scoring = new label(); //血条 label bar = new label(); //尾翼 picturebox wei = new picturebox(); picturebox weiyi = new picturebox(); //装字母的盒子 list<label> zmbox = new list<label>(); private void form1_load(object sender, eventargs e) { //button设置 this.keypreview = true; //最大化窗口 this.windowstate = formwindowstate.maximized; //背景图 this.backgroundimage = image.fromfile("../../img/背景 (2).jpg"); //拉伸 this.backgroundimagelayout = imagelayout.stretch; //游戏区设置 gamearea.size = new size(1000,750); //游戏区位置 gamearea.location = new point(30,30); this.controls.add(gamearea); gamearea.tag = "game"; //控制区设置 area.size = new size(300,750); //控制区位置 area.location = new point(gamearea.left+gamearea.width+20,30); area.backcolor = color.cyan; this.controls.add(area); //开始/暂停按钮 //button button = new button(); button.text = "开始"; this .keypreview = true; //字体大小 button.font = new font("",20); //按钮大小 button.size = new size(100,50); //按钮颜色 button.backcolor = color.blue; //按钮位置 button.location = new point(100,20); area.controls.add(button); //按钮点击事件 button.click += button_click; //积分器 //label scoring = new label(); scoring.text = "积分:0"; scoring.font = new font("",15); scoring.location = new point(100,100); area.controls.add(scoring); //装鸟的盒子设置 bird.tag = "niao"; bird.size = new size(200,200); bird.location = new point(0,0); //动画放入盒子 bird.image = imagelist1.images[0]; gamearea.controls.add(bird); //飞鸟与字母下落定时器 //timer fly = new timer(); fly.interval = 80; fly.tick += fly_tick; //fly.start(); //字母出现定时器 //timer zimu = new timer(); zimu.interval = 1000; zimu.tick += zimu_tick; //zimu.start(); //键盘 this.keypress += form1_keypress1; //飞机 //picturebox plane = new picturebox(); plane.tag = "plane"; //盒子大小 plane.size = new size(100,100); //放进图片 plane.image = image.fromfile("../../img/rp03.png"); //图片自适应 plane.sizemode = pictureboxsizemode.stretchimage; //飞机位置 plane.location = new point(gamearea.width/2-plane.width/2,gamearea.height-plane.height-60); gamearea.controls.add(plane); //血条 //label bar = new label(); bar.tag = "bar"; bar.size = new size(100, 10); bar.backcolor = color.red; //位置 bar.left = plane.left + plane.width - bar.width; bar.top = plane.top - bar.height; gamearea.controls.add(bar); //尾翼 wei.tag = "wei"; wei.size = new size(30, 40); //位置 wei.left = plane.left + plane.width / 2; wei.top = plane.top + plane.height; //图片集放进盒子 wei.image = imagelist3.images[0]; gamearea.controls.add(wei); weiyi.tag = "weiji"; //图片集放进盒子 weiyi.image = imagelist3.images[0]; weiyi.size = new size(30, 40); //位置 weiyi.left = plane.left + plane.width /2-28; weiyi.top = plane.top + plane.height; gamearea.controls.add(weiyi); } //按钮设置 private void button_click(object sender, eventargs e) { if (button.text=="开始") { fly.start(); zimu.start(); button.text = "暂停"; } else if (button.text=="暂停") { fly.stop(); zimu.stop(); button.text = "开始"; } } //飞机 picturebox plane = new picturebox(); //键盘事件 private void form1_keypress1(object sender, keypresseventargs e) { //遍历游戏区内所有控件 foreach (control item in gamearea.controls) { //找到name为label的控件 if (item.gettype().name == "label") { //判断按键与字母是否相同 if (item.text == e.keychar.tostring()&& item.tag.tostring() == "zm") { ////消除字母 //item.dispose(); plane.left = item.left + item.width / 2 - plane.width / 2; //血条随飞机 bar.left = plane.left + plane.width - bar.width; bar.top = plane.top - bar.height; //尾翼随飞机 wei.left = plane.left + plane.width / 2; wei.top = plane.top + plane.height; weiyi.left = plane.left + plane.width / 2 - 28; weiyi.top = plane.top + plane.height; item.tag = "bj"; //创造子弹 picturebox bullet = new picturebox(); bullet.tag = "bullet"; bullet.size = new size(10,30); //放进图片 bullet.image = image.fromfile("../../img/ammo1.png"); //图片自适应 bullet.sizemode = pictureboxsizemode.stretchimage; //子弹位置 bullet.location = new point(plane.left+plane.width/2-bullet.width/2,plane.top-bullet.height); gamearea.controls.add(bullet); return; } } } } //字母 private void zimu_tick(object sender, eventargs e) { //判断飞鸟出现屏幕字母开始下落 if (bird.left >= 0 && bird.left <= gamearea.width - bird.width) { label zm = new label(); zm.tag = "zm"; //随机字母 zm.text =((char)r.next(97,123)).tostring(); //随机大小 zm.font = new font("",r.next(10,45)); //字体自适应 zm.autosize = true; //随机颜色 zm.forecolor = color.fromargb(r.next(255), r.next(255), r.next(255)); //随着鸟的位置下落 zm.top = bird.height; zm.left = bird.left + bird.width / 2 - zm.width / 2; //添加进游戏区 gamearea.controls.add(zm); zmbox.add(zm); } } //播放飞鸟动画 int bo = 0; //积分 int fen = 0; int t = 0, y = 0; private void fly_tick(object sender, eventargs e) { //飞鸟动画播放 bird.image = imagelist1.images[bo]; bo++; //图片播放完重零开始重新播放 if (bo >= imagelist1.images.count) { bo = 0; } //遍历控件 foreach (control item in gamearea.controls) { //鸟飞 if (item.tag.tostring()=="niao") { item.left += 10; //超出边界重新开始飞 if (item.left>=gamearea.width) { item.left = -item.width; } } //字母下落 if (item.tag.tostring() == "zm"||item.tag.tostring()=="bj") { item.top += 10; //超出下边界清除 if (item.top+item.height>=gamearea.height) { item.dispose(); bar.width -= 10; //判断血条为零时 if (bar.width==0) { fly.stop(); zimu.stop(); //弹框 if ( messagebox.show("游戏结束,积分为:"+fen,"game over", messageboxbuttons.retrycancel, messageboxicon.warning) == dialogresult.retry) { fly.stop(); zimu.stop(); //满血条 bar.width = 100; //按钮变开始 button.text = "开始"; //积分清零 scoring.text = "积分:0"; //鸟回归原位 bird.location = new point(0, 0); //清屏 qingping(); } else { this.close(); } } } } //子弹上升 if (item.tag.tostring() == "bullet") { item.top -= 10; //超出上边界清除 if (item.top==-item.top) { item.dispose(); } //重新遍历 foreach (control letter in gamearea.controls) { //找到字母 if (letter.tag.tostring() == "bj") { //判断字母与子弹相遇 if (item.top <= letter.top + letter.height && item.left + item.width / 2 == letter.left + letter.width / 2) { item.dispose(); letter.dispose(); //积分自加 fen++; //写入控制区 scoring.text = "积分:" + fen; //创造爆炸 picturebox detonate = new picturebox(); //记录播放图片从零开始 detonate.tag = 0; //盒子大小 detonate.size = new size(50,50); //将图片放进盒子 detonate.image = imagelist2.images[0]; //图片自适应 detonate.sizemode = pictureboxsizemode.stretchimage; //爆炸位置 detonate.location = new point(letter.left + letter.width / 2 - detonate.width / 2, letter.top + letter.height / 2 - detonate.height / 2); gamearea.controls.add(detonate); //爆炸timer timer zha = new timer(); //爆炸图片放进timer zha.tag = detonate; zha.interval = 100; zha.tick += zha_tick; zha.start(); } } } } //尾翼动画播放 wei.image = imagelist3.images[t]; t++; if (t >= imagelist3.images.count) { t = 0; } weiyi.image = imagelist3.images[y]; y++; if (y >= imagelist3.images.count) { y = 0; } } } private void zha_tick(object sender, eventargs e) { //获取timer timer zha = (timer)sender; //从盒子获取图片集 picturebox picture = (picturebox)zha.tag; //获取imagelist2中图片 picture.image = imagelist2.images[(int)picture.tag]; //图片播放 picture.tag = (int)picture.tag + 1; //一组爆炸图播完清除 if ((int)picture.tag == 32) { zha.dispose(); picture.dispose(); } } //清屏字母 private void qingping() { foreach (label lazm in zmbox) { lazm.dispose(); } } } }
更多有趣的经典小游戏实现专题,也分享给大家:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。