在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 坦克大战 游戏代码

C# 坦克大战 游戏代码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.05M
  • 下载次数:72
  • 浏览次数:1047
  • 发布时间:2015-06-10
  • 实例类别:C#语言基础
  • 发 布 人:瓜瓜
  • 文件格式:.zip
  • 所需积分:4
 相关标签: 坦克

实例介绍

【实例简介】

【实例截图】

【核心代码】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Text;

namespace Tank
{
    //游戏状态
    public enum GameState
    {
        close = 1, open = 2
    }
    //方向
    public enum Direction
    {
        left = 1, up = 2, right = 3, down = 4
    }
    //坦克类
    public class tank
    {
        //坦克坐标位置
        public Point _position = new Point(300, 300);
        //运动方向
        public Direction _direction = Direction.up;
        //运动步长
        public int _step = 4;
        //坦克运动状态图片
        public Boolean _imgstate = true;


        //坦克位图数组
        Bitmap[] _tankBmp = new Bitmap[8];

        //当前坦克位图
        public Bitmap _nowTankBmp = new Bitmap(30, 30);

        //(方法)构造方法
        public tank()
        {
            //装载所有的坦克位图图片
            _tankBmp[0] = new Bitmap("MyTankUp1.gif");
            _tankBmp[1] = new Bitmap("MyTankUp2.gif");
            _tankBmp[2] = new Bitmap("MyTankDown1.gif");
            _tankBmp[3] = new Bitmap("MyTankDown2.gif");
            _tankBmp[4] = new Bitmap("MyTankLeft1.gif");
            _tankBmp[5] = new Bitmap("MyTankLeft2.gif");
            _tankBmp[6] = new Bitmap("MyTankRight1.gif");
            _tankBmp[7] = new Bitmap("MyTankRight2.gif");

            //设置坦克位图的透明色
            for (int i = 0; i <= 7; i  )
                _tankBmp[i].MakeTransparent(Color.Black);

            //当前坦克位图为向上运动的位图
            _nowTankBmp = _tankBmp[0];
        }
        //坦克运动
        public void Go(Direction direction)
        {
            _direction = direction;
            if (_direction == Direction.up)
            {
                _position.Y = _position.Y - _step;
                if (_imgstate == true)
                    _nowTankBmp = _tankBmp[0];
                else
                    _nowTankBmp = _tankBmp[1];
            }
            else if (_direction == Direction.down)
            {
                _position.Y = _position.Y   _step;
                if (_imgstate == true)
                    _nowTankBmp = _tankBmp[2];
                else
                    _nowTankBmp = _tankBmp[3];
            }
            else if (_direction == Direction.left)
            {
                _position.X = _position.X - _step;
                if (_imgstate == true)
                    _nowTankBmp = _tankBmp[4];
                else
                    _nowTankBmp = _tankBmp[5];
            }
            else if (_direction == Direction.right)
            {
                _position.X = _position.X   _step;
                if (_imgstate == true)
                    _nowTankBmp = _tankBmp[6];
                else
                    _nowTankBmp = _tankBmp[7];
            }
            _imgstate = !_imgstate;
        }
        //(方法)坦克图像绘制
        public void DrawMe(Graphics g)
        {
            g.DrawImage(_nowTankBmp, _position);
        }
    }
    //子弹类
    public class Bullet
    {
        //子弹坐标位置
        public Point _position;
        //子弹运动方向
        public Direction _direction = Direction.up;
        //子弹运动步长
        public int _step = 15;
        //子弹位图数组
        Bitmap[] _BulletBmp = new Bitmap[4];

        //当前子弹位图
        public Bitmap _nowBulletBmp = new Bitmap(16, 16);

        //方法
        public Bullet(Direction direction, Point point)
        {
            _direction = direction;
            _position = point;

            //装载所有的坦克位图图片
            _BulletBmp[0] = new Bitmap("BulletLeft.gif");
            _BulletBmp[1] = new Bitmap("BulletUp.gif");
            _BulletBmp[2] = new Bitmap("BulletRight.gif");
            _BulletBmp[3] = new Bitmap("BulletDown.gif");


            //设置子弹位图的透明色
            for (int i = 0; i <= 3; i  )
                _BulletBmp[i].MakeTransparent(Color.Black);

            if (_direction == Direction.left)
                _nowBulletBmp = _BulletBmp[0];
            else if (_direction == Direction.up)
                _nowBulletBmp = _BulletBmp[1];
            else if (_direction == Direction.right)
                _nowBulletBmp = _BulletBmp[2];
            else if (_direction == Direction.down)
                _nowBulletBmp = _BulletBmp[3];

        }
        //(方法)子弹图像绘制
        public void DrawBullet(Graphics g)
        {
            g.DrawImage(_nowBulletBmp, _position);
        }
        //(方法)子弹运动
        public void Go()
        {
            //设置子弹所在位置
            if (_direction == Direction.up)
                _position.Y = _position.Y - _step;
            else if (_direction == Direction.down)
                _position.Y = _position.Y   _step;
            else if (_direction == Direction.left)
                _position.X = _position.X - _step;
            else if (_direction == Direction.right)
                _position.X = _position.X   _step;
        }
    }
   
}

标签: 坦克

实例下载地址

C# 坦克大战 游戏代码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警