home *** CD-ROM | disk | FTP | other *** search
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Components;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Storage;
- using XNAExtras;
-
- namespace StopTheLogo
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- partial class Game1 : Microsoft.Xna.Framework.Game
- {
-
- //this is a texture we can render
- Texture2D pcwLogo;
- Texture2D upArrow;
- //coordinates to draw the sprite at
- int spriteX = 0;
- int spriteY = 0;
- int ArrowX = 0; //position where fixed arrow starts drawing
-
- bool isRunning = true;
- bool wasDown = false;
-
- String caption = "Press SPACE to stop the logo.";
-
- public static BitmapFont m_fontTimes;
-
- //this is the object that will draw the sprites
- SpriteBatch spriteBatch;
-
- //store some info about the sprite's motion
- int m_dSpriteHorizSpeed = 5;
- int m_dSpriteVertSpeed = 0;
-
- int oldSpeed = 0;
-
- protected override void OnStarting()
- {
- base.OnStarting();
- graphics.GraphicsDevice.DeviceReset += new EventHandler(GraphicsDevice_DeviceReset);
-
- m_fontTimes = new BitmapFont("times.xml");
- LoadResources();
- spriteY = (int)Math.Truncate((double)((Window.ClientHeight/4) - (pcwLogo.Height/2)));
- ArrowX = (int)(Window.ClientWidth / 2);
- Window.Title = "Stop the logo";
- }
-
- void GraphicsDevice_DeviceReset(object sender, EventArgs e)
- {
- LoadResources();
- }
-
- void LoadResources()
- {
- pcwLogo = Texture2D.FromFile(graphics.GraphicsDevice, "pcwlogo.dds");
- upArrow = Texture2D.FromFile(graphics.GraphicsDevice, "arrowup.dds");
- spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
- m_fontTimes.Reset(graphics.GraphicsDevice);
- m_fontTimes.KernEnable = true;
- }
-
- public Game1()
- {
- InitializeComponent();
- }
-
- protected override void Update()
- {
- // The time since Update was called last
- float elapsed = (float)ElapsedTime.TotalSeconds;
-
- // TODO: Add your game logic here
- // System.Diagnostics.Debug.WriteLine("Update");
-
- KeyboardState kbs = Keyboard.GetState();
- if (kbs.IsKeyDown(Keys.Space) & !wasDown)
- {
- wasDown = true;
-
- if (isRunning)
- {
- //stop the sprite
-
- isRunning = false;
-
- m_dSpriteHorizSpeed = 0;
- m_dSpriteVertSpeed = 0;
-
- //how far are we from the arrow?
- int StopArrowPos = spriteX + 74;
- int Distance = Math.Abs(ArrowX - StopArrowPos);
-
- int score = 20 - Distance;
-
- if (score < 0)
- {
- score = 0;
- caption = "Oops, Zero score! Press space to try again.";
- }
- else if (score == 20)
- {
- caption = "PERFECT SCORE! Press space to try again.";
- }
- else
- {
- caption = "Score: " + score.ToString() + "/20. Press space to try again.";
- }
- }
- else
- {
- //reset the game
- isRunning = true;
- m_dSpriteHorizSpeed = oldSpeed;
-
- caption = "Press SPACE to stop the logo.";
-
- }
-
- }
- else
- {
- wasDown = kbs.IsKeyDown(Keys.Space);
- }
- this.UpdateSprite();
-
- // Let the GameComponents update
- UpdateComponents();
- }
-
- protected override void Draw()
- {
- // Make sure we have a valid device
- if (!graphics.EnsureDevice())
- return;
-
- graphics.GraphicsDevice.Clear(Color.LightYellow);
- graphics.GraphicsDevice.BeginScene();
-
- // TODO: Add your drawing code here
- spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
- spriteBatch.Draw(pcwLogo, new Rectangle(spriteX, spriteY, pcwLogo.Width, pcwLogo.Height), Color.White);
- spriteBatch.Draw(upArrow, new Rectangle(ArrowX, spriteY + pcwLogo.Height, upArrow.Width, upArrow.Height), Color.White);
-
- spriteBatch.End();
-
- int nWidth = m_fontTimes.MeasureString(caption);
- m_fontTimes.DrawString(20, 120 + m_fontTimes.LineHeight, Color.Black, caption, nWidth);
-
- // Let the GameComponents draw
- DrawComponents();
-
- graphics.GraphicsDevice.EndScene();
- graphics.GraphicsDevice.Present();
- }
-
- void UpdateSprite()
- {
- //move the sprite by speed
- spriteX += m_dSpriteHorizSpeed;
- spriteY += m_dSpriteVertSpeed;
-
- int MaxX = Window.ClientWidth - pcwLogo.Width;
- int MinX = 0;
- int MaxY = Window.ClientHeight - pcwLogo.Height;
- int MinY = 0;
-
- //check for bounce
- if (spriteX > MaxX)
- {
- m_dSpriteHorizSpeed *= -1;
- spriteX = MaxX;
- }
- else if (spriteX < MinX)
- {
- m_dSpriteHorizSpeed *= -1;
- spriteX = MinX;
- }
-
- if (spriteY > MaxY)
- {
- m_dSpriteVertSpeed *= -1;
- spriteY = MaxY;
- }
- else if (spriteY < MinY)
- {
- m_dSpriteVertSpeed *= -1;
- spriteY = MinY;
- }
-
- if (isRunning)
- {
- oldSpeed = m_dSpriteHorizSpeed;
- }
- }
- }
- }