基于C#弹幕类射击游戏的实现——(一)概述

本文介绍如何使用C#实现一个弹幕类射击游戏的辅助库,包括自定义Vector类、辅助数学计算及碰撞检测的Rectangle类,为游戏逻辑打下基础。
摘要由CSDN通过智能技术生成

好几个月没写博客了~~变懒了

 

提到弹幕类游戏,想到的最多的应该是《东方Project》系列了吧

酷炫的弹幕~~酷炫的弹幕~~我只记得酷炫。。。。

 

今天我们也来实现一个仿制版,阉割版~

 

关于弹幕类游戏,有篇文章写的不错。我也就不重复写了

今天首先来实现以下辅助库,因为这些作为游戏的基石,还是很重要的

 

由于C#没有原生的Vector类(XNA有,但这里不打算用XNA)所以我们自己实现一个

 

public struct Vector2
    {
        public static readonly Vector2 Zero = new Vector2(0, 0);

        public float X;
        public float Y;

        public Vector2(float x, float y)
        {
            this.X = x;
            this.Y = y;
        }

        public Vector2(Vector2 v)
        {
            this.X = v.X;
            this.Y = v.Y;
        }

        public override string ToString()
        {
            return "(" + X.ToString() + "," + Y.ToString() + ")";
        }

        public override bool Equals(object obj)
        {
            if (object.Equals(obj, null))
            {
                return false;
            }

            Vector2 v = (Vector2)obj;

            return this.X == v.X && this.Y == v.Y;
        }

        public override int GetHashCode()
        {
            return X.GetHashCode() + Y.GetHashCode();
        }

        public void Normalize()
        {
            float m = this.X * this.X + this.Y * this.Y;

            if (m < 0.00001f)
            {
                m = 1;
            }

            m = 1.0f / (float)Math.Sqrt((double)m);

            X = X * m;
            Y = Y * m;
        }

        public void Reverse()
        {
            this.X = -this.X;
            this.Y = -this.Y;
        }

        public float Length()
        {
            return (float)Math.Sqrt(X * X + Y * Y);
        }
        
        public float Angle()
        {
        	return (float)Math.Atan2(Y, X);
        }
        
        public float Angle(Vector2 vec)
        {
    		Vector2 s = this;
    		s.Normalize();
    		vec.Normalize();
    		return (float)Math.Acos(Vector2.Dot(s, vec));
        }
        
        public void Rotate(float angle)
        {
        	float x = X;
        	float y = Y;
        	
        	x = X * (float)Math.Cos(angle) - Y * (float)Math.Sin(angle);
        	y = X * (float)Math.Sin(angle) + Y * (float)Math.Cos(angle);
        	
        
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值