Jump to content

Is this efficient?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Bull3t09

Bull3t09

    Newbie

  • Members
  • Pip
  • 8 posts
This little app was written using microsoft XNA 3.1 for a programming course I'm taking. We haven't covered efficiency at all, and I'm wondering if the way this application is written would be more efficient if the sprites were encapsulated in classes and those classes handled the data manipulation. This code is fully compilable and was written in Visual Studio 2008.


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 BouncingBalls

{

    /// <summary>

    /// This is the main type for your game

    /// </summary>

    public class Game1 : Microsoft.Xna.Framework.Game

    {

        GraphicsDeviceManager graphics;

        SpriteBatch spriteBatch;


        //ball 1

        Texture2D redBall;

        Vector2 acceleration;

        Vector2 ballPosition;

        Vector2 ballCenter;


        int radius;

        float xPos, yPos;

        float friction;


        //ball 2

        Texture2D blackBall;

        Vector2 acceleration2;

        Vector2 ballPosition2;

        Vector2 ballCenter2;


        int radius2;

        float xPos2, yPos2;

        float friction2;



        public Game1()

        {

            graphics = new GraphicsDeviceManager(this);

            Content.RootDirectory = "Content";

        }


        /// <summary>

        /// Allows the game to perform any initialization it needs to before starting to run.

        /// This is where it can query for any required services and load any non-graphic

        /// related content.  Calling base.Initialize will enumerate through any components

        /// and initialize them as well.

        /// </summary>

        protected override void Initialize()

        {


            xPos = 4.0f;

            yPos = 4.0f;


            xPos2 = 5.0f;

            yPos2 = 5.0f;


            ballPosition = new Vector2(0.0f, 0.0f);

            ballPosition2 = new Vector2(10.0f, 10.0f);


            acceleration = new Vector2(xPos, yPos);

            acceleration2 = new Vector2(xPos2, yPos2);


            friction = 0.003f;

            friction2 = 0.005f;


            base.Initialize();

        }


        /// <summary>

        /// LoadContent will be called once per game and is the place to load

        /// all of your content.

        /// </summary>

        protected override void LoadContent()

        {

            spriteBatch = new SpriteBatch(GraphicsDevice);


            redBall = Content.Load<Texture2D>("Images\\Red_Circle");

            blackBall = Content.Load<Texture2D>("Images\\Black_Circle");


            ballCenter = new Vector2(redBall.Height / 2.0f, redBall.Width / 2.0f);

            ballCenter2 = new Vector2(blackBall.Height / 2.0f, blackBall.Width / 2.0f);


            radius = redBall.Height / 2;

            radius2 = blackBall.Height / 2;


            ballPosition = new Vector2(Window.ClientBounds.Width/2 - redBall.Width / 2,

                                          Window.ClientBounds.Height/2 - redBall.Height / 2);


            ballPosition2 = new Vector2(Window.ClientBounds.Width / 2 - blackBall.Width,

                                         Window.ClientBounds.Height / 2 - blackBall.Height);

        }


        /// <summary>

        /// UnloadContent will be called once per game and is the place to unload

        /// all content.

        /// </summary>

        protected override void UnloadContent()

        {


        }


        /// <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)

        {

            

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||

                Keyboard.GetState().IsKeyDown(Keys.Escape))

                this.Exit();


            if (Keyboard.GetState().IsKeyDown(Keys.Up))

                acceleration.Y = -yPos;


            if (Keyboard.GetState().IsKeyDown(Keys.Down))

                acceleration.Y = yPos;


            if (Keyboard.GetState().IsKeyDown(Keys.Left))

                acceleration.X = -xPos;


            if (Keyboard.GetState().IsKeyDown(Keys.Right))

                acceleration.X = xPos;


            ballCenter.X = ballPosition.X + radius;

            ballCenter.Y = ballPosition.Y + radius;


            ballCenter2.X = ballPosition2.X + radius2;

            ballCenter2.Y = ballPosition2.Y + radius2;


            if (ballCenter.X + radius > Window.ClientBounds.Width || ballCenter.X - radius < 0)

                acceleration.X = -acceleration.X;


            if (ballCenter2.X + radius2 > Window.ClientBounds.Width || ballCenter2.X - radius2 < 0)

                acceleration2.X = -acceleration2.X;


            if (ballCenter.Y + radius > Window.ClientBounds.Height || ballCenter.Y - radius < 0)

                acceleration.Y = -acceleration.Y;


            if (ballCenter2.Y + radius2 > Window.ClientBounds.Height || ballCenter2.Y - radius2 < 0)

                acceleration2.Y = -acceleration2.Y;


            if (acceleration.X > 0)

                acceleration.X -= friction;


            else

                acceleration.X += friction;


            if (acceleration2.X > 0)

                acceleration2.X -= friction2;


            else

                acceleration2.X += friction2;


            if (acceleration.Y > 0)

                acceleration.Y -= friction;


            else

                acceleration.Y += friction;


            if (acceleration2.Y > 0)

                acceleration2.Y -= friction2;


            else

                acceleration2.Y += friction2;


            ballPosition += acceleration;

            ballPosition2 += acceleration2;


            base.Update(gameTime);

        }


        /// <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);


            spriteBatch.Begin();

            spriteBatch.Draw(redBall, ballPosition, Color.White);

            spriteBatch.Draw(blackBall, ballPosition2, Color.White);

            spriteBatch.End();


            base.Draw(gameTime);

        }

    }

}



#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
More efficient? Probably not. There is an overhead when calling methods that you won't have with monolithic code (like yours). But if you want to add a third ball, or a forth, or change how a ball works, then this code is hard to maintain and more likely to encounter a bug than if you had a ball object that handled it's own data (only have to make changes in one place to change ball behavior, or you can just add another ball to a list to have more than 2).

One thing you can do to improve efficiency is to only make a call one time, rather than every time you want the value. For example, you call Keyboard.GetState() 5 times in your Update method. Call it once and save the value.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users