c#
c# - 作业5:坦克大战(未完成)
- 总体视图 & 废话集结区
- 代码(全)
- FormMain.cs
- Program.cs
- Bullet.cs
- Tank.cs
- 现存bug
总体视图 & 废话集结区
为什么没写完呢,给自己解释一下,不是因为那啥懒惰也不是因为没时间(某种程度上也算是的吧。。。原因1,没有课件,原因2,我没玩过(我有童年只是童年不是这个。所以导致我就完全不知道怎么做下去,没有规则也没有印象,我自己肆意发展吗,我选择自己去做大作业,我爱俄罗斯方块
所以,我就做到老师有课件的部分(以及比课时多了,印象里是这样的,或者做的比老师上课进程快一些?就。老师上课讲坦克大战的时候我在做大作业
代码(全)
FormMain.cs
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 FormMian : Form{#region 私有字段//游戏状态private GameState _gameState = GameState.Close;//我方坦克private Tank _myTank = new Tank(Side.Me);//敌方坦克List集合private List<Tank> _listEnemyTank = new List<Tank>();//子弹的List集合private List<Bullet> _listBullet = new List<Bullet>();#endregion//导入动态链接库(System.Runtime.InteropServices)[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]public static extern short GetAsyncKeyState(int keyCode);public FormMian(){InitializeComponent();}private void BeginToolStripMenuItem_Click(object sender, EventArgs e){//开始游戏_gameState = GameState.Open;timer1.Enabled = true;timer2.Enabled = true;timer3.Enabled = true;timer4.Enabled = true;}private void EndToolStripMenuItem_Click(object sender, EventArgs e){//结束游戏_gameState = GameState.Close;timer1.Enabled = false;timer2.Enabled = false;timer3.Enabled = false;timer4.Enabled = false;}private void pictureBox1_Paint(object sender, PaintEventArgs e){//绘制我方坦克_myTank.DrawMe(e.Graphics);//绘制敌方坦克for (int i = 0;i <= _listEnemyTank.Count - 1 ;i++)_listEnemyTank[i].DrawMe(e.Graphics);//逐一绘制子弹foreach (Bullet myBullet in _listBullet){myBullet.DrawMe(e.Graphics);}}private void FormMian_KeyDown(object sender, KeyEventArgs e){if(_gameState == GameState.Open){//如果按下Space键,则我方坦克发射子弹if (e.KeyCode == Keys.Space){//我方坦克发射子弹,并添加到_listBullet中Bullet myBullet = _myTank.Fire();_listBullet.Add(myBullet);}//强制刷新pictureBox1控件pictureBox1.Invalidate();}}private void timer1_Tick(object sender, EventArgs e){if (_gameState == GameState.Open){//产生一辆坦克Tank enemyTank = new Tank(Side.Enemy);//把坦克加到链表中_listEnemyTank.Add(enemyTank);//强制刷新pictureBox1.Invalidate();}}private void timer2_Tick(object sender, EventArgs e){if (_gameState == GameState.Open){//定义一个随机类Random myRand = new Random(DateTime.Now.Second);//随机控制每一辆敌方坦克的运动方向for (int i = 0; i <= _listEnemyTank.Count - 1; i++){int newDircetion = myRand.Next(1, 20);if(newDircetion <= 4)_listEnemyTank[i].Move((Direction)newDircetion);else _listEnemyTank[i].Move(_listEnemyTank[i]._Dircetion);}//让子弹飞起来foreach (Bullet myBullet in _listBullet){myBullet.Move();}//强制刷新pictureBox1.Invalidate();}}//Timer实现敌方坦克发射子弹功能private void timer3_Tick(object sender, EventArgs e){if (_gameState == GameState.Open){//定义一个随机类Random myRand = new Random(DateTime.Now.Second);//让子弹飞起来foreach (Tank enemyTank in _listEnemyTank){int fireFlag = myRand.Next(1, 10);if (fireFlag <= 4){Bullet enemyBullet = enemyTank.Fire();_listBullet.Add(enemyBullet);}}//强制刷新pictureBox1.Invalidate();}}//定时测定按下方向键下压状态,解决我方坦克边移动边发射子弹的问题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);pictureBox1.Invalidate();}}}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace MyTankWar
{static class Program{/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new FormMian());}}
}
Bullet.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;namespace MyTankWar
{class Bullet{#region//子弹坐标位置private Point _position = new Point(200, 200);//子弹发射方向private Direction _dircetion = Direction.Up;//子弹运动步长private int _step = 10;//敌我子弹标志Side _side;//子弹位图private Bitmap _bulletBmp = new Bitmap(16, 16);#endregion#regionpublic Point _Position{get { return _position; }set { _position = value; }}public Direction _Dircetion{get { return _dircetion; }set { _dircetion = value; }}public int _Step{get { return _step; }set { _step = value; }}public Side _Side{get { return _side; }set { _side = value; }}#endregion//构造方法public Bullet(Side side, Direction direction){//保存敌我方标志_side = side;//保存子弹发射方向_dircetion = direction;//根据敌我方和子弹发射方向,装载相应的子弹位图if (side == Side.Me){if (direction == Direction.Up)_bulletBmp = new Bitmap("Images\\MyBulletUp.gif");else if (_dircetion == Direction.Down)_bulletBmp = new Bitmap("Images\\MyBulletDown.gif");else if (_dircetion == Direction.Left)_bulletBmp = new Bitmap("Images\\MyBulletLeft.gif");else if (_dircetion == Direction.Right)_bulletBmp = new Bitmap("Images\\MyBulletRight.gif");}else{if (direction == Direction.Up)_bulletBmp = new Bitmap("Images\\EnemyBulletUp.gif");else if (_dircetion == Direction.Down)_bulletBmp = new Bitmap("Images\\EnemyBulletDown.gif");else if (_dircetion == Direction.Left)_bulletBmp = new Bitmap("Images\\EnemyBulletLeft.gif");else if (_dircetion == Direction.Right)_bulletBmp = new Bitmap("Images\\EnemyBulletRight.gif");}//设置坦克位图的透明色_bulletBmp.MakeTransparent(Color.Black);}//子弹移动public void Move(){//设置子弹所在位置if (_dircetion == Direction.Up){_position.Y = _position.Y - _step;}else if(_dircetion == Direction.Down){_position.Y = _position.Y + _step;}else if (_dircetion == Direction.Left){_position.X = _position.X - _step;}else if (_dircetion == Direction.Right){_position.X = _position.X + _step;}}//子弹绘制public void DrawMe(Graphics g){g.DrawImage(_bulletBmp, _position);}}
}
Tank.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;namespace MyTankWar
{//枚举游戏状态public enum GameState{Close = 1,Open = 2,Pause = 3}//枚举坦克运动方向public enum Direction{Up = 1,Down = 2,Left = 3, Right = 4}//枚举敌我双方public enum Side{Me = 1, Enemy = 2}class Tank{#region 私有属性//坦克坐标位置private Point _position = new Point(200, 200);//坦克运动方向private Direction _dircetion = Direction.Up;//坦克运动步长private int _step = 5;//坦克尺寸大小private int _size = 30;//敌我标志Side _side;//坦克位图数组private Bitmap[] _tankBmp = new Bitmap[8];//当前坦克位图private Bitmap _nowTankBmp = new Bitmap(30, 30);//坦克位图轮换标志private Boolean _tankBmpChange = true;#endregion#region 公有属性public Point _Position{get { return _position; }set { _position = value; }}public Direction _Dircetion{get { return _dircetion; }set { _dircetion = value; }}public int _Step{get { return _step; }set { _step = value; }}public int _Size{get { return _size; }set { _size = value; }}public Side _Side{get { return _side; }set { _side = value; }}#endregion//类构造方法public Tank(Side side){//保存敌我方标志_side = side;if (side == Side.Me){//装载坦克位图_tankBmp[0] = new Bitmap("Images\\MyTankUp1.gif");_tankBmp[1] = new Bitmap("Images\\MyTankUp2.gif");_tankBmp[2] = new Bitmap("Images\\MyTankDown1.gif");_tankBmp[3] = new Bitmap("Images\\MyTankDown2.gif");_tankBmp[4] = new Bitmap("Images\\MyTankLeft1.gif");_tankBmp[5] = new Bitmap("Images\\MyTankLeft2.gif");_tankBmp[6] = new Bitmap("Images\\MyTankRight1.gif");_tankBmp[7] = new Bitmap("Images\\MyTankRight2.gif");//让我方坦克在屏幕正下方中央生成_position.X = Screen.PrimaryScreen.Bounds.Width / 2 - _size / 2;_position.Y = Screen.PrimaryScreen.Bounds.Height - 150;//设置我方坦克默认的运动方向为上_dircetion = Direction.Up;}else{//装载坦克位图_tankBmp[0] = new Bitmap("Images\\EnemyTankUp1.gif");_tankBmp[1] = new Bitmap("Images\\EnemyTankUp2.gif");_tankBmp[2] = new Bitmap("Images\\EnemyTankDown1.gif");_tankBmp[3] = new Bitmap("Images\\EnemyTankDown2.gif");_tankBmp[4] = new Bitmap("Images\\EnemyTankLeft1.gif");_tankBmp[5] = new Bitmap("Images\\EnemyTankLeft2.gif");_tankBmp[6] = new Bitmap("Images\\EnemyTankRight1.gif");_tankBmp[7] = new Bitmap("Images\\EnemyTankRight2.gif");//让敌方坦克在屏幕正下方中央生成_position.X = Screen.PrimaryScreen.Bounds.Width / 2 - _size / 2;_position.Y = _size;//设置敌方坦克默认的运动方向为上_dircetion = Direction.Down;}//设置坦克的透明度for (int i = 0; i <= 7; i++)_tankBmp[i].MakeTransparent(Color.Black);//当前坦克位图为向上运动的位图_nowTankBmp = _tankBmp[0];}//坦克移动public void Move(Direction direction){//保存运动方向_dircetion = direction;if (_dircetion == Direction.Up){//设定坦克移动后所在的位置_position.Y = _position.Y - _step;//设置当前显示的坦克位图(为了让坦克移动效果更逼真,需要在两幅位图之间进行切换)if (_tankBmpChange == true)_nowTankBmp = _tankBmp[0];else_nowTankBmp = _tankBmp[1];}else if (_dircetion == Direction.Down){//设定坦克移动后所在的位置_position.Y = _position.Y + _step;//设置当前显示的坦克位图(为了让坦克移动效果更逼真,需要在两幅位图之间进行切换)if (_tankBmpChange == true)_nowTankBmp = _tankBmp[2];else_nowTankBmp = _tankBmp[3];}else if (_dircetion == Direction.Left){//设定坦克移动后所在的位置_position.X = _position.X - _step;//设置当前显示的坦克位图(为了让坦克移动效果更逼真,需要在两幅位图之间进行切换)if (_tankBmpChange == true)_nowTankBmp = _tankBmp[4];else_nowTankBmp = _tankBmp[5];}else if (_dircetion == Direction.Right){//设定坦克移动后所在的位置_position.X = _position.X + _step;//设置当前显示的坦克位图(为了让坦克移动效果更逼真,需要在两幅位图之间进行切换)if (_tankBmpChange == true)_nowTankBmp = _tankBmp[6];else_nowTankBmp = _tankBmp[7];}//切换坦克为位图轮换标志_tankBmpChange = !_tankBmpChange;}//坦克绘制public void DrawMe(Graphics g){g.DrawImage(_nowTankBmp, _position);}//坦克发射子弹public Bullet Fire(){Bullet myBullet = new Bullet(_side, _dircetion);if (_dircetion == Direction.Up){myBullet._Position = new Point(_position.X + 8,_position.Y - 15);}else if (_dircetion == Direction.Down){myBullet._Position = new Point(_position.X + 8, _position.Y + 25);}else if (_dircetion == Direction.Left){myBullet._Position = new Point(_position.X - 15, _position.Y + 8);}else if (_dircetion == Direction.Right){myBullet._Position = new Point(_position.X + 25, _position.Y + 8);}return myBullet;}}
}
现存bug
这个应该很多吧,比如子弹不会真正意义上的打到坦克之类的毕竟是未完成产品
发布评论