Don't like ads? PRO users don't see any ads ;-)
Guest

nyashit

By: Mellen on Sep 9th, 2012  |  syntax: C#  |  size: 7.90 KB  |  hits: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace Lesson01
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class Game1 : Microsoft.Xna.Framework.Game
  18.     {
  19.         //GLOBAL VARIABLES
  20.         GraphicsDeviceManager graphics;
  21.         SpriteBatch spriteBatch;
  22.  
  23.         //screen size
  24.         public int screenHeight;
  25.         public int screenWidth;
  26.  
  27.         public int gameState = 0;
  28.  
  29.         //make a texture
  30.         Texture2D nyanTex;
  31.         Texture2D chenTex;
  32.  
  33.         SpriteFont font;
  34.  
  35.         //position things
  36.         Vector2 nyanPos = Vector2.Zero;
  37.  
  38.         //make a chen army, an array if you will
  39.         static int chenPopulation = 25;
  40.         Vector2[] chen = new Vector2[chenPopulation];
  41.  
  42.         Random rand = new Random();
  43.  
  44.         int score = 0;
  45.  
  46.         public Game1()
  47.         {
  48.             graphics = new GraphicsDeviceManager(this);
  49.             Content.RootDirectory = "Content";
  50.         }
  51.  
  52.         /// <summary>
  53.         /// Allows the game to perform any initialization it needs to before starting to run.
  54.         /// This is where it can query for any required services and load any non-graphic
  55.         /// related content.  Calling base.Initialize will enumerate through any components
  56.         /// and initialize them as well.
  57.         /// </summary>
  58.         protected override void Initialize()
  59.         {
  60.             // TODO: Add your initialization logic here
  61.  
  62.             //screen size
  63.             screenWidth = 1024;
  64.             screenHeight = 768;
  65.  
  66.             graphics.PreferredBackBufferWidth = screenWidth;
  67.             graphics.PreferredBackBufferHeight = screenHeight;
  68.  
  69.             graphics.IsFullScreen = false;
  70.             graphics.ApplyChanges();
  71.  
  72.             //nyan start position
  73.             nyanPos.Y = screenHeight-75;
  74.  
  75.             //start position for chen soldiers
  76.             for (int i = 0; i < chenPopulation; i++)
  77.             {
  78.                 //position within screen width minus nyan width
  79.                 chen[i].X = rand.Next(screenWidth - 100);
  80.                 chen[i].Y = 0 - ((i * 150) + 100);
  81.                 //float y = chen[i].Y;
  82.             }
  83.  
  84.             base.Initialize();
  85.         }
  86.  
  87.         /// <summary>
  88.         /// LoadContent will be called once per game and is the place to load
  89.         /// all of your content.
  90.         /// </summary>
  91.         protected override void LoadContent()
  92.         {
  93.             // Create a new SpriteBatch, which can be used to draw textures.
  94.             spriteBatch = new SpriteBatch(GraphicsDevice);
  95.  
  96.             //Load up dem grafix
  97.             nyanTex = Content.Load<Texture2D>("nyancat");
  98.             chenTex = Content.Load<Texture2D>("chen");
  99.  
  100.             font = Content.Load<SpriteFont>("mainfont");
  101.             // TODO: use this.Content to load your game content here
  102.         }
  103.  
  104.         /// <summary>
  105.         /// UnloadContent will be called once per game and is the place to unload
  106.         /// all content.
  107.         /// </summary>
  108.         protected override void UnloadContent()
  109.         {
  110.             // TODO: Unload any non ContentManager content here
  111.         }
  112.  
  113.         /// <summary>
  114.         /// Allows the game to run logic such as updating the world,
  115.         /// checking for collisions, gathering input, and playing audio.
  116.         /// </summary>
  117.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  118.         protected override void Update(GameTime gameTime)
  119.         {
  120.  
  121.             //gameState menu
  122.             switch (gameState)
  123.             {
  124.                 case 0:
  125.                     UpdateMenu(gameTime);  //intro screen
  126.                     break;
  127.  
  128.                 case 1:
  129.                     UpdateGame(gameTime);  //main game
  130.                     break;
  131.  
  132.                 default:
  133.                     /*error*/
  134.                     break;
  135.             }
  136.         }
  137.  
  138.  
  139.  
  140.         public void UpdateMenu(GameTime gameTime)
  141.         {
  142.             KeyboardState key = Keyboard.GetState();
  143.  
  144.             if (key.IsKeyDown(Keys.Space))
  145.             {
  146.                 gameState = 1;
  147.             }
  148.         }
  149.  
  150.         public void UpdateGame(GameTime gameTime)
  151.         {
  152.             // Allows the game to exit
  153.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  154.                 this.Exit();
  155.  
  156.             // TODO: Add your update logic here
  157.  
  158.             //Control our nyan
  159.             KeyboardState key = Keyboard.GetState();
  160.  
  161.             if (key.IsKeyDown(Keys.Left) && nyanPos.X > 0)
  162.             {
  163.                 nyanPos.X -= 5;
  164.             }
  165.             if (key.IsKeyDown(Keys.Right) && nyanPos.X < screenWidth - nyanTex.Width)
  166.             {
  167.                 nyanPos.X += 5;
  168.             }
  169.             if (key.IsKeyDown(Keys.Up) && nyanPos.Y > 0)
  170.             {
  171.                 nyanPos.Y -= 5;
  172.             }
  173.             if (key.IsKeyDown(Keys.Down) && nyanPos.Y < screenHeight - nyanTex.Height)
  174.             {
  175.                 nyanPos.Y += 5;
  176.             }
  177.  
  178.             //Drop some chens!
  179.  
  180.             for (int i = 0; i < chenPopulation; i++)
  181.             {
  182.                 chen[i].Y += 5;
  183.                 if (chen[i].Y > screenHeight)
  184.                 {
  185.                     Random rand = new Random();
  186.                     chen[i].X = rand.Next(screenWidth - chenTex.Width);  //randomise spawn position of next chen
  187.                     chen[i].Y = 0 - (chenPopulation * (150 - score / 10)) - 100; //score increments by 10 when chen leaves bottom of screen
  188.                     score += 10;
  189.                 }
  190.             }
  191.  
  192.  
  193.             //get centre of nyan
  194.             int x = (int)nyanPos.X + (nyanTex.Width / 2);
  195.             int y = (int)nyanPos.Y + (nyanTex.Height / 2);
  196.             //check for collision
  197.             for (int i = 0; i < chenPopulation; i++)
  198.             {
  199.                 if (x > chen[i].X && x < chen[i].X + chenTex.Width)
  200.                 {
  201.                     if (y > chen[i].Y && y < chen[i].Y + chenTex.Height)
  202.                     {
  203.                         score = 0; //score resets
  204.                         nyanPos.X = 0;
  205.                         nyanPos.Y = 0; //reset your position too
  206.                     }
  207.                 }
  208.             }
  209.  
  210.  
  211.             base.Update(gameTime);
  212.         }
  213.  
  214.         /// <summary>
  215.         /// This is called when the game should draw itself.
  216.         /// </summary>
  217.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  218.         protected override void Draw(GameTime gameTime)
  219.         {
  220.             GraphicsDevice.Clear(Color.Black);
  221.  
  222.             // TODO: Add your drawing code here
  223.  
  224.             switch (gameState)
  225.             {
  226.                 //draw menu
  227.                 case 0:
  228.                     spriteBatch.Begin();
  229.                     spriteBatch.DrawString(font, "CHEEEEEN!\nArrows to move\nPress Space to Play ",
  230.                         new Vector2(100, 10), Color.White);
  231.                     spriteBatch.End();
  232.                     break;
  233.  
  234.                 //draw main game
  235.                 case 1:
  236.  
  237.                     spriteBatch.Begin();
  238.                     spriteBatch.Draw(nyanTex, nyanPos, Color.White);
  239.  
  240.                     //draw the chen army
  241.                     for (int i = 0; i < chenPopulation; i++)
  242.                     {
  243.                         spriteBatch.Draw(chenTex, chen[i], Color.White);
  244.                     }
  245.  
  246.                     spriteBatch.DrawString(font, "Score: " + score, new Vector2(20, 10), Color.White);
  247.  
  248.                     spriteBatch.End();
  249.                     break;
  250.             }
  251.  
  252.             base.Draw(gameTime);
  253.         }
  254.     }
  255. }