SHARE
TWEET

Starry Night

SuperLemrick Mar 23rd, 2015 203 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////////////
  2. // Copyright 2013, CompuScholar, Inc.
  3. //
  4. // This source code is for use by the students and teachers who
  5. // have purchased the corresponding TeenCoder or KidCoder product.
  6. // It may not be transmitted to other parties for any reason
  7. // without the written consent of CompuScholar, Inc.
  8. // This source is provided as-is for educational purposes only.
  9. // CompuScholar, Inc. makes no warranty and assumes
  10. // no liability regarding the functionality of this program.
  11. //
  12. ////////////////////////////////////////////////////////////////
  13.  
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using Microsoft.Xna.Framework;
  18. using Microsoft.Xna.Framework.Audio;
  19. using Microsoft.Xna.Framework.Content;
  20. using Microsoft.Xna.Framework.GamerServices;
  21. using Microsoft.Xna.Framework.Graphics;
  22. using Microsoft.Xna.Framework.Input;
  23. using Microsoft.Xna.Framework.Media;
  24. using Microsoft.Xna.Framework.Net;
  25. using Microsoft.Xna.Framework.Storage;
  26.  
  27. namespace StarryNight
  28. {
  29.     /// <summary>
  30.     /// This is the main type for your game
  31.     /// </summary>
  32.     public class StarryNight : Microsoft.Xna.Framework.Game
  33.     {
  34.         // all game variables are provided complete in the Activity Starter
  35.  
  36.         GraphicsDeviceManager graphics;
  37.         SpriteBatch spriteBatch;
  38.  
  39.         Texture2D pixelTexture; // this will hold a single pixel texture
  40.         Texture2D treeTexture;  // this will hold the image of a tree
  41.  
  42.         float starfieldRotationAngle;    // current rotation starfieldRotationAngle of the starfield
  43.         const int NUM_STARS = 300;  // how many stars do we display?
  44.  
  45.         // this array of vectors holds the original (unrotated) star positions
  46.         Vector2[] starLocations = new Vector2[NUM_STARS];
  47.  
  48.         // this array of vectors holds the star rotation origins
  49.         Vector2[] starOrigins = new Vector2[NUM_STARS];
  50.  
  51.         // this will make the stars more visible
  52.         Vector2 starScale = new Vector2(3.0f, 3.0f);
  53.  
  54.         // this vector will position the tree
  55.         Vector2 treeLocation;
  56.  
  57.         // these variables will store the screen width and height for other calculations
  58.         int screenHeight = 0;
  59.         int screenWidth = 0;
  60.  
  61.         public StarryNight()
  62.         {
  63.             graphics = new GraphicsDeviceManager(this);
  64.             Content.RootDirectory = "Content";
  65.         }
  66.  
  67.         /// <summary>
  68.         /// Allows the game to perform any initialization it needs to before starting to run.
  69.         /// This is where it can query for any required services and load any non-graphic
  70.         /// related content.  Calling base.Initialize will enumerate through any components
  71.         /// and initialize them as well.
  72.         /// </summary>
  73.         // This method is provided complete in the Activity Starter
  74.         protected override void Initialize()
  75.         {
  76.             // initialize screen width and height
  77.             screenHeight = GraphicsDevice.Viewport.Height;
  78.             screenWidth = GraphicsDevice.Viewport.Width;
  79.  
  80.             // posiiton the tree to the left just above the horizon
  81.             treeLocation = new Vector2(screenWidth / 4, (screenHeight / 2) - 130);
  82.  
  83.             // initialize a new random number generator
  84.             System.Random random = new System.Random();
  85.  
  86.             // determine the rotation origin as the middle of the screen
  87.             Vector2 rotateOrigin = new Vector2(screenWidth / 2, screenHeight / 2);
  88.  
  89.             // now distribute the stars over an area that will cover the screen plus some on the outside,
  90.             // so that the rotation won't leave any gaps due to the non-circular screen.
  91.            
  92.             // distance from center of screen to upper left corner
  93.             int radius = (int)Math.Sqrt(rotateOrigin.X * rotateOrigin.X + rotateOrigin.Y * rotateOrigin.Y);
  94.  
  95.             // for each star
  96.             for (int i = 0; i < NUM_STARS; i++)
  97.             {
  98.                 // create a random position on the screen by generating a random X and Y number
  99.                 // from -radius to +radius
  100.                 int randomX = random.Next(-radius, radius);
  101.                 int randomY = random.Next(-radius, radius);
  102.  
  103.                 // star location is the random value plus the rotation origin
  104.                 starLocations[i] = new Vector2(randomX + rotateOrigin.X, randomY + rotateOrigin.Y);
  105.  
  106.                 // calculate the rotation origin for this star so it rotates around the middle of the screen
  107.                 starOrigins[i] = rotateOrigin - starLocations[i];
  108.             }
  109.  
  110.             base.Initialize();
  111.         }
  112.  
  113.         /// <summary>
  114.         /// LoadContent will be called once per game and is the place to load
  115.         /// all of your content.
  116.         /// </summary>
  117.         /// Student will complete this method as part of the Chapter 4 Activity
  118.         protected override void LoadContent()
  119.         {
  120.             // Create a new SpriteBatch, which can be used to draw textures.
  121.             spriteBatch = new SpriteBatch(GraphicsDevice);
  122.             pixelTexture = Content.Load<Texture2D>("pixel");
  123.  
  124.             treeTexture = Content.Load<Texture2D>("tree");
  125.            
  126.         }
  127.  
  128.         /// <summary>
  129.         /// UnloadContent will be called once per game and is the place to unload
  130.         /// all content.
  131.         /// </summary>
  132.         // This method is provided complete in the Activity Starter
  133.         protected override void UnloadContent()
  134.         {
  135.             // TODO: Unload any non ContentManager content here
  136.         }
  137.  
  138.         /// <summary>
  139.         /// Allows the game to run logic such as updating the world,
  140.         /// checking for collisions, gathering input, and playing audio.
  141.         /// </summary>
  142.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  143.         // This method is provided complete in the Activity Starter
  144.         protected override void Update(GameTime gameTime)
  145.         {
  146.  
  147.             // calculate some constants
  148.             const float CIRCLE_RADIANS = MathHelper.Pi * 2.0F;
  149.             const float DEGREE_IN_RADIANS = CIRCLE_RADIANS / 360.0F;
  150.  
  151.             // Let's adjust the starfieldRotationAngle by enough to accomplish a full rotation every 20 seconds.
  152.             // Since Update() is called 60 times a second, we will have 1200 calls to update for 360 degree change.
  153.             // Therefore each call to update should change the starfieldRotationAngle by .3 degrees.
  154.  
  155.             starfieldRotationAngle += 0.3f * DEGREE_IN_RADIANS;
  156.  
  157.             // wrap starfieldRotationAngle around when we come full circle
  158.             if (starfieldRotationAngle >= CIRCLE_RADIANS)    // if we are greater than 360 degrees
  159.                 starfieldRotationAngle -= CIRCLE_RADIANS;    // wrap around
  160.  
  161.             base.Update(gameTime);
  162.         }
  163.  
  164.         /// <summary>
  165.         /// This is called when the game should draw itself.
  166.         /// </summary>
  167.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  168.         /// Student will complete this method as part of Chapter 4 Activity
  169.         protected override void Draw(GameTime gameTime)
  170.         {
  171.             GraphicsDevice.Clear(Color.CornflowerBlue);
  172.             GraphicsDevice.Clear(Color.DarkBlue);
  173.  
  174.             spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
  175.  
  176.             spriteBatch.Draw(pixelTexture, new Vector2(0, screenHeight / 2), null, Color.LightGreen, 0.0F, new Vector2(0, 0), new Vector2(screenWidth, screenHeight / 2), SpriteEffects.None, 0.5f);
  177.  
  178.             for (int i = 0; i < NUM_STARS; i++)
  179.             {
  180.                 spriteBatch.Draw(pixelTexture, starLocations[i] + starOrigins[i], null, Color.White, -starfieldRotationAngle, starOrigins[i] / starScale, starScale, SpriteEffects.None, 1.0F);
  181.             }
  182.  
  183.             spriteBatch.Draw(treeTexture, treeLocation, null, Color.White, 0.0F, new Vector2(0, 0), new Vector2(1.0f, 1.0f), SpriteEffects.None, 0.0F);
  184.  
  185.             spriteBatch.End();
  186.  
  187.             base.Draw(gameTime);
  188.         }
  189.     }
  190. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 60% OFF on Pastebin PRO accounts!
Top