Skip to content

Instantly share code, notes, and snippets.

@BlockAfterBlock
Created June 12, 2011 21:12
Show Gist options
  • Save BlockAfterBlock/1021989 to your computer and use it in GitHub Desktop.
Save BlockAfterBlock/1021989 to your computer and use it in GitHub Desktop.
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 FallingUp
{
public class Main : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState keyboardState;
Texture2D textPlayer;
Texture2D platform;
Texture2D side;
Rectangle rectPlayer;
Scrolling scrolling1;
Scrolling scrolling2;
SpriteFont font1;
SpriteFont fontBig1;
SpriteFont fontBig2;
public static bool hasStarted = false;
const int screenWidth = 1080;
const int screenHeight = 720;
double score;
//int score;
public Main()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = screenWidth;
graphics.PreferredBackBufferHeight = screenHeight;
}
static class Positions
{
public static Vector2 player = Vector2.Zero;
public static Vector2 side = new Vector2(0f, 0f);
public static Vector2 center = new Vector2(screenWidth - 360 - 64 / 2 , screenHeight / 2);
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
textPlayer = Content.Load<Texture2D>("Images//Player");
rectPlayer = new Rectangle(graphics.GraphicsDevice.Viewport.Width / 2 - textPlayer.Width / 2, graphics.GraphicsDevice.Viewport.Height - textPlayer.Height, textPlayer.Width, textPlayer.Height);
side = Content.Load<Texture2D>("Images//Side");
Positions.player.X = graphics.GraphicsDevice.Viewport.Width - 360 - textPlayer.Width / 2;
Positions.player.Y = graphics.GraphicsDevice.Viewport.Height - 180;
scrolling1 = new Scrolling(Content.Load<Texture2D>("Images//Background1"), new Rectangle(360, 0, 720, 720));
scrolling2 = new Scrolling(Content.Load<Texture2D>("Images//Background2"), new Rectangle(360, 720, 720, 720));
font1 = Content.Load<SpriteFont>("Font1");
fontBig1 = Content.Load<SpriteFont>("FontBig1");
fontBig2 = Content.Load<SpriteFont>("FontBig2");
}
protected override void UnloadContent()
{
}
double vi, t = 0;
double g = -1;
int keyState = 0;
protected override void Update(GameTime gameTime)
{
if (hasStarted == true)
score = gameTime.ElapsedGameTime.TotalSeconds;
#region Floating
/*
if (Keyboard.GetState().IsKeyDown(Keys.Space))
{
keyState = 1;
vi = -100;
}
if(keyState == 1)
{
Positions.player.Y = (float)(vi * t + g * t * t / 2) + graphics.GraphicsDevice.Viewport.Height - textPlayer.Height;
t = t + gameTime.ElapsedGameTime.TotalSeconds;
}
if (Positions.player.Y > graphics.GraphicsDevice.Viewport.Height - textPlayer.Height)
{
Positions.player.Y = graphics.GraphicsDevice.Viewport.Height - textPlayer.Height;
keyState = 0;
t = 0;
}
*/
#endregion
#region Controls
keyboardState = Keyboard.GetState();
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
if (keyboardState.IsKeyDown(Keys.Space))
hasStarted = true;
if (hasStarted == true)
{
if (keyboardState.IsKeyDown(Keys.A))
Positions.player.X -= 6;
if (keyboardState.IsKeyDown(Keys.D))
Positions.player.X += 6;
if (keyboardState.IsKeyDown(Keys.Left) && keyboardState.IsKeyUp(Keys.A))
Positions.player.X -= 6;
if (keyboardState.IsKeyDown(Keys.Right) && keyboardState.IsKeyUp(Keys.D))
Positions.player.X += 6;
}
#endregion
#region Boundries
if (Positions.player.X < 360)
Positions.player.X = 360;
if (Positions.player.X > graphics.PreferredBackBufferWidth - textPlayer.Width)
Positions.player.X = graphics.PreferredBackBufferWidth - textPlayer.Width;
#endregion
#region Scrolling Background
if (scrolling1.bgRect.Y + scrolling1.bg.Width <= 0)
scrolling1.bgRect.Y = scrolling2.bgRect.Y + scrolling2.bg.Width;
if (scrolling2.bgRect.Y + scrolling2.bg.Width <= 0)
scrolling2.bgRect.Y = scrolling1.bgRect.Y + scrolling1.bg.Width;
scrolling1.Update();
scrolling2.Update();
#endregion
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
scrolling1.Draw(spriteBatch);
scrolling2.Draw(spriteBatch);
spriteBatch.Draw(textPlayer, Positions.player, Color.White);
spriteBatch.Draw(side, Positions.side, Color.White);
if (hasStarted == false)
spriteBatch.DrawString(font1, "Press 'Space' to play!", new Vector2(490, screenHeight / 2), Color.White);
spriteBatch.DrawString(fontBig1, "Falling", new Vector2(10, 0), Color.White);
spriteBatch.DrawString(fontBig2, "Up", new Vector2(60, 100), Color.White);
spriteBatch.DrawString(font1, "Score: " + score.ToString(), new Vector2(50, 360), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment