C#游戏——极品蜜蜂V1.0

闲着无聊,开发第二个游戏,怀恋小时候在游戏机上玩的那种什么飞机。

游戏界面:



本来是想做成飞机的,无奈没有找到飞机图片,于是用一只蜜蜂代替。

本游戏属于碰撞类游戏,主要用到了aabb类。游戏中的物体都是该类派生的子类。

aabb类描述如下:

       

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

namespace SuperPlane
{   
/*
     * 本类可以实例化为场景中的任何物体,如某个障碍物、飞机的机身、飞机的机翼等
     
*/
    
class aabb
    {
        
public Point location;     //对象坐标
        public Size size;          //对象大小
        public int speed;             //移动速度
        public Color myColor;    //对象的颜色
        public objTypes objType;//形状类型,有三种:“圆”,“矩形”,“椭圆”
        public float centerX, centerY;//对象中心坐标
        public float extentX, extentY;//对象xy半径
       
        
public enum objTypes
        {
            Circle 
= 1,
            Rectangle 
= 2,
            ellipse
=3
        };  
//形状之种类
        public aabb(Point xy, Size wh, objTypes oType, int Speed,Color mcolor)
        {
            location 
= xy;
            size 
= wh;
            centerX 
= xy.X+wh.Width/2;
            centerY 
= xy.Y+wh.Height/2;
            extentX 
= wh.Width/2;
            extentY 
= wh.Height/2;
            objType 
= oType;
            speed 
= Speed;
            myColor 
= mcolor;
        }
        
/*与参数提供的对象进行碰撞检测,碰撞则返回true,否则flase*/
        
public virtual bool hit(aabb BB)
        {
            
/*其实本应用中除了矩形碰撞,其他的形状都没有出现,椭圆的碰撞比较复杂暂时没有做*/
            
if (this.objType == objTypes.Rectangle && BB.objType == objTypes.Rectangle)
                
return RectangleAndRectangle(BB);  
            
else if (this.objType == objTypes.Circle && BB.objType == objTypes.Circle)
                
return CircleAndCircle(BB);
            
else if (this.objType == objTypes.Circle && BB.objType == objTypes.Rectangle)
                
return CircleAndRectangle(BB);
            
else if (this.objType == objTypes.Rectangle && BB.objType == objTypes.Circle)
                
return RectangleAndCircle(BB);
            
else
                
return RectangleAndRectangle(BB);  //默认当成矩形来处理
        }

        
/*圆与圆的碰撞检测*/
        
public bool CircleAndCircle(aabb BB)
        {
            
float sx = Math.Abs(this.centerX - BB.centerX);//两中心之间的横向距离
            float sy = Math.Abs(this.centerY - BB.centerY);
            
if ((this.extentX + BB.extentX) < sx || (this.extentY + BB.extentY) < sy)
                
return false;  //肯定不能碰撞
            else
                
return Math.Sqrt(this.extentX + BB.extentX) > Math.Sqrt(sx) + Math.Sqrt(sy);
        }

        
/*圆与矩形*/
        
public bool CircleAndRectangle(aabb BB)
        {
            
float sx = Math.Abs(this.centerX - BB.centerX);//两中心之间的横向距离
            float sy = Math.Abs(this.centerY - BB.centerY);

            
if ((this.extentX + BB.extentX) < sx || (this.extentY + BB.extentY) < sy)
                
return false;  //肯定不能碰撞
            else
            {
                
float dx = Math.Abs(Math.Abs(this.centerX - BB.centerX) - BB.extentX);//圆之中心与矩形最近一角的距离
                float dy = Math.Abs(Math.Abs(this.centerY - BB.centerY) - BB.extentY);
                
if (this.extentX > (dx + dy))
                    
return true;
                
else
                    
return Math.Sqrt(this.extentX) > (Math.Sqrt(dx) + Math.Sqrt(dy));
            }
        }

        
/*矩形与圆*/
        
public bool RectangleAndCircle(aabb BB)
        {
            
float sx = Math.Abs(this.centerX - BB.centerX);//两中心之间的横向距离
            float sy = Math.Abs(this.centerY - BB.centerY);

            
if ((this.extentX + BB.extentX) < sx || (this.extentY + BB.extentY) < sy)
                
return false;  //肯定不能碰撞
            else
            {
                
float dx = Math.Abs(Math.Abs(this.centerX - BB.centerX) - this.extentX);//圆之中心与矩形最近一角的距离
                float dy = Math.Abs(Math.Abs(this.centerY - BB.centerY) - this.extentY);
                
if (BB.extentX > (dx + dy))
                    
return true;
                
else
                    
return Math.Sqrt(BB.extentX) > (Math.Sqrt(dx) + Math.Sqrt(dy));
            }
        }

        
/*矩形与矩形*/
        
public bool RectangleAndRectangle(aabb BB)
        {
            
float sx = Math.Abs(this.centerX - BB.centerX);//两中心之间的横向距离
            float sy = Math.Abs(this.centerY - BB.centerY);
            
if ((this.extentX + BB.extentX) < sx || (this.extentY + BB.extentY) < sy)
                
return false;  //肯定不能碰撞
            else
                
return true;
        }
        
/*其实本应用中除了矩形碰撞,其他的形状都没有出现*/

        
/*移动*/
        
public void up()
        {
            Erase(GameField.winHandle);
            
this.location.Y -= speed;
            
this.centerY -= speed;
            Draw(GameField.winHandle);
        }
        
public void down()
        {
            Erase(GameField.winHandle);
            
this.location.Y += speed;
            
this.centerY += speed;
            Draw(GameField.winHandle);
        }
        
public void down(int newSpeed) //重载down方法
        {
            Erase(GameField.winHandle);
            
this.location.Y += newSpeed;
            
this.centerY += newSpeed;
            Draw(GameField.winHandle);
        }
        
public void left()
        {
            Erase(GameField.winHandle);
            
this.location.X -= speed;
            
this.centerX -= speed;
            Draw(GameField.winHandle);
        }
        
public void right()
        {
            Erase(GameField.winHandle);
            
this.location.X += speed;
            
this.centerX += speed;
            Draw(GameField.winHandle);
        }
        
/*画对象*/
        
public virtual void Draw(System.IntPtr winHandle)
        {
            Graphics g 
= Graphics.FromHwnd(winHandle);
            
//根据需要 画出不同的图形
            Rectangle r = new Rectangle(location, size);
            
if (this.objType == objTypes.Rectangle)
                g.FillRectangle(
new SolidBrush(this.myColor), r);
            
else  
                g.FillEllipse(
new SolidBrush(this.myColor), r);
        }
        
/*擦除对象*/
        
public virtual void Erase(System.IntPtr winHandle)
        {
            Graphics g 
= Graphics.FromHwnd(winHandle);
            Rectangle r
=new Rectangle(location, size);
            
if (this.objType == objTypes.Rectangle)
                g.FillRectangle(
new SolidBrush(GameField.BackColor), r);
            
else
                g.FillEllipse(
new SolidBrush(GameField.BackColor), r);
        }
    }
}


由于键盘操作实在没有小型游戏机快,于是没有进一步做更复杂的功能。
不过本游戏中亦有第一个游戏所没做到的功能。
如捕获方向键,项目资源的使用方法等。读者慢慢体会吧~~

源码及演示版下载:C#游戏——极品蜜蜂V1.0

http://www.cnblogs.com/tuyile006/archive/2007/06/01/767303.html

加支付宝好友偷能量挖...


评论(0)网络
阅读(78)喜欢(0)游戏开发