ArcadeRPG.GameEngine.Draw C# (CSharp) Method

Draw() public method

This is called when the game should draw itself.
public Draw ( GameTime gameTime, SpriteBatch spriteBatch ) : void
gameTime Microsoft.Xna.Framework.GameTime Provides a snapshot of timing values.
spriteBatch Microsoft.Xna.Framework.Graphics.SpriteBatch
return void
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            game_state.tile_engine.getCurrentMap().drawBackground(spriteBatch, game_state.local_player.getX(), game_state.local_player.getY());
            game_state.tile_engine.getCurrentMap().drawObjects(spriteBatch, game_state.local_player.getX(), game_state.local_player.getY());

            if (game_state.local_player.getX() <= 400 - 16)
            {
                character_sprite[(int)game_state.local_player.getWeapon()].loc.X = game_state.local_player.getX();
            }
            else
            {

                if (game_state.local_player.getX() >= game_state.tile_engine.getCurrentMap().getWidth() * game_state.tile_engine.getTileSize() - 400 - 16)
                    character_sprite[(int)game_state.local_player.getWeapon()].loc.X = game_state.local_player.getX() - (game_state.tile_engine.getCurrentMap().getWidth() * game_state.tile_engine.getTileSize() - 800);
                else
                    character_sprite[(int)game_state.local_player.getWeapon()].loc.X = (800 / 2) - 16;// -(32 / 2);
            }

            if (game_state.local_player.getY() <= 240 - 16)
            {
                character_sprite[(int)game_state.local_player.getWeapon()].loc.Y = game_state.local_player.getY();
            }
            else
            {

                if (game_state.local_player.getY() >= game_state.tile_engine.getCurrentMap().getHeight() * game_state.tile_engine.getTileSize() - 240 - 16)
                    character_sprite[(int)game_state.local_player.getWeapon()].loc.Y = game_state.local_player.getY() - (game_state.tile_engine.getCurrentMap().getHeight() * game_state.tile_engine.getTileSize() - 480);
                else
                    character_sprite[(int)game_state.local_player.getWeapon()].loc.Y = (480 / 2) - 16;// - (32 / 2);

            }

            // Fade a color over the player if they are hurt or have a boost active
            bool shouldFadeHurt = game_state.local_player.hurt;
            bool shouldFadeAttack = game_state.local_player.getAttackBonus() > 0;
            bool shouldFadeDefense = game_state.local_player.getDefenseBonus() > 0;
            Color toFade;
            //choose arbitrary colors for boosts
            if (shouldFadeHurt)
            {
                toFade = Color.Red;
            }
            else if (shouldFadeAttack && shouldFadeDefense)
            {
                toFade = Color.LightGreen;
            }
            else if (shouldFadeAttack)
            {
                toFade = Color.LightYellow;
            }
            else if (shouldFadeDefense)
            {
                toFade = Color.LightBlue;
            }
            else
            {
                toFade = Color.White;
            }

            if (sword_swing) // if the sword has been swung
            {
                sword_sprite.loc = character_sprite[(int)game_state.local_player.getWeapon()].loc;
                sword_sprite.Draw(spriteBatch, (int)game_state.local_player.getDirection(), (shouldFadeHurt || shouldFadeAttack || shouldFadeDefense), toFade);
            }
            else
            {
                character_sprite[(int)game_state.local_player.getWeapon()].Draw(spriteBatch, (shouldFadeHurt || shouldFadeAttack || shouldFadeDefense), toFade);
            }

            //Draw monsters
            List<Enemy> monsters = game_state.monster_engine.GetMonsters();
            int offset_x = game_state.local_player.getX() - (int)character_sprite[(int)game_state.local_player.getWeapon()].loc.X;
            int offset_y = game_state.local_player.getY() - (int)character_sprite[(int)game_state.local_player.getWeapon()].loc.Y;

            for (int i = 0; i < monsters.Count(); ++i)
            {
                Enemy monster = monsters.ElementAt(i);
                if (game_state.monster_engine.IsVisible(monster))
                {

                    Sprite mons_sprite = monster.getSprite();
                    mons_sprite.loc.X = monster.getX() - offset_x;
                    mons_sprite.loc.Y = monster.getY() - offset_y;
                    if (mons_sprite.loc.X > 0 && mons_sprite.loc.X < 800)
                    {
                        if (mons_sprite.loc.Y > 0 && mons_sprite.loc.Y < 400)
                        {
                            mons_sprite.Draw(spriteBatch); //Draw if the location is not negative
                        }
                    }
                }
            }

            List<Bullet> bullets = game_state.bullet_engine.GetBullets();
            //draw the bullets appropriately
            for (int i = 0; i < bullets.Count(); ++i)
            {
                Bullet bullet = bullets.ElementAt(i);
                if (bullet.type != bulletType.SWORD)
                {
                    bullet_sprite.loc.X = bullet.x - offset_x;
                    bullet_sprite.loc.Y = bullet.y - offset_y;
                    bullet_sprite.Draw(spriteBatch);
                }
            }

            game_state.tile_engine.getCurrentMap().drawForeground(spriteBatch,game_state.local_player.getX(), game_state.local_player.getY());
            game_state.fx_engine.Draw(spriteBatch,offset_x, offset_y);

            //Draw HUD

            //begin operations on display textures
            //gets spritebatch in a state to be 'ready' to draw

            //******************DRAWING ARROWS**************************//
            spriteBatch.Draw(uparrow, uparrowpos, null, trans, 0, imageOffset, 4.0f, SpriteEffects.None, 0);
            spriteBatch.Draw(downarrow, downarrowpos, null, trans, 0, imageOffset, 4.0f, SpriteEffects.None, 0);
            spriteBatch.Draw(leftarrow, leftarrowpos, null, trans, 0, imageOffset, 4.0f, SpriteEffects.None, 0);
            spriteBatch.Draw(rightarrow, rightarrowpos, null, trans, 0, imageOffset, 4.0f, SpriteEffects.None, 0);
            spriteBatch.Draw(fire_button, fire_button_pos, null, trans, 0, imageOffset, 3.0f, SpriteEffects.None, 0);
            spriteBatch.DrawString(displayFont, "FIRE!", new Vector2(fire_button_pos.X + 38, fire_button_pos.Y + 45), Color.Black);
            //draw the arrow graphics to the screen with given position, 3x as big as original size, no effects
            //************************************************************//

            //***********************DRAWING GRAPHIC SPRITES***********************//
            spriteBatch.Draw(backpack, backpackpos, null, trans, 0, imageOffset, new Vector2(0.4f,1.0f), SpriteEffects.None, 0); //draw the backpack "button"
            spriteBatch.DrawString(displayFont, "Inventory", new Vector2(backpackpos.X + 20, backpackpos.Y + 12), Color.Black);
            spriteBatch.Draw(healthbar_empty, health_bar_empty_rec, null, trans);  //draw health bar
            spriteBatch.Draw(healthbar_full, health_bar_rec, null, trans);  //draw health bar
            string curHealth = "" + game_state.local_player.getHealth();
            string maxHealth = "" + game_state.local_player.getMaxHealth();
            spriteBatch.DrawString(displayFont, "Health: " + curHealth + "/" + maxHealth, new Vector2(health_bar_rec.X + 42, health_bar_rec.Y + 5), Color.Black);
            //********************************************************************//

            //***************************DRAWING STRINGS***********************************//
            spriteBatch.DrawString(displayFont, levelstring, scoreStringPos, Color.Cyan); // draw "level: "
            spriteBatch.DrawString(displayFont, game_state.tile_engine.getCurrentLevelName(), currScorePos, Color.Cyan); // draw level number
            spriteBatch.DrawString(displayFont, livesString+livesRemaining.ToString(), livesStringPos, Color.Cyan); // draw lives remaining
            //****************************************************************************//

            //check to see if the score needs to continue to be drawn (if time hasn't run out)
            //if it has, undraw it and display time expired string and menu
            if (!hasTimeLeft())
            {
                die();
                if(hasMoreLives())
                    timex.Show(spriteBatch); // brings up time expired screen
            }
            else
            {
                spriteBatch.DrawString(displayFont, timeLeft, timeLeftPos, Color.Cyan);
                spriteBatch.DrawString(displayFont, ((int)(currTime.TotalSeconds)).ToString(), timePos, Color.Cyan);

            }
            if(backpackmenu.backpack_touched) // backpack button has been pressed
            {
                if (hasTimeLeft()) // if time hasnt run out during the period where the inventory is brought up by the user, show the inventory
                {
                    backpackmenu.Show(spriteBatch); // draw backpack menu
                    drawItems(spriteBatch, itemfont); // draw items according to layout in itemfont
                }
                else // time has run out while the inventory is up, hide inventory and bring up time expired menu
                {
                    backpackmenu.Hide();
                    timex.Show(spriteBatch);
                }
            }
        }

Usage Example

コード例 #1
0
ファイル: Game1.cs プロジェクト: nkreitz/Super-Space-Escape
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            if (intro.isShowing())
            {
                spriteBatch.Begin();
                intro.Show(spriteBatch); // show intro screen until timer runs out
                spriteBatch.End();
            }
            else if (instruct.isShowing())
            {
                spriteBatch.Begin();
                instruct.Show(spriteBatch); // show instructions screen until timer runs out
                spriteBatch.End();
            }
            else if (instruct2.isShowing())
            {
                spriteBatch.Begin();
                instruct2.Show(spriteBatch); // show instructions screen until timer runs out
                spriteBatch.End();
            }
            else if (timex.isShowing())
            {
                spriteBatch.Begin();
                timex.Show(spriteBatch); // show time expired screen until user makes a selection
                spriteBatch.End();
            }
            else if (gameover.isShowing())
            {
                spriteBatch.Begin();
                gameover.Show(spriteBatch);
                spriteBatch.End();
            }
            else // no menus displayed
            {
                spriteBatch.Begin();
                game_engine.Draw(gameTime, spriteBatch);
                spriteBatch.End(); // go to game environment
            }
            base.Draw(gameTime);
        } // end draw function
All Usage Examples Of ArcadeRPG.GameEngine::Draw