home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 December / PCWDEC06.iso / Software / HandsOn / WindowsGame1 / Game1.cs < prev    next >
Encoding:
Text File  |  2006-09-07  |  6.2 KB  |  203 lines

  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Audio;
  5. using Microsoft.Xna.Framework.Components;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8. using Microsoft.Xna.Framework.Storage;
  9. using XNAExtras;
  10.  
  11. namespace StopTheLogo
  12. {
  13.     /// <summary>
  14.     /// This is the main type for your game
  15.     /// </summary>
  16.     partial class Game1 : Microsoft.Xna.Framework.Game
  17.     {
  18.  
  19.         //this is a texture we can render
  20.         Texture2D pcwLogo;
  21.         Texture2D upArrow;
  22.         //coordinates to draw the sprite at
  23.         int spriteX = 0;
  24.         int spriteY = 0;
  25.         int ArrowX = 0; //position where fixed arrow starts drawing
  26.  
  27.         bool isRunning = true;
  28.         bool wasDown = false;
  29.   
  30.         String caption = "Press SPACE to stop the logo.";
  31.  
  32.         public static BitmapFont m_fontTimes;
  33.         
  34.         //this is the object that will draw the sprites
  35.         SpriteBatch spriteBatch;
  36.  
  37.         //store some info about the sprite's motion
  38.         int m_dSpriteHorizSpeed = 5;
  39.         int m_dSpriteVertSpeed = 0;
  40.  
  41.         int oldSpeed = 0;
  42.  
  43.         protected override void OnStarting()
  44.         {
  45.             base.OnStarting();
  46.             graphics.GraphicsDevice.DeviceReset += new EventHandler(GraphicsDevice_DeviceReset);
  47.  
  48.             m_fontTimes = new BitmapFont("times.xml");
  49.             LoadResources();
  50.             spriteY =  (int)Math.Truncate((double)((Window.ClientHeight/4) - (pcwLogo.Height/2)));
  51.             ArrowX = (int)(Window.ClientWidth / 2);
  52.             Window.Title = "Stop the logo";
  53.         }
  54.  
  55.         void GraphicsDevice_DeviceReset(object sender, EventArgs e)
  56.         {
  57.             LoadResources();
  58.         }
  59.  
  60.         void LoadResources()
  61.         {
  62.             pcwLogo = Texture2D.FromFile(graphics.GraphicsDevice, "pcwlogo.dds");
  63.             upArrow = Texture2D.FromFile(graphics.GraphicsDevice, "arrowup.dds");
  64.             spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
  65.             m_fontTimes.Reset(graphics.GraphicsDevice);
  66.             m_fontTimes.KernEnable = true;
  67.         }
  68.         
  69.         public Game1()
  70.         {
  71.             InitializeComponent();
  72.         }
  73.  
  74.         protected override void Update()
  75.         {
  76.             // The time since Update was called last
  77.             float elapsed = (float)ElapsedTime.TotalSeconds;
  78.  
  79.             // TODO: Add your game logic here
  80.             // System.Diagnostics.Debug.WriteLine("Update");
  81.    
  82.             KeyboardState kbs = Keyboard.GetState();
  83.             if (kbs.IsKeyDown(Keys.Space) & !wasDown)
  84.             {
  85.                 wasDown = true;
  86.  
  87.                 if (isRunning)
  88.                 {
  89.                     //stop the sprite
  90.  
  91.                     isRunning = false;
  92.  
  93.                     m_dSpriteHorizSpeed = 0;
  94.                     m_dSpriteVertSpeed = 0;
  95.  
  96.                     //how far are we from the arrow?
  97.                     int StopArrowPos = spriteX + 74;
  98.                     int Distance = Math.Abs(ArrowX - StopArrowPos);
  99.  
  100.                     int score = 20 - Distance;
  101.  
  102.                     if (score < 0)
  103.                     {
  104.                         score = 0;
  105.                         caption = "Oops, Zero score! Press space to try again.";
  106.                     }
  107.                     else if (score == 20)
  108.                     {
  109.                         caption = "PERFECT SCORE! Press space to try again.";
  110.                     }
  111.                     else
  112.                     {
  113.                         caption = "Score: " + score.ToString() + "/20. Press space to try again.";
  114.                     }
  115.                 }
  116.                 else
  117.                 {
  118.                     //reset the game
  119.                     isRunning = true;
  120.                     m_dSpriteHorizSpeed = oldSpeed;
  121.  
  122.                     caption = "Press SPACE to stop the logo.";
  123.  
  124.                 }
  125.  
  126.             }
  127.             else
  128.             {
  129.                 wasDown = kbs.IsKeyDown(Keys.Space);
  130.             }
  131. this.UpdateSprite();
  132.  
  133.             // Let the GameComponents update
  134.             UpdateComponents();
  135.         }
  136.  
  137.         protected override void Draw()
  138.         {
  139.             // Make sure we have a valid device
  140.             if (!graphics.EnsureDevice())
  141.                 return;
  142.  
  143.             graphics.GraphicsDevice.Clear(Color.LightYellow);
  144.             graphics.GraphicsDevice.BeginScene();
  145.  
  146.             // TODO: Add your drawing code here
  147.             spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
  148.             spriteBatch.Draw(pcwLogo, new Rectangle(spriteX, spriteY, pcwLogo.Width, pcwLogo.Height), Color.White);
  149.             spriteBatch.Draw(upArrow, new Rectangle(ArrowX, spriteY + pcwLogo.Height, upArrow.Width, upArrow.Height), Color.White);
  150.           
  151.             spriteBatch.End();
  152.                    
  153.             int nWidth = m_fontTimes.MeasureString(caption);
  154.             m_fontTimes.DrawString(20, 120 + m_fontTimes.LineHeight, Color.Black, caption, nWidth);
  155.            
  156.             // Let the GameComponents draw
  157.             DrawComponents();
  158.  
  159.             graphics.GraphicsDevice.EndScene();
  160.             graphics.GraphicsDevice.Present();
  161.         }
  162.  
  163.         void UpdateSprite()
  164.         {
  165.             //move the sprite by speed
  166.             spriteX += m_dSpriteHorizSpeed;
  167.             spriteY += m_dSpriteVertSpeed;
  168.  
  169.             int MaxX = Window.ClientWidth - pcwLogo.Width;
  170.             int MinX = 0;
  171.             int MaxY = Window.ClientHeight - pcwLogo.Height;
  172.             int MinY = 0;
  173.  
  174.             //check for bounce
  175.             if (spriteX > MaxX)
  176.             {
  177.                 m_dSpriteHorizSpeed *= -1;
  178.                 spriteX = MaxX;
  179.             }
  180.             else if (spriteX < MinX)
  181.             {
  182.                 m_dSpriteHorizSpeed *= -1;
  183.                 spriteX = MinX;
  184.             }
  185.  
  186.             if (spriteY > MaxY)
  187.             {
  188.                 m_dSpriteVertSpeed *= -1;
  189.                 spriteY = MaxY;
  190.             }
  191.             else if (spriteY < MinY)
  192.             {
  193.                 m_dSpriteVertSpeed *= -1;
  194.                 spriteY = MinY;
  195.             }
  196.  
  197.             if (isRunning)
  198.             {
  199.                 oldSpeed = m_dSpriteHorizSpeed;
  200.             }
  201.         }
  202.     }
  203. }