2
\$\begingroup\$

For minimalist sake, let's say I have an image of a button in a game and on the screen. When you left-click the button, it will disappear. If you right-click (anything), the button will reappear.

I'm trying to improve my code, so if you see anything that is redundant or that can be done cleaner, it'd be awesome if you could let me know.

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;

namespace ButtonGame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    MouseState mouse, prevMouse;
    public class GameImages
    {
        //Image Diminsions and Graphic
        public Texture2D texture;
        //Images position on the Viewport
        public Vector2 position = new Vector2(0, 0);

    }//GameImages
    GameImages button;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferWidth = 800;
        graphics.PreferredBackBufferHeight = 600;
        graphics.PreferMultiSampling = false;
        graphics.IsFullScreen = false;
    }//Game1()

    protected override void Initialize()
    {
        button = new GameImages();
        base.Initialize();
    }//Initialize

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        //Gives the button the graphic "button.PNG" from resources
        button.texture = Content.Load<Texture2D>("Button");
        //Sets the drawing point of the button to the middle of the screen
        button.position = new Vector2(400, 300);
    }
    protected override void Update(GameTime gameTime)
    {
        //Gets current position and condition of the mouse
        mouse = Mouse.GetState();
        //Makes the cursor visible on the screen of the game
        this.IsMouseVisible = true;
        //If the User has released the Left mouse button and previously had it pressed, (Left Clicked)
        if (mouse.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Released && prevMouse.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
        {
            //Check if the mouse was within the bounds of GameImage button
            if (CheckForClick(button))
            {
                //Remove the button from view
                button.position = new Vector2(5000, 5000);
            }
        }
            //If the User has released the Right mouse button and previously had it pressed, (Right Clicked)
        else if((mouse.RightButton == Microsoft.Xna.Framework.Input.ButtonState.Released && prevMouse.RightButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed))
        {
            //Bring Back the button
            button.position = new Vector2(400,300);
        }
        //Store the current mouse position and conditon into a orevious state to prepare for new input
        prevMouse = mouse;
        base.Update(gameTime);
    }//Update

    /// <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)
    {
        spriteBatch.Begin();

        //Background Color
        GraphicsDevice.Clear(Color.CornflowerBlue);

        //if button is not off the screen
        if (button.position != new Vector2(5000, 5000))
        {
            //Draw the button
            DrawImage(button);
        }
        spriteBatch.End();
        base.Draw(gameTime);
    }//Draw
    public void DrawImage(GameImages image)
    {
        spriteBatch.Draw(image.texture, image.position, Color.White);
    }//DrawImage
    public bool CheckForClick(GameImages rectangle)
    {

        if (mouse.Y < rectangle.position.Y + rectangle.texture.Height &&
            mouse.Y > rectangle.position.Y &&
            mouse.X > rectangle.position.X &&
            mouse.X < rectangle.position.X + rectangle.texture.Width)
            return true;
        else
            return false;

    }//CheckForClick
}
}

Thoughts:

I've never used the Unload method, maybe that is what I need to/should use.

Another approach I had was to give GameImages a bool variable of visible. Instead of throwing the image to a random vector off-screen, I would change the value of

button.visible = false;

and then in the draw method I would use

if(button.visible)
{
DrawImage(button);
}
\$\endgroup\$
2
\$\begingroup\$

I would recommend your second thought creating an Visibility Property.

if you need such an property for different classes you could go even further and create an Interface

interface IDrawable
{
    bool Visible;
}

now you would be able to use it like

//anything could be drawable
 var drawable = yourobject as IDrawable;

if(drawable.Visible)
{
    //Draw
}
\$\endgroup\$
4
  • \$\begingroup\$ yourObject as IDrawable doesn't look like valid c# syntax to me. \$\endgroup\$ – RubberDuck Jul 8 '14 at 11:53
  • \$\begingroup\$ @ckuhn203 but it is valid. If yourobject used IDrawable you will get an object else you will get null \$\endgroup\$ – WiiMaxx Jul 8 '14 at 12:01
  • \$\begingroup\$ @ckuhn203 here you can check it \$\endgroup\$ – WiiMaxx Jul 8 '14 at 12:02
  • 1
    \$\begingroup\$ Okay then. I guess you learn something new everyday. I'd never seen that before. Thanks. \$\endgroup\$ – RubberDuck Jul 8 '14 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.