Пример #1
0
    public void move()
    {
        this.velocity -= this.gravity;
        this.velocity *= 0.4f;
        this.y        += this.velocity;

        // Keep it stuck to top or bottom
        float height = -ScreenUtils.GetWorldScreenSize().y / 2f;

        if (this.y < height)
        {
            this.y        = height;
            this.velocity = 0f;
        }
        if (this.y > 5f)
        {
            this.y        = 5f;
            this.velocity = 0f;
        }

        Vector3 pos = gameObject.transform.localPosition;

        pos.y = this.y;
        gameObject.transform.localPosition = pos;

        // Every frame it is alive increases the score
        this.score++;
    }
Пример #2
0
 public bool OffScreen()
 {
     if (this.x < -this.w - (ScreenUtils.GetWorldScreenSize().x / 2f))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #3
0
    // Use this for initialization
    void Start()
    {
        float diffY = ScreenUtils.GetWorldScreenSize().y / 2f;

        // How big is the empty space
        float spacing = Random.Range(3f, ScreenUtils.GetWorldScreenSize().y / 2f);

        // Where is the center of the empty space
        float centery = Random.Range(spacing, ScreenUtils.GetWorldScreenSize().y - spacing);

        this.top    = centery - spacing / 2;
        this.bottom = ScreenUtils.GetWorldScreenSize().y - (centery + spacing / 2);

        this.x = ScreenUtils.GetWorldScreenSize().x / 2f;

        this.w     = 1f;
        this.speed = 0.16f;

        Vector3 topPos = topPipe.transform.localPosition;
        Vector3 btmPos = btmPipe.transform.localPosition;
        Vector3 topSc  = topPipe.transform.localScale;
        Vector3 btmSc  = btmPipe.transform.localScale;

        topPos.y = diffY - (this.top / 2f);
        topSc.y  = this.top;

        btmPos.y = -diffY + (this.bottom / 2f);
        btmSc.y  = this.bottom;

        topPipe.transform.localPosition = topPos;
        topPipe.transform.localScale    = topSc;

        btmPipe.transform.localPosition = btmPos;
        btmPipe.transform.localScale    = btmSc;

        Vector3 pos = gameObject.transform.localPosition;

        pos.y = 0f;
        pos.z = 0f;
        gameObject.transform.localPosition = pos;

        UpdatePosition();

        /* make sure top and bottom position
         * topPos.x = 0.6f;
         * topPos.y = diffY-this.top;
         * btmPos.x = 0.6f;
         * btmPos.y = -diffY+this.bottom;
         * t.transform.localPosition = topPos;
         * b.transform.localPosition = btmPos;
         */
    }
Пример #4
0
    public bool Hits(Bird bird)
    {
        float diffY = ScreenUtils.GetWorldScreenSize().y / 2f;

        float top = (diffY - (this.top));

        if ((bird.y + (bird.r / 2f)) > top || (bird.y - (bird.r / 2f)) < -diffY + (this.bottom))
        {
            if (bird.x > this.x && bird.x < this.x + (this.w / 2f))
            {
                return(true);
            }
        }
        return(false);
    }
Пример #5
0
    public void think(List <GameObject> pipes)
    {
        // First find the closest pipe
        Pipe  closest = null;
        float record  = Mathf.Infinity;

        for (var i = 0; i < pipes.Count; i++)
        {
            Pipe  p    = pipes [i].GetComponent <Pipe> ();
            float diff = p.x - this.x;
            if (diff > 0 && diff < record)
            {
                record  = diff;
                closest = pipes[i].GetComponent <Pipe>();
            }
        }

        if (closest != null)
        {
            // Now create the inputs to the neural network
            List <float> inputs = new List <float>();
            // x position of closest pipe
            float diffY = ScreenUtils.GetWorldScreenSize().y / 2f;

            float i0 = map(closest.x, this.x, ScreenUtils.GetWorldScreenSize().x / 2f, -1f, 1f);
            inputs.Add(i0);
            // top of closest pipe opening
            float i1 = map(diffY - closest.top, -ScreenUtils.GetWorldScreenSize().y / 2f, ScreenUtils.GetWorldScreenSize().y / 2f, -1f, 1f);
            inputs.Add(i1);
            // bottom of closest pipe opening
            float i2 = map(-diffY + closest.bottom, -ScreenUtils.GetWorldScreenSize().y / 2f, ScreenUtils.GetWorldScreenSize().y / 2f, -1f, 1f);
            inputs.Add(i2);
            // bird's y position
            float i3 = map(this.y, -ScreenUtils.GetWorldScreenSize().y / 2f, ScreenUtils.GetWorldScreenSize().y, -1f, 1f);
            inputs.Add(i3);
            // Get the outputs from the network
            List <float> action = this.brain.feedforward(inputs);
            // Decide to jump or not!

            if (action[1] > action[0])
            {
                this.up();
            }
        }
    }
Пример #6
0
    public void Init(NeuralNetwork _brain)
    {
        // position and size of bird
        this.x = -(ScreenUtils.GetWorldScreenSize().y / 2f) + 1f;
        this.y = ScreenUtils.GetWorldScreenSize().y / 2f;
        this.r = 0.5f;

        // Gravity, lift and velocity
        this.gravity  = 0.4f;
        this.lift     = 1.3f;
        this.velocity = 0f;


        // Is this a copy of another Bird or a new one?
        // The Neural Network is the bird's "brain"
        if (_brain is NeuralNetwork)
        {
            this.brain = _brain.copy();
            this.brain.mutate();
        }
        else
        {
            this.brain = new NeuralNetwork(4, 16, 2);
        }

        // Score is how many frames it's been alive
        this.score = 0;

        // Fitness is normalized version of score
        this.fitness = 0f;

        Vector3 pos = gameObject.transform.localPosition;

        pos.x = this.x;
        pos.y = this.y;
        gameObject.transform.localPosition = pos;
    }