服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#实现坦克大战游戏

C#实现坦克大战游戏

2022-09-22 15:39无名风沙 C#

这篇文章主要为大家详细介绍了C#实现坦克大战游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

前言

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

界面效果图

C#实现坦克大战游戏

图1

部分代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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();
   }
  }
 }
}

下载:完整文件

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

原文链接:https://blog.csdn.net/weixin_43624740/article/details/107130436

延伸 · 阅读

精彩推荐
  • C#C#视频转换类分享

    C#视频转换类分享

    这篇文章主要为大家分享了C#视频转换类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    孤者自清7272022-01-11
  • C#C#完成word文档打印的方法

    C#完成word文档打印的方法

    在日常工作中,我们可能常常需要打印各种文件资料,比如word文档。对于编程员,应用程序中文档的打印是一项非常重要的功能,也一直是一个非常复杂的...

    Yesi11082021-12-09
  • C#WPF 自定义雷达图开发实例教程

    WPF 自定义雷达图开发实例教程

    这篇文章主要介绍了WPF 自定义雷达图开发实例教程,本文介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下...

    WinterFish11522021-12-06
  • C#关于C#反射 你需要知道的

    关于C#反射 你需要知道的

    这篇文章主要介绍了C#反射的相关知识,文中讲解的非常详细,代码帮助大家更好的参考学习,感兴趣的朋友可以了解下...

    Liam Wang8232022-09-09
  • C#深入解析C#中的泛型类与泛型接口

    深入解析C#中的泛型类与泛型接口

    这篇文章主要介绍了C#中的泛型类与泛型接口,对泛型的支持是C#语言的重要特性,需要的朋友可以参考下...

    C#教程网3872021-11-11
  • C#c#将字节数组转成易读的字符串的实现

    c#将字节数组转成易读的字符串的实现

    这篇文章主要介绍了c#将字节数组转成易读的字符串的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    森大科技4912022-08-17
  • C#C#读写INI文件的方法

    C#读写INI文件的方法

    这篇文章主要介绍了C#读写INI文件的方法,涉及C#读写ini文件的相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Nozer3942021-10-28
  • C#C# 字符串、数组和List的截取和转换实例

    C# 字符串、数组和List的截取和转换实例

    下面小编就为大家分享一篇C# 字符串、数组和List的截取和转换实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    杜子烟8962022-02-10