Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 Ghost
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D ghostPic;
- Texture2D background;
- Random rand;
- int ghostColour;
- Rectangle ghostLocation;
- float timeRemaining;
- float showTime;
- int score;
- MouseState mouseState, mouseStatePrevious;
- 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()
- {
- // TODO: Add your initialization logic here
- graphics.PreferredBackBufferHeight = 550;
- graphics.PreferredBackBufferWidth = 750;
- this.IsMouseVisible = true;
- this.Window.Title = "Ghost Hunt | Score: 0";
- this.Window.AllowUserResizing = true;
- showTime = 2f;
- rand = new Random();
- 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()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- ghostPic = Content.Load<Texture2D>("GhostPic");
- background = Content.Load<Texture2D>("Background");
- // TODO: use this.Content to load your game content here
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <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();
- // TODO: Add your update logic here
- if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
- this.Exit();
- mouseState = Mouse.GetState();
- if (mouseState.LeftButton == ButtonState.Pressed && mouseStatePrevious.LeftButton == ButtonState.Released && ghostLocation.Contains(mouseState.X, mouseState.Y))
- {
- if (ghostColour == 0) score++;
- if (ghostColour == 1) score--;
- this.Window.Title = "Ghost Hunt | Score: " + score.ToString();
- timeRemaining = 0.0f;
- }
- else
- {
- timeRemaining = MathHelper.Max(0, timeRemaining - (float)gameTime.ElapsedGameTime.TotalSeconds);
- }
- mouseStatePrevious = mouseState;
- if (timeRemaining == 0.0f)
- {
- ghostColour = rand.Next(2);
- ghostLocation = new Rectangle(rand.Next(GraphicsDevice.Viewport.Bounds.Width - ghostPic.Width), rand.Next(GraphicsDevice.Viewport.Bounds.Height - ghostPic.Height), ghostPic.Width / 2, ghostPic.Height / 2);
- timeRemaining = showTime;
- }
- 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(SpriteSortMode.BackToFront,BlendState.AlphaBlend);
- spriteBatch.Draw(background, new Rectangle(0,0,GraphicsDevice.Viewport.Bounds.Width,GraphicsDevice.Viewport.Bounds.Height), Color.White);
- switch (ghostColour)
- {
- case 0:
- spriteBatch.Draw(ghostPic, ghostLocation, Color.White * 0.7f);
- break;
- case 1:
- spriteBatch.Draw(ghostPic, ghostLocation, new Color(255,0,0,50));
- break;
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement