次の方法で共有


Bizzy Bees Step 5: Adding some bees to the mix (XNA Walkthrough)

This is part of a walkthrough series on creating a game (Bizzy Bees) in XNA

Overview
Step 1: Setting the stage (projects and assets)
Step 2: Drawing the scene
Step 3: Adding flowers
Step 4: Making things move
Step 5: Adding some bees to the mix
Step 6: User interaction
Step 7: Rounding it up

We have two steps left before we have a fully functioning game.  We need to add some bees to match with the flowers and we need to add some user interaction to be able to match the flowers and the bees.

BeePicker and Bees

We are going to use a similar way to generate the bees as we did with the flowers.

First off we have a BeePicker (the bottom area of the screen) that takes care of adding, removing and interacting with the bees

image

Add a new BeePicker class with the following contents

  class BeePicker
    {
        const int beeDeltaX = 96;
        const int beeStartX = 5;
        const int beeStartY = 700;
        const int numberOfBeeColors = 5;

        private List<Bee> bees = null;
        private SpriteBatch spriteBatch;
        private Texture2D beeMap;
        private Random r;

        public BeePicker(ContentManager content, SpriteBatch spriteBatch, int seed)
        {
            beeMap = content.Load<Texture2D>("beemap");
            this.spriteBatch = spriteBatch;
            bees = new List<Bee>();
            r = new Random(seed);

            for (int i = 0; i < 5; i++)
            {
                AddRandomBee();
            }

        }

        private void AddRandomBee()
        {
            bees.Add(new Bee(r.Next(numberOfBeeColors + 1)));
        }

        public void Draw()
        {
            for (int i = 0; i < 5; i++)
            {
                spriteBatch.Draw(beeMap, new Vector2(beeStartX + i * beeDeltaX, beeStartY), new Rectangle(bees[i].Color * 91, 0, 91, 91), Color.White);
            }
        }
    }

Most of this should look very familiar from the colum class except much simpler since the bees don’t move

The Bee class itself is for now just holding the color of the bee

     class Bee { public int Color;  public Bee(int color) { this.Color = color; } }

Generating and drawing bees

In order to generate the bees and get them on the screen we add a BeePicker to the Game1 class

  BeePicker beePicker = null;

initialize the bee picker in the LoadContent class

             beePicker = new BeePicker(Content, spriteBatch, System.DateTime.Now.Millisecond);

and then to draw it by calling beePicker.Draw in the Game1.Draw method

  if (!gameOver)
            {
                foreach (Column c in columns)
                    c.Draw();
 beePicker.Draw();              }

And voila, we have all of the UI in place. 

beePicker

Next time we will add the user interaction and put some finishing touches on this.