欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)坦克大戰(zhàn)游戲

 更新時(shí)間:2020年07月08日 11:05:36   作者:無(wú)名風(fēng)沙  
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)坦克大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#實(shí)現(xiàn)坦克大戰(zhàn)游戲的具體代碼,供大家參考,具體內(nèi)容如下

前言

該程序主要對(duì)原始的坦克大戰(zhàn)游戲進(jìn)行了簡(jiǎn)單的還原。目前程序可以做到自動(dòng)生成敵方坦克且敵方能夠隨機(jī)發(fā)射子彈,我方坦克也能做到邊發(fā)射子彈邊移動(dòng)。唯一的不足之處就是還沒(méi)有完整通關(guān)的設(shè)置以及障礙的設(shè)置。

界面效果圖

圖1

部分代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace MyTankWar
{
 public partial class FormMain : Form
 {
  //game state
  private GameState _gameState = GameState.Close;
  //my tank
  private Tank _myTank = new Tank(Side.Me);
  //list set of enemy's tank
  private List<Tank> _listEnemyTank = new List<Tank>();
  //list set of bullets
  private List<Bullet> _listBullet = new List<Bullet>();

  // import dynamic link library
  [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
  public static extern short GetAsyncKeyState(int keyCode);

  public FormMain()
  {
   InitializeComponent();
  }

  //'begin game' button function
  private void BeginToolStripMenuItem_Click(object sender, EventArgs e)
  {
   _gameState = GameState.Open;

   //enable timer
   timer1.Enabled = true;
   timer2.Enabled = true;
   timer3.Enabled = true;
   timer4.Enabled = true;
  }

  //'end game' button function
  private void EndToolStripMenuItem_Click(object sender, EventArgs e)
  {
   _gameState = GameState.Close;

   //unenable timer
   timer1.Enabled = false;
   timer2.Enabled = false;
   timer3.Enabled = false;
   timer4.Enabled = false;
  }

  //'pictureBox1'-> Paint function
  private void pictureBox1_Paint(object sender, PaintEventArgs e)
  {
   //draw my tanks
   _myTank.DrawMe(e.Graphics);
   //draw enemy's tanks
   for (int i = 0; i <= _listEnemyTank.Count - 1; i++)
    _listEnemyTank[i].DrawMe(e.Graphics);

   //draw each bullet
   foreach (Bullet myBullet in _listBullet)
   {
    myBullet.DrawMe(e.Graphics);
   }
  }

  //'FormMain'-> KeyDown function
  private void FormMain_KeyDown(object sender, KeyEventArgs e)
  {
   //if game started
   if (_gameState == GameState.Open)
   {
    //if press 'Space' key, my tank start shooting
    if (e.KeyCode == Keys.Space)
    {
     Bullet myBullet = _myTank.Fire();
     _listBullet.Add(myBullet);
    }

    //refresh interface
    pictureBox1.Invalidate();
   }
  }

  //timer1' Tick function, used for generating a enemy's tank
  private void timer1_Tick(object sender, EventArgs e)
  {
   //if game started
   if (_gameState == GameState.Open)
   { 
    //generate a enemy's tank
    Tank enemyTank = new Tank(Side.Enemy);

    //add enenmy's tank
    _listEnemyTank.Add(enemyTank);

    //refresh pictureBox1
    pictureBox1.Invalidate();
   }
  }

  //timer2' Tick function, used for controlling the direction of a enemy's tank
  private void timer2_Tick(object sender, EventArgs e)
  {
   // if game started
   if (_gameState == GameState.Open)
   { 
    //define a random object
    Random myRand = new Random(DateTime.Now.Second);

    for (int i = 0; i <= _listEnemyTank.Count - 1; i++)
    { 
     //generate a random number
     int newDirection = myRand.Next(1, 10);

     //move the tank
     if (newDirection <= 4)
      _listEnemyTank[i].Move((Direction)newDirection);
     else
      _listEnemyTank[i].Move(_listEnemyTank[i]._Direction);
    }

    //make bullet move
    foreach (Bullet Bullet in _listBullet)
     Bullet.Move();

    //refresh pictureBox1
    pictureBox1.Invalidate();
   }
  }

  //timer3' Tick function, used for shooting of a enemy's tank
  private void timer3_Tick(object sender, EventArgs e)
  {
   // if game started
   if (_gameState == GameState.Open)
   { 
    //define a random class
    Random myRand = new Random(DateTime.Now.Second);
    //make bullet move
    foreach (Tank enemyTank in _listEnemyTank)
    { 
     //generate a random number whose value is between 1 to 4
     int fireFlag = myRand.Next(1, 10);
     //if enemy's tank can shoot
     if (fireFlag <= 4)
     {
      Bullet enemyBullet = enemyTank.Fire();
      _listBullet.Add(enemyBullet);
     }
    }
   }
  }

  //timer4' Tick function, used for spying the key pressing state to make my tank move while shooting
  private void timer4_Tick(object sender, EventArgs e)
  {
   if (_gameState == GameState.Open)
   {
    bool keyDown = (((ushort)GetAsyncKeyState((int)Keys.Down)) & 0xffff) != 0;
    if (keyDown == true)
     _myTank.Move(Direction.Down);
    bool keyUp = (((ushort)GetAsyncKeyState((int)Keys.Up)) & 0xffff) != 0;
    if (keyUp == true)
     _myTank.Move(Direction.Up);
    bool keyLeft = (((ushort)GetAsyncKeyState((int)Keys.Left)) & 0xffff) != 0;
    if (keyLeft == true)
     _myTank.Move(Direction.Left);
    bool keyRight = (((ushort)GetAsyncKeyState((int)Keys.Right)) & 0xffff) != 0;
    if (keyRight == true)
     _myTank.Move(Direction.Right);

    //refresh pictureBox1
    pictureBox1.Invalidate();
   }
  }
 }
}

下載:完整文件

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何在C#中調(diào)用COM組件

    如何在C#中調(diào)用COM組件

    這篇文章主要介紹了如何在C#中調(diào)用COM組件,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#調(diào)用SQLite的方法實(shí)例分析

    C#調(diào)用SQLite的方法實(shí)例分析

    這篇文章主要介紹了C#調(diào)用SQLite的方法,較為詳細(xì)的介紹了SQLite的功能與特點(diǎn),并實(shí)例分析了C#調(diào)用SQLite的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • 詳解C# 中的正則表達(dá)式運(yùn)用

    詳解C# 中的正則表達(dá)式運(yùn)用

    這篇文章主要介紹了C# 中的正則表達(dá)式運(yùn)用的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • C#讀取word中表格數(shù)據(jù)的方法實(shí)現(xiàn)

    C#讀取word中表格數(shù)據(jù)的方法實(shí)現(xiàn)

    本文主要介紹了C#讀取word中表格數(shù)據(jù)的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • c#中使用BackgroundWorker的實(shí)現(xiàn)

    c#中使用BackgroundWorker的實(shí)現(xiàn)

    本文主要介紹了c#中使用BackgroundWorker的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • C#中try...catch的使用與常見(jiàn)面試題分享

    C#中try...catch的使用與常見(jiàn)面試題分享

    這篇文章首先給大家介紹了關(guān)于C#中try...catch的語(yǔ)法,而后又給大家分享了關(guān)于C#中try...catch最常見(jiàn)的面試題,具有一定的參考借鑒價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-02-02
  • C#實(shí)現(xiàn)Access通用訪問(wèn)類OleDbHelper完整實(shí)例

    C#實(shí)現(xiàn)Access通用訪問(wèn)類OleDbHelper完整實(shí)例

    這篇文章主要介紹了C#實(shí)現(xiàn)Access通用訪問(wèn)類OleDbHelper,結(jié)合完整實(shí)例形式分析了C#針對(duì)access數(shù)據(jù)庫(kù)的連接、查詢、遍歷、分頁(yè)顯示等相關(guān)操作技巧,需要的朋友可以參考下
    2017-02-02
  • 最新評(píng)論