Created
May 17, 2012 22:45
-
-
Save anonymous/2722108 to your computer and use it in GitHub Desktop.
QBert
This file contains hidden or 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 | |
{ | |
public class Actor : Sprite | |
{ | |
private Queue<Vector2> waypoints = new Queue<Vector2>(); | |
private Vector2 currentWaypoint = Vector2.Zero; | |
public float minJumpTime = 1000f; | |
private float speed = 190f; | |
private float curspeed = 190f; | |
private float accel = 0f; | |
private bool down = true; | |
public bool Destroyed = false; | |
public bool isJumping = false; | |
private int actorRadius = 15; | |
private Vector2 previousLocation = Vector2.Zero; | |
public event EventHandler OnMoveFinish; | |
public Vector2 boardPosition = Vector2.Zero; | |
public float elapsedTime = 0; | |
public Actor( | |
Vector2 location, | |
Texture2D texture, | |
Rectangle initialFrame, | |
Vector2 velocity, | |
Vector2 boardPosition) | |
: base(location, texture, initialFrame, velocity) | |
{ | |
this.boardPosition = boardPosition; | |
/* | |
this = new Sprite( | |
location, | |
texture, | |
initialFrame, | |
Vector2.Zero); | |
for (int x = 1; x < frameCount; x++) | |
{ | |
this.AddFrame( | |
new Rectangle( | |
initialFrame.X = (initialFrame.Width * x), | |
initialFrame.Y, | |
initialFrame.Width, | |
initialFrame.Height)); | |
} | |
*/ | |
previousLocation = location; | |
currentWaypoint = location; | |
this.CollisionRadius = actorRadius; | |
} | |
public float Speed | |
{ | |
get { return speed; } | |
set { speed = value; curspeed = speed; } | |
} | |
private void doOnMoveFinish() | |
{ | |
SetDirection(down); | |
if (OnMoveFinish != null) | |
this.OnMoveFinish(this, new EventArgs()); | |
} | |
public void SetDirection(bool godown) | |
{ | |
down = godown; | |
accel = 0; | |
if (down) | |
curspeed = speed; | |
else | |
curspeed = 350; | |
} | |
public void AddWaypoint(Vector2 waypoint) | |
{ | |
waypoints.Enqueue(waypoint); | |
} | |
public void ClearWaypoints() | |
{ | |
waypoints.Clear(); | |
} | |
public bool WaypointReached() | |
{ | |
if (Vector2.Distance(this.Location, currentWaypoint) < | |
(float)this.Source.Width / 6) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
public bool IsActive() | |
{ | |
if (Destroyed) | |
{ | |
return false; | |
} | |
if (waypoints.Count > 0) | |
{ | |
return true; | |
} | |
if (WaypointReached()) | |
{ | |
return false; | |
} | |
return true; | |
} | |
public void AddWayPoints(Actor m, Vector2 start, Vector2 end) | |
{ | |
List<Vector2> waypoints = new List<Vector2>(); | |
if (m.isJumping) | |
return; | |
bool flip = start.Y < end.Y ? false : true; | |
m.SetDirection(!flip); | |
Vector2 v1 = flip ? end : start; | |
Vector2 v2 = flip ? start : end; | |
float height = (v1.Y - v2.Y); | |
height = height < 0 ? height * -1 : height; | |
waypoints.Add(v1); | |
float diff = (v2.X - v1.X); | |
for (int i = 5; i < (int)height; i += 5) | |
{ | |
int step = i; | |
float progress = (float)step / height; | |
float curve = (float)Math.Sin((double)progress * MathHelper.PiOver2) * diff; | |
float curveY = (float)Math.Sin((double)progress - 0.3f) * (v2.Y - v1.Y); | |
if (curveY > v2.Y) | |
break; | |
waypoints.Add(new Vector2(v1.X + curve, v1.Y + curveY)); | |
} | |
waypoints.Add(v2); | |
if (flip) | |
waypoints.Reverse(); | |
for (int i = 1; i < waypoints.Count; i++) | |
{ | |
m.AddWaypoint(waypoints[i]); | |
} | |
} | |
public bool Jump(Map map, bool down, bool left) | |
{ | |
if (elapsedTime > minJumpTime && !isJumping) | |
{ | |
Vector2 start = Location; | |
boardPosition = map.GetNextSquare(down, left, boardPosition); | |
Vector2 end = map.GetSquareCenter((int)boardPosition.X, (int)boardPosition.Y); | |
elapsedTime = 0.0f; | |
AddWayPoints(this, start, end); | |
return true; | |
} | |
return false; | |
} | |
public override void Update(GameTime gameTime) | |
{ | |
elapsedTime += (float)gameTime.ElapsedGameTime.TotalMilliseconds; | |
isJumping = false; | |
if (waypoints.Count > 0) | |
isJumping = true; | |
if (IsActive()) | |
{ | |
Vector2 heading = currentWaypoint - this.Location; | |
if (heading != Vector2.Zero) | |
{ | |
heading.Normalize(); | |
} | |
heading *= curspeed; | |
this.Velocity = heading; | |
previousLocation = this.Location; | |
base.Update(gameTime); | |
if (WaypointReached()) | |
{ | |
if (waypoints.Count > 0) | |
{ | |
currentWaypoint = waypoints.Dequeue(); | |
if (!down) | |
accel -= 1; | |
else | |
accel += 1; | |
curspeed += accel; | |
} | |
else | |
{ | |
doOnMoveFinish(); | |
} | |
} | |
} | |
} | |
public override void Draw(SpriteBatch spriteBatch) | |
{ | |
if (!Destroyed) | |
{ | |
base.Draw(spriteBatch); | |
} | |
} | |
} | |
} |
This file contains hidden or 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 System.Text; | |
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 | |
{ | |
class Enemy : Actor | |
{ | |
public Sprite EnemySprite; | |
public Enemy( | |
Vector2 location, | |
Texture2D texture, | |
Rectangle initialFrame, | |
Vector2 velocity, | |
Vector2 boardPosition) | |
: base(location, texture, initialFrame, velocity, boardPosition) | |
{ | |
this.boardPosition = boardPosition; | |
EnemySprite = new Sprite( | |
location, | |
texture, | |
initialFrame, | |
Vector2.Zero); | |
} | |
public override void Update(GameTime gameTime) | |
{ | |
base.Update(gameTime); | |
} | |
} | |
} |
This file contains hidden or 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 System.Text; | |
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 | |
{ | |
class EnemyManager | |
{ | |
Map map; | |
PlayerManager playerManager; | |
public List<Enemy> Enemies; | |
Texture2D spriteSheet; | |
float totalElapsed = 0; | |
Random rand; | |
Enemy fox; | |
static int playerStartingLives = 3; | |
static int LivesRemaining = playerStartingLives; | |
public Actor player; | |
public EnemyManager(Map map, Texture2D spriteSheet, PlayerManager playerManager) | |
{ | |
rand = new Random(System.Environment.TickCount); | |
this.map = map; | |
this.spriteSheet = spriteSheet; | |
this.playerManager = playerManager; | |
Enemies = new List<Enemy>(); | |
fox = new Enemy(map.GetSquareCenter(3, 0), spriteSheet, new Rectangle(170, 250, 51, 68), Vector2.Zero, new Vector2(3, 0)); //drawing the fox seperately because its better | |
fox.Speed = 150f; | |
fox.OnMoveFinish += new EventHandler(fox_OnMoveFinish); | |
} | |
public void SpawnEnemy() | |
{ | |
Enemy android = new Enemy(map.GetSquareCenter(3, 0), spriteSheet, new Rectangle(4, 7, 45, 62), Vector2.Zero, new Vector2(3,0)); | |
android.Speed = 120f; | |
android.OnMoveFinish += new EventHandler(android_OnMoveFinish); | |
Enemies.Add(android); //android is a obstacle, it just gets in the way it moves to a random spot | |
} | |
void android_OnMoveFinish(object sender, EventArgs e) | |
{ | |
//throw new NotImplementedException(); | |
Enemy enemy = (Enemy)sender; | |
if ((int)enemy.boardPosition.Y >= 7) | |
{ | |
Enemies.Remove(enemy); | |
//this.SpawnEnemy(); | |
} | |
} | |
void fox_OnMoveFinish(object sender, EventArgs e) | |
{ | |
//throw new NotImplementedException(); | |
} | |
public void FoxFollow() | |
{ | |
bool down = true; | |
bool left = true; | |
// Based off of the fox boardposition and the player position, choose whether the fox should go up/down and left/right | |
// playerManager.playerPosition | |
if (playerManager.player.boardPosition.Y < fox.boardPosition.Y) | |
down = false; | |
if (playerManager.player.boardPosition.X > fox.boardPosition.X) | |
left = false; | |
if (rand.Next(0, 5) == 0) | |
left = !left; // Randomly make mistakes | |
if (rand.Next(0, 4) == 0) | |
down = !down; // Randomly make mistakes | |
fox.Jump(map, down, left); | |
} | |
public void Update(GameTime gameTime) | |
{ | |
totalElapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds; | |
if (totalElapsed > rand.Next(3000,3500)) | |
{ | |
this.SpawnEnemy(); | |
totalElapsed = 0; | |
} | |
for (int i = Enemies.Count - 1; i >= 0; i--) | |
{ | |
Enemy enemy = Enemies[i]; | |
if ((int)enemy.boardPosition.Y == 6) | |
{ | |
Enemies.RemoveAt(i); | |
} | |
else | |
{ | |
bool left = rand.Next(0, 2) == 0 ? true : false; | |
enemy.Jump(map, true, left); | |
} | |
if (enemy.boardPosition.Y == playerManager.player.boardPosition.Y && enemy.boardPosition.X == playerManager.player.boardPosition.X) //need to fix this | |
{ | |
Reset(); | |
} | |
enemy.Update(gameTime); | |
} | |
FoxFollow(); | |
fox.Update(gameTime); | |
} | |
public void Reset() | |
{ | |
player = new Actor(map.GetSquareCenter(3, 0), spriteSheet, new Rectangle(4, 7, 45, 73), Vector2.Zero, new Vector2(3,0)); | |
} | |
public void Draw(SpriteBatch spriteBatch) | |
{ | |
foreach (Sprite enemy in Enemies) | |
{ | |
enemy.Draw(spriteBatch); | |
} | |
fox.Draw(spriteBatch); | |
} | |
} | |
} |
This file contains hidden or 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 System.Text; | |
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 | |
{ | |
public class Map | |
{ | |
public int[,] board; | |
public Color[] blockColors = new Color[] { Color.Blue, Color.Green }; | |
Texture2D block; | |
public Map(Texture2D block) | |
{ | |
this.block = block; | |
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 } | |
} | |
; | |
} | |
public Vector2 GetSquareCoords(int x, int y) | |
{ | |
int ofs = block.Width / 2; | |
ofs *= y % 2; | |
return new Vector2(x * block.Width + ofs, y * 96); | |
} | |
public Vector2 GetSquareCenter(int x, int y) | |
{ | |
Vector2 coords = GetSquareCoords(x, y); | |
return new Vector2(coords.X + block.Width / 2, coords.Y + 32); | |
} | |
public 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; | |
if (left) | |
{ | |
next_x = x - 1; | |
} | |
else | |
{ | |
next_x = x; | |
} | |
} | |
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; | |
} | |
if (next_x < 0) | |
{ | |
next_x += 1; | |
} | |
if (next_x > 6) | |
{ | |
next_x -= 1; | |
} | |
if (next_y < 0) | |
{ | |
next_y += 1; | |
} | |
if (next_y > 6) | |
{ | |
next_y -= 1; | |
} | |
if (board[next_y, next_x] == 0) | |
{ | |
return new Vector2(x, y); | |
} | |
else | |
{ | |
return new Vector2(next_x, next_y); | |
} | |
} | |
public void Draw(SpriteBatch spriteBatch) | |
{ | |
int drawXOffset = 30; | |
int drawYOffset = 60; | |
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); | |
} | |
} | |
} | |
} |
This file contains hidden or 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 System.Text; | |
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 | |
{ | |
class PlayerManager | |
{ | |
Map map; | |
bool moveTrigger = false; | |
public Actor player; | |
public EnemyManager enemyManager; | |
Texture2D spriteSheet; | |
static int playerStartingLives = 3; | |
static int LivesRemaining = playerStartingLives; | |
public PlayerManager(Map map, Texture2D spriteSheet) | |
{ | |
this.map = map; | |
this.spriteSheet = spriteSheet; | |
player = new Actor(map.GetSquareCenter(3, 0), spriteSheet, new Rectangle(4, 7, 45, 73), Vector2.Zero, new Vector2(3,0)); | |
player.minJumpTime = 500f; | |
player.OnMoveFinish += new EventHandler(player_OnMoveFinish); | |
} | |
void player_OnMoveFinish(object sender, EventArgs e) | |
{ | |
if (map.board[(int)player.boardPosition.Y, (int)player.boardPosition.X] < map.blockColors.Length && !player.isJumping) | |
map.board[(int)player.boardPosition.Y, (int)player.boardPosition.X] += 1; | |
moveTrigger = false; | |
} | |
public void Update(GameTime gameTime) | |
{ | |
KeyboardState kb = Keyboard.GetState(); | |
//keys used are Q, E, A, D or arrow keys | |
if (player.boardPosition.X < 0) | |
{ | |
player.boardPosition.X += 1; | |
} | |
if (player.boardPosition.X > 6) | |
{ | |
player.boardPosition.X -= 1; | |
} | |
if (player.boardPosition.Y < 0) | |
{ | |
player.boardPosition.Y += 1; | |
} | |
if (player.boardPosition.Y > 6) | |
{ | |
player.boardPosition.Y -= 1; | |
} | |
if ((kb.IsKeyDown(Keys.Q) && !moveTrigger) || kb.IsKeyDown(Keys.Up) && !moveTrigger) // UP Left | |
{ | |
moveTrigger = true; | |
player.Jump(map, false, true); | |
} | |
if ((kb.IsKeyDown(Keys.D) && !moveTrigger) || kb.IsKeyDown(Keys.Down) && !moveTrigger) //Down right | |
{ | |
moveTrigger = true; | |
player.Jump(map, true, false); | |
} | |
if ((kb.IsKeyDown(Keys.A) && !moveTrigger) || kb.IsKeyDown(Keys.Left) && !moveTrigger) //Down Left | |
{ | |
moveTrigger = true; | |
player.Jump(map, true, true); | |
} | |
if ((kb.IsKeyDown(Keys.E) && !moveTrigger) || kb.IsKeyDown(Keys.Right) && !moveTrigger) //up Right | |
{ | |
moveTrigger = true; | |
player.Jump(map, false, false); | |
} | |
if (kb.GetPressedKeys().Length == 0) | |
moveTrigger = false; | |
player.Update(gameTime); | |
} | |
public void Draw(SpriteBatch spriteBatch) | |
{ | |
player.Draw(spriteBatch); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment