Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////
- // Copyright 2013, CompuScholar, Inc.
- //
- // This source code is for use by the students and teachers who
- // have purchased the corresponding TeenCoder or KidCoder product.
- // It may not be transmitted to other parties for any reason
- // without the written consent of CompuScholar, Inc.
- // This source is provided as-is for educational purposes only.
- // CompuScholar, Inc. makes no warranty and assumes
- // no liability regarding the functionality of this program.
- //
- ////////////////////////////////////////////////////////////////
- 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;
- using Microsoft.Xna.Framework.Net;
- using Microsoft.Xna.Framework.Storage;
- namespace StarryNight
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class StarryNight : Microsoft.Xna.Framework.Game
- {
- // all game variables are provided complete in the Activity Starter
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D pixelTexture; // this will hold a single pixel texture
- Texture2D treeTexture; // this will hold the image of a tree
- float starfieldRotationAngle; // current rotation starfieldRotationAngle of the starfield
- const int NUM_STARS = 300; // how many stars do we display?
- // this array of vectors holds the original (unrotated) star positions
- Vector2[] starLocations = new Vector2[NUM_STARS];
- // this array of vectors holds the star rotation origins
- Vector2[] starOrigins = new Vector2[NUM_STARS];
- // this will make the stars more visible
- Vector2 starScale = new Vector2(3.0f, 3.0f);
- // this vector will position the tree
- Vector2 treeLocation;
- // these variables will store the screen width and height for other calculations
- int screenHeight = 0;
- int screenWidth = 0;
- public StarryNight()
- {
- 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>
- // This method is provided complete in the Activity Starter
- protected override void Initialize()
- {
- // initialize screen width and height
- screenHeight = GraphicsDevice.Viewport.Height;
- screenWidth = GraphicsDevice.Viewport.Width;
- // posiiton the tree to the left just above the horizon
- treeLocation = new Vector2(screenWidth / 4, (screenHeight / 2) - 130);
- // initialize a new random number generator
- System.Random random = new System.Random();
- // determine the rotation origin as the middle of the screen
- Vector2 rotateOrigin = new Vector2(screenWidth / 2, screenHeight / 2);
- // now distribute the stars over an area that will cover the screen plus some on the outside,
- // so that the rotation won't leave any gaps due to the non-circular screen.
- // distance from center of screen to upper left corner
- int radius = (int)Math.Sqrt(rotateOrigin.X * rotateOrigin.X + rotateOrigin.Y * rotateOrigin.Y);
- // for each star
- for (int i = 0; i < NUM_STARS; i++)
- {
- // create a random position on the screen by generating a random X and Y number
- // from -radius to +radius
- int randomX = random.Next(-radius, radius);
- int randomY = random.Next(-radius, radius);
- // star location is the random value plus the rotation origin
- starLocations[i] = new Vector2(randomX + rotateOrigin.X, randomY + rotateOrigin.Y);
- // calculate the rotation origin for this star so it rotates around the middle of the screen
- starOrigins[i] = rotateOrigin - starLocations[i];
- }
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- /// Student will complete this method as part of the Chapter 4 Activity
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- pixelTexture = Content.Load<Texture2D>("pixel");
- treeTexture = Content.Load<Texture2D>("tree");
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- // This method is provided complete in the Activity Starter
- 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>
- // This method is provided complete in the Activity Starter
- protected override void Update(GameTime gameTime)
- {
- // calculate some constants
- const float CIRCLE_RADIANS = MathHelper.Pi * 2.0F;
- const float DEGREE_IN_RADIANS = CIRCLE_RADIANS / 360.0F;
- // Let's adjust the starfieldRotationAngle by enough to accomplish a full rotation every 20 seconds.
- // Since Update() is called 60 times a second, we will have 1200 calls to update for 360 degree change.
- // Therefore each call to update should change the starfieldRotationAngle by .3 degrees.
- starfieldRotationAngle += 0.3f * DEGREE_IN_RADIANS;
- // wrap starfieldRotationAngle around when we come full circle
- if (starfieldRotationAngle >= CIRCLE_RADIANS) // if we are greater than 360 degrees
- starfieldRotationAngle -= CIRCLE_RADIANS; // wrap around
- base.Update(gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- /// Student will complete this method as part of Chapter 4 Activity
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- GraphicsDevice.Clear(Color.DarkBlue);
- spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
- 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);
- for (int i = 0; i < NUM_STARS; i++)
- {
- spriteBatch.Draw(pixelTexture, starLocations[i] + starOrigins[i], null, Color.White, -starfieldRotationAngle, starOrigins[i] / starScale, starScale, SpriteEffects.None, 1.0F);
- }
- spriteBatch.Draw(treeTexture, treeLocation, null, Color.White, 0.0F, new Vector2(0, 0), new Vector2(1.0f, 1.0f), SpriteEffects.None, 0.0F);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement