2 using System.Collections.Generic;
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 using Microsoft.Xna.Framework.Net;
12 using Microsoft.Xna.Framework.Storage;
17 /// This is the main type for your game
19 public class Game1 : Microsoft.Xna.Framework.Game
21 GraphicsDeviceManager graphics;
22 SpriteBatch spriteBatch;
28 Vector2 backgroundPos;
30 Vector2 spotLightCenter;
34 Vector2 spotLightVelocity;
43 graphics = new GraphicsDeviceManager(this);
44 Content.RootDirectory = "Content";
48 /// Allows the game to perform any initialization it needs to before starting to run.
49 /// This is where it can query for any required services and load any non-graphic
50 /// related content. Calling base.Initialize will enumerate through any components
51 /// and initialize them as well.
53 protected override void Initialize()
55 // TODO: Add your initialization logic here
56 graphics.PreferredBackBufferWidth = 800;
57 graphics.PreferredBackBufferWidth = 600;
62 /// LoadContent will be called once per game and is the place to load
63 /// all of your content.
65 protected override void LoadContent()
67 // Create a new SpriteBatch, which can be used to draw textures.
68 spriteBatch = new SpriteBatch(GraphicsDevice);
69 background = Content.Load<Texture2D>("background");
70 spotLight = Content.Load<Texture2D>("spotlight");
71 cs = Content.Load<Texture2D>("cs");
72 backgroundPos = new Vector2(0f, 0f);
73 spotLightPos = new Vector2(100f, graphics.GraphicsDevice.Viewport.Height - 98);
74 spotLightCenter = new Vector2(800f, 800f);
75 spotLightVelocity = new Vector2(-100, 33);
76 csPos = new Vector2(10f, graphics.GraphicsDevice.Viewport.Height - 98);
78 zero = new Vector2(0, 0);
80 MaxX = graphics.GraphicsDevice.Viewport.Width;
82 MaxY = graphics.GraphicsDevice.Viewport.Height;
84 // TODO: use this.Content to load your game content here
88 /// UnloadContent will be called once per game and is the place to unload
91 protected override void UnloadContent()
93 // TODO: Unload any non ContentManager content here
97 /// Allows the game to run logic such as updating the world,
98 /// checking for collisions, gathering input, and playing audio.
100 /// <param name="gameTime">Provides a snapshot of timing values.</param>
101 protected override void Update(GameTime gameTime)
103 // Allows the game to exit
104 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
107 // TODO: Add your update logic here
108 UpdateSpotLight(gameTime);
109 base.Update(gameTime);
113 /// This is called when the game should draw itself.
115 /// <param name="gameTime">Provides a snapshot of timing values.</param>
116 protected override void Draw(GameTime gameTime)
118 GraphicsDevice.Clear(Color.CornflowerBlue);
119 spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
120 // TODO: Add your drawing code here
121 spriteBatch.Draw(background, backgroundPos, null, Color.White, 0, zero, 0.5f, SpriteEffects.None, 0);
122 spriteBatch.Draw(cs, csPos, null, Color.White, 0, zero, 0.5f, SpriteEffects.None, 0);
123 spriteBatch.Draw(spotLight, spotLightPos, null, Color.White, 0, spotLightCenter, 1f, SpriteEffects.None, 0);
129 private void UpdateSpotLight(GameTime gameTime)
131 spotLightPos = new Vector2(spotLightPos.X + spotLightVelocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds,
132 spotLightPos.Y + spotLightVelocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds);
134 if (spotLightPos.X > MaxX || spotLightPos.X < MinX) //right or left wall
136 spotLightVelocity = new Vector2(spotLightVelocity.X * -1, spotLightVelocity.Y);
138 else if (spotLightPos.Y > MaxY || spotLightPos.Y < MinY) //top or bottom wall
140 spotLightVelocity = new Vector2(spotLightVelocity.X, spotLightVelocity.Y * -1);