5 Replies - 532 Views - Last Post: 13 May 2012 - 04:40 PM

#1 jpars  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-May 12

Rand reset help

Posted 12 May 2012 - 09:13 PM

Hi, im trying to make it so that when the Parachute man collides with the truck the number of birds (upto 5) will be reset and a new random of birds will be called to the screen.

I'm stuck i have no idea how to do it help plz!

Posted Image

not sure what code i need to upload so i put it all in.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace ParachuteGame
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        TruckClass TruckObject;
        Vector2 planeVelocity = new Vector2(-8f, 0f); 
        Texture2D planeTexture;
        Vector2 planePosition = new Vector2(300f, 1f);
        ParachuteClass spawnObject;
        BirdClass[] birdObject;
        int birdCount = 4;
        Random rand = new Random();
        private SpriteEffects planeflip;
        private SpriteFont font;
        private int score = 0;
       

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 700;
            graphics.PreferredBackBufferWidth = 1000;
        }

        protected override void Initialize()
        {

            TruckObject = new TruckClass(TruckClass.Texture, new Vector2(300f, 600f), new Vector2(-10f, 0f), Color.White);
            birdObject = new BirdClass[birdCount];
            for (int index = 0; index < birdCount; index++)
            {
                byte r = (byte)rand.Next(64, 256); // red
                byte g = (byte)rand.Next(64, 256); // green
                byte b = (byte)rand.Next(64, 256); // blue
                
                Color tempColor = new Color(r, g, B)/>;
                birdObject[index] = new BirdClass(BirdClass.Texture, new Vector2(rand.Next(300, 300), rand.Next(300, 300)), new Vector2(rand.Next(-5, -3), rand.Next(-5,-3)), tempColor);
            } 

            
            

            base.Initialize();
        }

        protected override void LoadContent()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);
            TruckClass.Texture = Content.Load<Texture2D>("Sprites\\Truck");
            TruckClass.GraphicsViewport = graphics.GraphicsDevice.Viewport;
            ParachuteClass.Texture = Content.Load<Texture2D>("Sprites\\Parachute1");
            ParachuteClass.GraphicsViewport = graphics.GraphicsDevice.Viewport;
            planeTexture = Content.Load<Texture2D>("Sprites\\Plane");
            BirdClass.Texture = Content.Load<Texture2D>("Sprites\\Bird");
            BirdClass.GraphicsViewport = graphics.GraphicsDevice.Viewport;
            font = Content.Load<SpriteFont>("Score");

        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            TruckObject.Update();
           

            foreach (BirdClass Bird in birdObject)
            {
                Bird.Update();

                if (ParachuteClass.spawn == true)
                {
                 
                    Rectangle parachuteRectangle = new Rectangle((int)spawnObject.Position.X, (int)spawnObject.Position.Y, ParachuteClass.Texture.Width, ParachuteClass.Texture.Height);
                    Rectangle truckRectangle = new Rectangle((int)TruckObject.Position.X, (int)TruckObject.Position.Y, TruckClass.Texture.Width, TruckClass.Texture.Height);
                    Rectangle birdRectangle = new Rectangle((int)Bird.Position.X, (int)Bird.Position.Y, BirdClass.Texture.Width, BirdClass.Texture.Height);

                    if (parachuteRectangle.Intersects(truckRectangle))
                    {
                        score += 10;
                        ParachuteClass.spawn = false;  
                    }
                    if (parachuteRectangle.Intersects(birdRectangle))
                    {
                        score -= 5;
                        ParachuteClass.spawn = false;                        
                    }
                }
                
                
            }

            KeyboardState keyboardState = Keyboard.GetState();

            if (ParachuteClass.spawn == false)
            {
                if (keyboardState.IsKeyDown(Keys.Space))
                {
                    Vector2 spawnPosition = planePosition;
                    spawnObject = new ParachuteClass(ParachuteClass.Texture, spawnPosition);
                    ParachuteClass.spawn = true;
                }
            }
            if (ParachuteClass.spawn)
            {
                spawnObject.MovingSpawn(); 
            }

      

            planePosition = planePosition + planeVelocity;

            if (planePosition.X < 0)
            {
                planeVelocity.X = -planeVelocity.X;
                planePosition.X = planePosition.X + planeVelocity.X;
                this.planeflip = SpriteEffects.FlipHorizontally; 
            }

            else if (planePosition.X > graphics.GraphicsDevice.Viewport.Width - planeTexture.Width)
            {
                planeVelocity.X = -planeVelocity.X;
                planePosition.X = planePosition.X + planeVelocity.X;
                this.planeflip = SpriteEffects.None;
            }

        
          

            base.Update(gameTime);
        }
    

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            spriteBatch.Begin();

            TruckObject.Draw(spriteBatch);
            spriteBatch.Draw(planeTexture, planePosition, null, Color.White, 0.0f, new Vector2(0, 0), 1.0f, this.planeflip, 0.0f);
            if (ParachuteClass.spawn)
            {
                spawnObject.Draw(spriteBatch);
            }
            foreach (BirdClass Bird in birdObject)
            {
                Bird.Draw(spriteBatch);
            }
            spriteBatch.DrawString(font, "Score: " + score, new Vector2(1, 1), Color.Black);

            spriteBatch.End();


            base.Draw(gameTime);
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Rand reset help

#2 jjames967  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 01-January 12

Re: Rand reset help

Posted 13 May 2012 - 12:47 AM

Are you asking how to assign a new random number to birdCount? If so, you already have a random number generator and variable available, so wherever you're updating your game if the chute lands in the truck just add:

birdCount = rand.next(maximum number of birds);

Was This Post Helpful? 1
  • +
  • -

#3 jpars  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-May 12

Re: Rand reset help

Posted 13 May 2012 - 04:39 AM

View Postjjames967, on 13 May 2012 - 12:47 AM, said:

Are you asking how to assign a new random number to birdCount? If so, you already have a random number generator and variable available, so wherever you're updating your game if the chute lands in the truck just add:

birdCount = rand.next(maximum number of birds);


just ran into a new error, the maximum number of birds that will appear on the screen is 2. Even if i make the birdcount 200 only 2 birds spawn.
Was This Post Helpful? 0
  • +
  • -

#4 jjames967  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 01-January 12

Re: Rand reset help

Posted 13 May 2012 - 02:05 PM

Have you stepped through your initialize method yet to make sure they're originally being created in the requested quantity? If so, it might be something in your BirdClass.
Was This Post Helpful? 0
  • +
  • -

#5 jpars  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-May 12

Re: Rand reset help

Posted 13 May 2012 - 02:27 PM

View Postjjames967, on 13 May 2012 - 02:05 PM, said:

Have you stepped through your initialize method yet to make sure they're originally being created in the requested quantity? If so, it might be something in your BirdClass.


Found the problem with the game only making two birds. It was in my bird class in this line
             
if (Position.Y < 400 || Position.Y > GraphicsViewport.Height - Texture.Height)
            {
                Velocity.Y = -Velocity.Y;
                Position.Y = Position.Y + Velocity.Y;
            }



the parameter 400 is what is keeping my birds in a straight line, is there another way to do this?
Was This Post Helpful? 0
  • +
  • -

#6 jjames967  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 01-January 12

Re: Rand reset help

Posted 13 May 2012 - 04:40 PM

View Postjpars, on 13 May 2012 - 02:27 PM, said:

View Postjjames967, on 13 May 2012 - 02:05 PM, said:

Have you stepped through your initialize method yet to make sure they're originally being created in the requested quantity? If so, it might be something in your BirdClass.


Found the problem with the game only making two birds. It was in my bird class in this line
             
if (Position.Y < 400 || Position.Y > GraphicsViewport.Height - Texture.Height)
            {
                Velocity.Y = -Velocity.Y;
                Position.Y = Position.Y + Velocity.Y;
            }



the parameter 400 is what is keeping my birds in a straight line, is there another way to do this?


I'm not quite sure what you're asking. Are you saying that the birds spawn outside of the drawing area? Also, do the birds just fly back and forth horizontally?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1