C#实现坦克大战游戏

 更新时间:2020年07月08日 11:05:36   作者:无名风沙  
这篇文章主要为大家详细介绍了C#实现坦克大战游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#实现坦克大战游戏的具体代码,供大家参考,具体内容如下

前言

该程序主要对原始的坦克大战游戏进行了简单的还原。目前程序可以做到自动生成敌方坦克且敌方能够随机发射子弹,我方坦克也能做到边发射子弹边移动。唯一的不足之处就是还没有完整通关的设置以及障碍的设置。

界面效果图

图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();
   }
  }
 }
}

下载:完整文件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析

    C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析

    这篇文章主要介绍了C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析,以实例形式较为详细的讲述了.NET Framework里面提供的三种Timer具体用法,需要的朋友可以参考下
    2014-10-10
  • C# webservice接口编写、发布与测试

    C# webservice接口编写、发布与测试

    这篇文章主要介绍了C# webservice接口编写、发布与测试,文章通过图文结合的方式给大家介绍的非常详细,对大家的学习或共组有一定的帮助,需要的朋友可以参考下
    2024-07-07
  • C#初始化数组的方法小结

    C#初始化数组的方法小结

    这篇文章主要介绍了C#初始化数组的方法,总结分析了C#声明与初始化一维数组及多维数组的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • C#的锯齿数组以及C++实现代码

    C#的锯齿数组以及C++实现代码

    锯齿数组首先是二维数组,第一维的维数是确定的。之所以在C#中能够出现灵活的锯齿数组,是因为,C#的数组是引用类型(本质上存放的是指针)。根据这个引用类型(指针)的概念,C++中用指针数组同样可以实现
    2013-09-09
  • C# 实现颜色渐变窗体控件详细讲解

    C# 实现颜色渐变窗体控件详细讲解

    这篇文章主要介绍了C# 实现颜色渐变窗体控件详细讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • 基于Unity3D实现3D迷宫小游戏的示例代码

    基于Unity3D实现3D迷宫小游戏的示例代码

    迷宫游戏作为经典的小游戏,一直深受大家的喜爱。本文小编将为大家详细介绍一下如何用Unity实现一个3D版的迷宫小游戏,感兴趣的可以动手试一试
    2022-03-03
  • xml 中的冒号 读取问题的解决

    xml 中的冒号 读取问题的解决

    c#读取xml时因为冒号问题的解决方法
    2008-03-03
  • C#/VB.NET实现在Word文档中添加页眉和页脚

    C#/VB.NET实现在Word文档中添加页眉和页脚

    页眉位于文档中每个页面的顶部区域,常用于显示文档的附加信息;页脚位于文档中每个页面的底部的区域,常用于显示文档的附加信息。今天这篇文章就将为大家展示如何以编程的方式在在 Word 文档中添加页眉和页脚
    2023-03-03
  • C#调用SQLite的方法实例分析

    C#调用SQLite的方法实例分析

    这篇文章主要介绍了C#调用SQLite的方法,较为详细的介绍了SQLite的功能与特点,并实例分析了C#调用SQLite的相关技巧,需要的朋友可以参考下
    2015-06-06
  • c#在控制台输出彩色文字的方法

    c#在控制台输出彩色文字的方法

    c#在控制台输出彩色文字的方法,需要的朋友可以参考一下
    2013-03-03

最新评论