Exemplo n.º 1
0
        public ExampleGameMain()
        {
            
            #region Giberish you don't need to know about
            graphics = new GraphicsDeviceManager(this);

            this.IsMouseVisible = true;

            Window.AllowUserResizing = true;
            graphics.PreferredBackBufferHeight = 250;
            graphics.PreferredBackBufferWidth = 500;

#if ANDROID
            graphics.PreferredBackBufferHeight = Window.ClientBounds.Height;
            graphics.PreferredBackBufferWidth = Window.ClientBounds.Width;
#endif
            baseScreenSize = new Vector2(500, 250);

            Content.RootDirectory = "Content";
            #endregion

            random = new Random();

            Heads = new TheHead[10];
            for (int i = 0; i < positions.Length; i++)
            {
                Heads[i] = new TheHead();
                Heads[i].SourceBox = new Rectangle(0, 0, 231, 166);
                Heads[i].DrawBox = positions[i];
                Heads[i].Hit = true;
            }

            headInterval = new TimeSpan(0, 0, 1);
            gameOver = false;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            
            mouse = Mouse.GetState();
            if (lastMousePos == null || lastMousePos != new Vector2(mouse.X, mouse.Y))
            {
                lastMousePos = new Vector2(mouse.X, mouse.Y);
            }

            Point click = Point.Zero;


            //account for screen size changes
            if (mouse.LeftButton == ButtonState.Pressed)
            {
                click.X = (int)(mouse.X * baseScreenSize.X / Window.ClientBounds.Width);
                click.Y = (int)(mouse.Y * baseScreenSize.Y / Window.ClientBounds.Height);
            }

            #if ANDROID
            touch = TouchPanel.GetState();
            if(touch.Count>0)
            {
                click.X = (int)(touch[0].Position.X * baseScreenSize.X / Window.ClientBounds.Width);
                click.Y = (int)(touch[0].Position.Y * baseScreenSize.Y / Window.ClientBounds.Height);
            }
            #endif

            if (!gameOver)
            {
                int headCount = 0;
                for (int i = 0; i < Heads.Length; i++)
                {
                    if (!Heads[i].Hit)
                        headCount++;
                }

                if (headCount > 9)
                {
                    gameOver = true;
                }

                for (int i = 0; i < Heads.Length; i++)
                {
                    if (Heads[i].CollisionBox.Contains(click))
                    {
                        if (Heads[i].Hit == false)
                        {
                            score++;
                            headInterval -= new TimeSpan(0,0,0,0,10);
                        }
                        Heads[i].Hit = true;
                    }
                }


                //add a head
                TimeSpan delta = DateTime.Now - lastHead;
                if (delta > headInterval)
                {
                    lastHead = DateTime.Now;
                    bool foundOne = false;
                    int i = 0;
                    while (!foundOne)
                    {
                        i = random.Next(0, 10);
                        if (Heads[i].Hit == true)
                            foundOne = true;
                    }

                    Heads[i] = new TheHead();
                    Heads[i].SourceBox = new Rectangle(0, 0, 231, 166);
                    Heads[i].DrawBox = positions[i];
                    Heads[i].Head = headTexture;
                }

            }
            else
            {
                TimeSpan delta = DateTime.Now - lastHead;
                if(delta > new TimeSpan(0,0,10))
                {
                    //reset game
                    Heads = new TheHead[10];
                    for (int i = 0; i < positions.Length; i++)
                    {
                        Heads[i] = new TheHead();
                        Heads[i].SourceBox = new Rectangle(0, 0, 231, 166);
                        Heads[i].DrawBox = positions[i];
                        Heads[i].Hit = true;
                    }

                    headInterval = new TimeSpan(0, 0, 1);
                    gameOver = false;

                    score = 0;
                }
            }
            // TODO: Add your update logic here

            base.Update(gameTime);

        }