using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace zombieGame
{
class Player
{
// Animation representing the player
public Texture2D PlayerTexture;
// Position of the Player relative to the upper left side of the screen
public Vector2 Position;
public Vector2 Direction = Vector2.Zero;
public Vector2 Speed = Vector2.Zero;
// State of the player
public bool Active;
// Amount of hit points that player has
public int Health;
// Get the width of the player
public int Width
{
get { return PlayerTexture.Width; }
}
// Get the height of the player
public int Height
{
get { return PlayerTexture.Height; }
}
public void Initialize(Texture2D texture, Vector2 position)
{
PlayerTexture = texture;
// Set the starting position of the player around the middle of the screen and to the back
Position = position;
// Set the player to be active
Active = true;
// Set the player health
Health = 100;
}
public void Update()
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
}
}
}
and here is my game1 class
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;
using Microsoft.Xna.Framework.Storage;
namespace zombieGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//used for jump
const int MOVE_UP = -1;
const int MOVE_DOWN = 1;
const int PLAYER_SPEED = 160;
// Represents the player
Player player;
enum State
{
Walking,
Jumping
}
State CurrentState = State.Walking;
Vector2 StartingPosition = Vector2.Zero;
// Keyboard states used to determine key presses
KeyboardState currentKeyboardState;
KeyboardState previousKeyboardState;
// A movement speed for the player
float playerMoveSpeed;
// Image used to display the static background
Texture2D mainBackground;
// Parallaxing Layers
Background bgLayer1;
Background bgLayer2;
Background bgLayer3;
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()
{
// Initialize the player class
player = new Player();
// Set a constant player move speed
playerMoveSpeed = 8.0f;
// Initialize our background
bgLayer1 = new Background();
bgLayer2 = new Background();
bgLayer3 = new Background();
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);
// Load the player resources
Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y + GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
player.Initialize(Content.Load<Texture2D>("Player"), playerPosition);
// Load the parallaxing background after the initialize player
bgLayer1.Initialize(Content, "cloud2", GraphicsDevice.Viewport.Width, -1);
bgLayer2.Initialize(Content, "cloud3", GraphicsDevice.Viewport.Width, -2);
bgLayer3.Initialize(Content, "Trees", GraphicsDevice.Viewport.Width, -4);
// the main background
mainBackground = Content.Load<Texture2D>("CloudAndGrass");
// 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
}
/// <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();
// Read the current state of the keyboard and store it
currentKeyboardState = Keyboard.GetState();
//Update the player jump
UpdateJump(currentKeyboardState);
// Save the previous state of the keyboard so we can determinesingle key/button presses
previousKeyboardState = currentKeyboardState;
// Update the parallaxing background AFTER THE UPDATE PLAYER
bgLayer1.Update();
bgLayer2.Update();
bgLayer3.Update();
base.Update(gameTime);
}
private void UpdatePlayer(GameTime gameTime)
{
}
private void UpdateJump(KeyboardState currentKeyboardState)
{
if (CurrentState == State.Walking)
{
if (currentKeyboardState.IsKeyDown(Keys.Space) == true && previousKeyboardState.IsKeyDown(Keys.Space) == false)
{
Jump();
}
}
if (CurrentState == State.Jumping)
{
if (StartingPosition.Y - player.Position.Y > 150)
{
player.Direction.Y = MOVE_DOWN;
}
if (player.Position.Y > StartingPosition.Y)
{
player.Position.Y = StartingPosition.Y;
CurrentState = State.Walking;
player.Direction = Vector2.Zero;
}
}
}
private void Jump()
{
if (CurrentState != State.Jumping)
{
CurrentState = State.Jumping;
StartingPosition = player.Position;
player.Direction.Y = MOVE_UP;
player.Speed = new Vector2(PLAYER_SPEED, PLAYER_SPEED);
}
}
/// <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.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(mainBackground, Vector2.Zero, Color.White);
// Draw the Player
player.Draw(spriteBatch);
// Draw the moving background
bgLayer1.Draw(spriteBatch);
bgLayer2.Draw(spriteBatch);
bgLayer3.Draw(spriteBatch);
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}

New Topic/Question
Reply



MultiQuote






|