Created
May 14, 2012 19:53
-
-
Save jcolebrand/2696169 to your computer and use it in GitHub Desktop.
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 QBert | |
{ | |
/// <summary> | |
/// This is the main type for your game | |
/// </summary> | |
public class Game1 : Microsoft.Xna.Framework.Game | |
{ | |
GraphicsDeviceManager graphics; | |
SpriteBatch spriteBatch; | |
/* | |
0 0 0 X 0 0 0 | |
0 0 X X 0 0 0 | |
0 0 X X X 0 0 | |
0 | |
*/ | |
public int[,] board; | |
Texture2D block; | |
Texture2D blocktop; | |
bool moveTrigger = false; | |
Sprite piggy; | |
Vector2 playerPosition = new Vector2(3, 0); | |
public Game1() | |
{ | |
graphics = new GraphicsDeviceManager(this); | |
graphics.PreferredBackBufferWidth = 1024; //1024 | |
graphics.PreferredBackBufferHeight = 768; //768 | |
this.Window.AllowUserResizing = true; | |
graphics.ApplyChanges(); | |
this.Window.AllowUserResizing = true; | |
Content.RootDirectory = "Content"; | |
board = new int[7, 7] | |
{ | |
{ 0, 0, 0, 1, 0, 0, 0 }, | |
{ 0, 0, 1, 1, 0, 0, 0 }, | |
{ 0, 0, 1, 1, 1, 0, 0 }, | |
{ 0, 1, 1, 1, 1, 0, 0 }, | |
{ 0, 1, 1, 1, 1, 1, 0 }, | |
{ 1, 1, 1, 1, 1, 1, 0 }, | |
{ 1, 1, 1, 1, 1, 1, 1 } | |
} | |
; | |
} | |
/// <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); | |
// TODO: use this.Content to load your game content here | |
block = Content.Load<Texture2D>(@"block"); | |
blocktop = Content.Load<Texture2D>(@"block_top"); | |
Vector2 playerPosition = new Vector2(3, 0); | |
piggy = new Sprite(GetSquareCenter(3, 0), Content.Load<Texture2D>(@"piggy"), new Rectangle(4, 7, 45, 73), Vector2.Zero); | |
} | |
/// <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 | |
} | |
protected Vector2 GetSquareCoords(int x, int y) | |
{ | |
int ofs = block.Width / 2; | |
ofs *= y % 2; | |
return new Vector2(x * block.Width + ofs, y * 96); | |
} | |
protected Vector2 GetSquareCenter(int x, int y) | |
{ | |
Vector2 coords = GetSquareCoords(x, y); | |
return new Vector2(coords.X + block.Width/2, coords.Y + 32); | |
} | |
protected Vector2 GetNextSquare(bool down, bool left, Vector2 position) | |
{ | |
// If on even row, right is directly below and left is below and to the left | |
// If on odd row, left is directly below and right is below and to the right | |
int next_x = 0, next_y = 0; | |
int x = (int)position.X; | |
int y = (int)position.Y; | |
if (down) | |
{ | |
next_y = y + 1; | |
} | |
else | |
{ | |
next_y = y - 1; | |
} | |
if (y % 2 == 0) | |
{ | |
if (left) | |
next_x = x - 1; | |
else | |
next_x = x; | |
} | |
else | |
{ | |
if (left) | |
next_x = x; | |
else | |
next_x = x + 1; | |
} | |
return new Vector2(next_x, next_y); | |
} | |
/// <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(); | |
// TODO: Add your update logic here | |
KeyboardState kb = Keyboard.GetState(); | |
if (kb.IsKeyDown(Keys.Right) && !moveTrigger) | |
{ | |
moveTrigger = true; | |
playerPosition = GetNextSquare(true, false, playerPosition); | |
piggy.Location = GetSquareCenter((int)playerPosition.X, (int)playerPosition.Y); | |
} | |
if (kb.IsKeyDown(Keys.Left) && !moveTrigger) | |
{ | |
moveTrigger = true; | |
playerPosition = GetNextSquare(true, true, playerPosition); | |
piggy.Location = GetSquareCenter((int)playerPosition.X, (int)playerPosition.Y); | |
} | |
if (kb.GetPressedKeys().Length == 0) | |
moveTrigger = false; | |
if (board[(int)playerPosition.Y, (int)playerPosition.X] < blockColors.Length) | |
board[(int)playerPosition.Y, (int)playerPosition.X] += 1; | |
base.Update(gameTime); | |
} | |
Color[] blockColors = new Color[] { Color.Blue, Color.Red }; | |
/// <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) | |
{ | |
GraphicsDevice.Clear(Color.Black); | |
int drawXOffset = 30; | |
int drawYOffset = 60; | |
spriteBatch.Begin(); | |
for (int x = 0; x < 7; x++) | |
for (int y = 0; y < 7; y++) | |
{ | |
Vector2 coord = GetSquareCoords(x, y); | |
if(board[y, x] > 0) | |
spriteBatch.Draw(block, new Rectangle(drawXOffset + (int)coord.X, drawYOffset + (int)coord.Y, block.Width, block.Height), blockColors[board[y, x] - 1]); | |
//if (board[y,x] == 1) | |
// spriteBatch.Draw(block, new Rectangle(drawXOffset + (int)coord.X, drawYOffset + (int)coord.Y, block.Width, block.Height), Color.White); | |
} | |
piggy.Draw(spriteBatch); | |
spriteBatch.End(); | |
base.Draw(gameTime); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment