Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XNA4.0
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
        {
        /// <summary>
        /// This is the main type for your game
        /// </summary>
        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()
                {
                // TODO: Add your initialization logic here
    
                base.Initialize();
                }
    
          
            protected override void LoadContent()
                {
                // Create a new SpriteBatch, which can be used to draw textures.
                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()
                {
                // 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)
                {
                 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);
    
                //Game Over
               if (player.position.Y + player.texture.Height >= 470)
    
                    this.Exit();
    
               // Prevent player from moving off the left edge of the screen
                if (player.position.X < 0)
                    player.position = new Vector2(0, player.position.Y); 
               
    
                // Prevent player from moving off the right edge of the screen
                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)
Posted 8-Oct-12 7:50am
Edited 8-Oct-12 8:01am
(no name)33.2K
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 434
1 ridoy 368
2 DamithSL 270
3 OriginalGriff 159
4 Suvendu Shekhar Giri 150
0 OriginalGriff 7,968
1 DamithSL 6,179
2 Sergey Alexandrovich Kryukov 5,524
3 Maciej Los 5,438
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100