您的位置:首页 > 职场人生

WinForm编写的小游戏坦克大战(源码) 推荐

2008-10-13 10:52 525 查看
前段时间在教课时突发奇想的一个小例子,国庆有时间完善了一下,拿出来和大家分享:)
非常简单的几个控件,实现了一个坦克移动,并打出子弹的小游戏.希望能给大家带来一点乐趣和知识.
注:本程序离真正的游戏差的很远,只用来让初学者对一些控件及线程更加深入的理解和应用才实现的.
所用控件及类:
Button,Label,ImageList,contextMenuStrip,Threading,ArrayList
所用事件:
Button :KeyPress事件.
注意:
在ImagesList控件中添加四张图片,分别是坦克的上下左右的四张图.然后用Button的ImageList属性绑定ImageList控件.
所用的四张图片:









Bullet是通过Lable类new出来的对象
线程调用方法时使用的是带参数调用ParameterizedThreadStart
contextMenuStrip右建菜单用来关闭程序
以下为游戏的源码:




坦克


using System;


using System.Collections.Generic;


using System.Collections;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


using System.Threading;




namespace 坦克大战






{




/**//// <summary>


/// 说明:在.net2.0及WinForm中坦克小游戏的示例


/// 作者:剑了


/// 日期:2008-10-02


/// 首发地址:http://www.cnblogs.com/xy8.cn/


/// </summary>


public partial class Form1 : Form






{


private static int screenWidth;//屏幕宽度


private static int screenHeight;//屏幕高度


private string Direction;//定义当前方向


public Form1()






{


InitializeComponent();


screenHeight = Screen.PrimaryScreen.Bounds.Height;//获取屏幕宽度


screenWidth = Screen.PrimaryScreen.Bounds.Width;//获取屏幕高度


Control.CheckForIllegalCrossThreadCalls = false;//不捕获错误的线程


}




/**//// <summary>


/// 开炮的过程


/// </summary>


/// <param name="paramters"></param>


private void Fire(object paramters)






{


ArrayList al = (ArrayList)paramters;


Label Bullet = (Label)al[0];


string direction = (string)al[1];


bool outOfScreen = false;//子弹是否超出屏幕显示区域


while (!outOfScreen)//当子弹没有超出屏幕显示区域的时候,继续移动子弹






{


switch (direction)






{


case "w":


Bullet.Top = Bullet.Top - 20;


break;


case "s":


Bullet.Top = Bullet.Top + 20;


break;


case "d":


Bullet.Left = Bullet.Left + 20;


break;


case "a":


Bullet.Left = Bullet.Left - 20;


break;




}


//当子弹的顶点大于屏幕高度或者小于0时,子弹超出屏幕显示区域,不应显示


//当子弹的距离屏幕的左边距大于屏幕宽度或者小于0时,子弹超出显示区域,不应显示


if (Bullet.Top < 0 || Bullet.Top > screenHeight || Bullet.Left < 0 || Bullet.Left > screenWidth)






{


outOfScreen=false;


}


Thread.Sleep(60);//暂停当前线程,让CPU做其它事情


}


Bullet.Dispose();


Thread.CurrentThread.Abort();


}






/**//// <summary>


/// button1的健盘接收事件所调用的方法


/// </summary>


/// <param name="sender"></param>


/// <param name="e"></param>


private void button1_KeyPress(object sender, KeyPressEventArgs e)






{


switch (e.KeyChar)






{


case 'w':


Direction = "w";//让方向字段得到俱体方向


button1.Top = button1.Top - 5;//改变坦克的离顶距离


button1.ImageIndex = 3;//改变ImageList源中图片的索引,也就是改变坦克的方向


break;


case 's':


Direction = "s";


button1.ImageIndex = 0;


button1.Top = button1.Top + 5;


break;


case 'd':


Direction = "d";


button1.ImageIndex = 1;


button1.Left = button1.Left + 5;


break;


case 'a':


Direction = "a";


button1.ImageIndex = 2;


button1.Left = button1.Left - 5;


break;


case 'k':


ArrayList paramters = new ArrayList(2);


Label Bullet = new Label();//创建Lable对象Bullet(子弹)


Bullet.BackColor = Color.Red;//Label对象的背景颜色


Bullet.Size = new Size(3, 3);//子弹的大小定义


Bullet.Top = button1.Top + 24;//子弹的离顶距离等于坦克当前的离顶距离


Bullet.Left = button1.Left + 24;//子弹的离左距离等于坦克当前的离左距离


Controls.Add(Bullet);//将控件Label添加到窗体中


paramters.Add(Bullet);//将子弹放入集合中


paramters.Add(Direction);//将当前方向放入集合中


Thread th = new Thread(new ParameterizedThreadStart(Fire));//创建有参数的线程对象


th.Start(paramters);//开始线程并传参.


break;


}




}






private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)






{


Application.Exit();


}








}


}



结果:
分别用w,s,a,d来控制坦克的方向,用k来开炮!
图1



图2



源码:
坦克游戏源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐