前段时间在教课时突发奇想的一个小例子,国庆有时间完善了一下,拿出来和大家分享:)
非常简单的几个控件,实现了一个坦克移动,并打出×××的小游戏.希望能给大家带来一点乐趣和知识.
注:本程序离真正的游戏差的很远,只用来让初学者对一些控件及线程更加深入的理解和应用才实现的.
所用控件及类:
Button,Label,ImageList,contextMenuStrip,Threading,ArrayList
所用事件:
Button :KeyPress事件.
注意:
在ImagesList控件中添加四张图片,分别是坦克的上下左右的四张图.然后用Button的ImageList属性绑定ImageList控件. 
所用的四张图片:
 
Bullet是通过Lable类new出来的对象
线程调用方法时使用的是带参数调用ParameterizedThreadStart
contextMenuStrip右建菜单用来关闭程序
以下为游戏的源码:
 
ContractedBlock.gif ExpandedBlockStart.gif 坦克
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 坦克大战
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary> 
    
/// 说明:在.net2.0及WinForm中坦克小游戏的示例 
    
/// 作者:剑了 
    
/// 日期:2008-10-02 
    
/// 首发地址:[url]http://www.cnblogs.com/xy8.cn/[/url]
    
/// </summary> 

    public partial class Form1 : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private static int screenWidth;//屏幕宽度
        private static int screenHeight;//屏幕高度
        private string Direction;//定义当前方向
        public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
            screenHeight 
= Screen.PrimaryScreen.Bounds.Height;//获取屏幕宽度
            screenWidth = Screen.PrimaryScreen.Bounds.Width;//获取屏幕高度
            Control.CheckForIllegalCrossThreadCalls = false;//不捕获错误的线程
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 开炮的过程
        
/// </summary>
        
/// <param name="paramters"></param>

        private void Fire(object paramters) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ArrayList al 
= (ArrayList)paramters;
            Label Bullet 
= (Label)al[0];
            
string direction = (string)al[1];
            
bool outOfScreen = false;//×××是否超出屏幕显示区域
            while (!outOfScreen)//当×××没有超出屏幕显示区域的时候,继续移动×××
ExpandedSubBlockStart.gifContractedSubBlock.gif
            {
                
switch (direction)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
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)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    outOfScreen
=false;
                }

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

            Bullet.Dispose();
            Thread.CurrentThread.Abort();
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// button1的健盘接收事件所调用的方法
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void button1_KeyPress(object sender, KeyPressEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
switch (e.KeyChar)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
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(33);//×××的大小定义
                    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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Application.Exit();
        }


   

    }

}

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