非常簡單的幾個控件,實現了一個坦克移動,並打出子彈的小游戲.希望能給大家帶來一點樂趣和知識.
注:本程序離真正的游戲差的很遠,只用來讓初學者對一些控件及線程更加深入的理解和應用才實現的.
所用控件及類:
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
