-
-
Save anonymous/e1f08d873f6216a48b59 to your computer and use it in GitHub Desktop.
A game I am working on in my class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
namespace Scrolling.cs | |
{ | |
/// <summary> | |
/// This is the main type for your game | |
/// </summary> | |
public class Game1 : Microsoft.Xna.Framework.Game | |
{ | |
enum GameStates { TitleScreen, Playing, PlayerCrash, GameOver }; | |
GameStates gameState = GameStates.TitleScreen; | |
Texture2D cone; | |
Texture2D titlescreen; | |
Texture2D background; | |
Texture2D car; | |
GraphicsDeviceManager graphics; | |
SpriteBatch spriteBatch; | |
private float titleScreenTimer = 0f; | |
private float titleScreenDelayTime = 1f; | |
Vector2 coneVelocity = new Vector2(0, 1); | |
Vector2 location = new Vector2(0, 0); | |
Vector2 stands = new Vector2(0, -1); | |
Vector2 carlocation = new Vector2(30, 400); | |
public List<Vector2> conePositions; | |
public Game1() | |
{ | |
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> | |
protected override void Initialize() | |
{ | |
// TODO: Add your initialization logic here | |
base.Initialize(); | |
} | |
/// <summary> | |
/// LoadContent will be called once per game and is the place to load | |
/// all of your content. | |
/// </summary> | |
protected override void LoadContent() | |
{ | |
// Create a new SpriteBatch, which can be used to draw textures. | |
spriteBatch = new SpriteBatch(GraphicsDevice); | |
cone = Content.Load<Texture2D>(@"Obstacle"); | |
titlescreen = Content.Load<Texture2D>(@"titlescreen"); | |
background = Content.Load<Texture2D>(@"background"); | |
car = Content.Load<Texture2D>(@"car"); | |
conePositions = new List<Vector2>(); | |
conePositions.Add(new Vector2(1300, 1)); | |
conePositions.Add(new Vector2(1600, 0)); | |
conePositions.Add(new Vector2(1900, 1)); | |
// TODO: use this.Content to load your game content here | |
} | |
/// <summary> | |
/// UnloadContent will be called once per game and is the place to unload | |
/// all content. | |
/// </summary> | |
protected override void UnloadContent() | |
{ | |
// TODO: Unload any non ContentManager content here | |
} | |
private void ResetGame() | |
{ | |
carlocation.X = 30; | |
carlocation.Y = 400; | |
} | |
/// <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> | |
protected override void Update(GameTime gameTime) | |
{ | |
// Allows the game to exit | |
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) | |
this.Exit(); | |
if (gameState == GameStates.Playing) | |
{ | |
location.X += (float)gameTime.ElapsedGameTime.TotalSeconds * 700f; | |
stands.X += (float)gameTime.ElapsedGameTime.TotalSeconds * 100f; | |
} | |
//conePosition.X += (float)gameTime.ElapsedGameTime.TotalSeconds * -325f; | |
// Check for collisions | |
foreach (Vector2 conePosition in conePositions) | |
{ | |
Vector2 conePos = conePosition - location; | |
conePos.Y = 330 + conePos.Y * 60; | |
if (Vector2.Distance(conePos, carlocation) < 50) | |
{ | |
// Collision! | |
ResetGame(); | |
} | |
} | |
KeyboardState ks = Keyboard.GetState(); | |
if (ks.IsKeyDown(Keys.Up)) | |
{ | |
carlocation.Y = 330; | |
} | |
if (ks.IsKeyDown(Keys.Down)) | |
{ | |
carlocation.Y = 390; | |
} | |
base.Update(gameTime); | |
} | |
public void DrawBackground(Texture2D bkg, int xofs) | |
{ | |
xofs = xofs % bkg.Width; | |
// Calculate which two source / destination rectangles to use based off of xofs and this.Window.ClientBounds.Width | |
spriteBatch.Draw(background, new Rectangle(0, 0, bkg.Width - xofs, this.Window.ClientBounds.Height), new Rectangle(xofs, 0, bkg.Width - xofs, bkg.Height), Color.White); | |
spriteBatch.Draw(background, new Rectangle(this.Window.ClientBounds.Width - xofs - 8, 0, xofs, this.Window.ClientBounds.Height), new Rectangle(0, 0, xofs, bkg.Height), Color.White); | |
// Draw stands | |
xofs = (int)stands.X % bkg.Width; | |
spriteBatch.Draw(background, new Rectangle(0, 0, bkg.Width - xofs, 290), new Rectangle(xofs, 0, bkg.Width - xofs, 349), Color.White); | |
spriteBatch.Draw(background, new Rectangle(this.Window.ClientBounds.Width - xofs - 8, 0, xofs, 290), new Rectangle(0, 0, xofs, 349), Color.White); | |
// Draw cone | |
} | |
/// <summary> | |
/// This is called when the game should draw itself. | |
/// </summary> | |
/// <param name="gameTime">Provides a snapshot of timing values.</param> | |
protected override void Draw(GameTime gameTime) | |
{ | |
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) | |
this.Exit(); | |
switch (gameState) | |
{ | |
case GameStates.TitleScreen: | |
titleScreenTimer += | |
(float)gameTime.ElapsedGameTime.TotalSeconds; | |
if (titleScreenTimer >= titleScreenDelayTime) | |
{ | |
if ((Keyboard.GetState().IsKeyDown(Keys.Space)) || | |
(GamePad.GetState(PlayerIndex.One).Buttons.A == | |
ButtonState.Pressed)) | |
{ | |
gameState = GameStates.Playing; | |
} | |
} | |
break; | |
} | |
GraphicsDevice.Clear(Color.CornflowerBlue); | |
spriteBatch.Begin(); | |
if (gameState == GameStates.Playing) | |
{ | |
//spriteBatch.Draw(background, new Rectangle(0, 0, this.Window.ClientBounds.Width, this.Window.ClientBounds.Height), Color.White); | |
DrawBackground(background, (int)location.X); | |
spriteBatch.Draw(car, carlocation, Color.White); | |
foreach (Vector2 conePosition in conePositions) | |
{ | |
if (conePosition.X >= location.X && conePosition.X <= location.X + this.Window.ClientBounds.Width) | |
{ | |
spriteBatch.Draw(cone, conePosition - new Vector2(location.X, 0) + new Vector2(0, 325 + conePosition.Y * 50), Color.White); | |
} | |
} | |
} | |
if (gameState == GameStates.TitleScreen) | |
{ | |
spriteBatch.Draw(titlescreen, | |
new Rectangle(0, 0, this.Window.ClientBounds.Width, | |
this.Window.ClientBounds.Height), | |
Color.White); | |
} | |
spriteBatch.End(); | |
// TODO: Add your drawing code here | |
base.Draw(gameTime); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment