Platform.cs
class Platform
{
Texture2D texture;
Vector2 position;
public Rectangle rectangle;
public Platform(Texture2D newTexture, Vector2 newPosition)
{
texture = newTexture;
position = newPosition;
rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, rectangle, Color.White);
}
}
}
Character.cs
Class Character
{
public Texture2D texture;
public Vector2 position;
public Vector2 velocity;
public Rectangle rectangle;
public float JumpeHeight = 3;
public bool hasJumped;
public Character(Texture2D newTexture, Vector2 newPosition) {
texture = newTexture;
position = newPosition;
hasJumped = true;
}
public void Update(GameTime gameTime) {
position += velocity;
rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
velocity.X = 3f;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
velocity.X = -3f;
}
else
{
velocity.X = 0f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Space) && hasJumped == false)
{
if (JumpeHeight >= 9) {
JumpeHeight = 4;
}
position.Y -= 10f;
velocity.Y = -JumpeHeight;
hasJumped = true;
}
if (hasJumped == true)
{
float i = 1;
velocity.Y += 0.15f * i;
}
if (position.Y + texture.Height >= 470)
{
hasJumped = false;
}
if (hasJumped == false)
{
velocity.Y = 0f;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, rectangle, Color.White);
}
}
}
Game1.cs
namespace Tuete1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Character player;
List<platform> platforms = new List<platform>();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
player = new Character(Content.Load<texture2d>("Face"), new Vector2(50, 50));
platforms.Add(new Platform(Content.Load<texture2d>("gh"), new Vector2(30, 400)));
platforms.Add(new Platform(Content.Load<texture2d>("Bree"), new Vector2(350, 300)));
platforms.Add(new Platform(Content.Load<texture2d>("gh"), new Vector2(140, 100)));
platforms.Add(new Platform(Content.Load<texture2d>("gh"), new Vector2(500, 100)));
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
IsMouseVisible = true ;
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
foreach (Platform platform in platforms)
if (player.rectangle.isOnTopOf(platform.rectangle))
{
player.JumpeHeight += 1;
player.hasJumped = false;
}
player.Update(gameTime);
if (player.position.Y + player.texture.Height >= 470)
this.Exit();
if (player.position.X < 0)
player.position = new Vector2(0, player.position.Y);
int rightEdge = GraphicsDevice.Viewport.Width - player.texture.Width;
if (player.position.X > rightEdge)
player.position = new Vector2(rightEdge, player.position.Y);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (Platform platform in platforms)
platform.Draw(spriteBatch);
player.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
static class RectangleHelper {
const int penetrationMargin = 5;
public static bool isOnTopOf(this Rectangle r1, Rectangle r2)
{
return (r1.Bottom >= r2.Top - penetrationMargin && r1.Bottom <= r2.Top + 1 && r1.Right >= r2.Left + 5 && r1.Left <= r2.Right - 5);
}
}
When it's jump to Tile(Platform) when i go along the tile(I use Brick tile) when the Tile end the Character not fallen to down steps, rather it's go from the particular Brick position even though the Brick is end. where i want to Change the code In Order to get this Correct(All game Class are here can copy and Check the Problem)