Advertisement
Alan468

Mario The Beginning

Oct 30th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12.  
  13. namespace GrafikaRuchoma_03b
  14. {
  15.     public class Background : GameComponent, IDrawable
  16.     {
  17.         public Game1 game { get; set; }
  18.         Random rnd = new Random();
  19.  
  20.         Texture2D Bricks;
  21.         Texture2D Floor;
  22.         int OffsetX;
  23.         public List<List<bool>> map;
  24.         public int mapOffset = 0;
  25.  
  26.         public Background(Game game)
  27.             : base(game)
  28.         {
  29.             this.game = game as Game1;
  30.  
  31.             Bricks = game.Content.Load<Texture2D>("brick");
  32.             Floor = game.Content.Load<Texture2D>("granite");
  33.  
  34.             OffsetX = 0;
  35.  
  36.  
  37.             map = new List<List<bool>>();
  38.             int i = 0;
  39.             for (int x = -Floor.Width; x < this.game.Screen.X + (Floor.Width*50); x += Floor.Width)
  40.             {
  41.                 map.Add(new List<bool>());
  42.                 for (int y = (this.game.Screen.Y / 2) + Floor.Height; y < (this.game.Screen.Y) + (Floor.Height*50); y += Floor.Height)
  43.                 {
  44.                     map[i].Add(rnd.Next(0, 2) == 1);
  45.                 }
  46.                 i++;
  47.             }
  48.         }
  49.  
  50.         public override void Initialize()
  51.         {
  52.             base.Initialize();
  53.         }
  54.  
  55.         public override void Update(GameTime gameTime)
  56.         {
  57.             var keyboard = Keyboard.GetState();
  58.  
  59.             if (keyboard.IsKeyDown(Keys.Left) && mapOffset > 0)
  60.             {
  61.                 OffsetX+=3;
  62.             }
  63.             else if (keyboard.IsKeyDown(Keys.Right))
  64.             {
  65.                 OffsetX-=3;
  66.             }
  67.  
  68.             if (OffsetX >= Bricks.Width / 2)
  69.             {
  70.                 OffsetX = -Bricks.Width / 2;
  71.                 mapOffset--;
  72.             }
  73.             else if (OffsetX <= -Bricks.Width / 2)
  74.             {
  75.                 OffsetX = Bricks.Width / 2;
  76.                 mapOffset++;
  77.                 map.Add(new List<bool>());
  78.                 for (int x = -Floor.Width; x < this.game.Screen.X + Floor.Width; x += Floor.Width)
  79.                 {
  80.                     map[map.Count - 1].Add(rnd.Next(0, 2) == 1);
  81.                 }
  82.             }
  83.  
  84.             base.Update(gameTime);
  85.         }
  86.  
  87.         public void Draw(GameTime gameTime)
  88.         {
  89.             game.spriteBatch.Begin();
  90.  
  91.             for (int y = -Bricks.Height; y < (game.Screen.Y / 3) + Bricks.Height; y += Bricks.Height)
  92.             {
  93.                 for (int x = -Bricks.Width; x < game.Screen.X + Bricks.Width; x += Bricks.Width)
  94.                 {
  95.                     game.spriteBatch.Draw(Bricks, new Rectangle(x + OffsetX, y, Bricks.Width, Bricks.Height), Color.White);
  96.                 }
  97.             }
  98.  
  99.             int i = mapOffset;
  100.             for (int x = -Floor.Width; x < game.Screen.X + Floor.Width; x += Floor.Width)
  101.             {
  102.                 int j = 0;
  103.                 for (int y = (game.Screen.Y / 2) + Floor.Height; y < (game.Screen.Y) + Floor.Height; y += Floor.Height)
  104.                 {
  105.                     if (map[i][j])
  106.                         game.spriteBatch.Draw(Floor, new Rectangle(x + OffsetX, y, Floor.Width, Floor.Height), Color.White);
  107.                     j++;
  108.                 }
  109.                 i++;
  110.             }
  111.  
  112.             game.spriteBatch.End();
  113.         }
  114.  
  115.         #region No idea
  116.         public int DrawOrder { get { return 1; } }
  117.         public bool Visible { get { return true; } }
  118.         public event EventHandler<EventArgs> DrawOrderChanged;
  119.         public event EventHandler<EventArgs> VisibleChanged;
  120.         #endregion
  121.     }
  122. }
  123.  
  124. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  125.  
  126. using System;
  127. using System.Collections.Generic;
  128. using System.Linq;
  129. using Microsoft.Xna.Framework;
  130. using Microsoft.Xna.Framework.Audio;
  131. using Microsoft.Xna.Framework.Content;
  132. using Microsoft.Xna.Framework.GamerServices;
  133. using Microsoft.Xna.Framework.Graphics;
  134. using Microsoft.Xna.Framework.Input;
  135. using Microsoft.Xna.Framework.Media;
  136.  
  137.  
  138. namespace GrafikaRuchoma_03b
  139. {
  140.     public class Player : GameComponent, IDrawable
  141.     {
  142.         Point position;
  143.         int FrameRate = 16;
  144.         int timeSinceLastFrame;
  145.  
  146.         bool Jump;
  147.  
  148.         Game1 game;
  149.         Texture2D MarioWalk;
  150.         int MarioStep;
  151.  
  152.         Texture2D MarioJump;
  153.         int MarioJumpIndex;
  154.  
  155.         public Player(Game game)
  156.             : base(game)
  157.         {
  158.             this.game = game as Game1;
  159.  
  160.             MarioWalk = game.Content.Load<Texture2D>("mario");
  161.             MarioJump = game.Content.Load<Texture2D>("mario-skok");
  162.             MarioStep = 0;
  163.             MarioJumpIndex = 0;
  164.             Jump = false;
  165.             marioWalkEffect = SpriteEffects.None;
  166.             position = new Point(this.game.Screen.X / 2, this.game.Screen.Y / 2);
  167.         }
  168.  
  169.         public override void Initialize()
  170.         {
  171.             // TODO: Add your initialization code here
  172.  
  173.             base.Initialize();
  174.         }
  175.  
  176.         public override void Update(GameTime gameTime)
  177.         {
  178.             var keyboard = Keyboard.GetState();
  179.             timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
  180.  
  181.             if (keyboard.IsKeyDown(Keys.Right) && !Jump)
  182.             {
  183.                 marioWalkEffect = SpriteEffects.None;
  184.                 if (timeSinceLastFrame > FrameRate)
  185.                 {
  186.                     timeSinceLastFrame = 0;
  187.                     MarioStep++;
  188.                     if (MarioStep > 3)
  189.                     {
  190.                         MarioStep = 0;
  191.                     }
  192.                 }
  193.             }
  194.             else if (keyboard.IsKeyDown(Keys.Left) && !Jump)
  195.             {
  196.                 marioWalkEffect = SpriteEffects.FlipHorizontally;
  197.                 if (timeSinceLastFrame > FrameRate)
  198.                 {
  199.                     timeSinceLastFrame = 0;
  200.                     MarioStep--;
  201.                     if (MarioStep < 0)
  202.                     {
  203.                         MarioStep = 3;
  204.                     }
  205.                 }
  206.             }
  207.  
  208.  
  209.             if (keyboard.IsKeyDown(Keys.Up) || Jump)
  210.             {
  211.                 Jump = true;
  212.  
  213.                 if (timeSinceLastFrame > FrameRate*2)
  214.                 {
  215.                     timeSinceLastFrame = 0;
  216.                     MarioJumpIndex++;
  217.                 }
  218.  
  219.                 if (MarioJumpIndex > 4)
  220.                 {
  221.                     Jump = false;
  222.                     timeSinceLastFrame = 0;
  223.                     MarioJumpIndex = 0;
  224.                 }
  225.             }
  226.  
  227.             base.Update(gameTime);
  228.         }
  229.  
  230.         public void Draw(GameTime gameTime)
  231.         {
  232.             game.spriteBatch.Begin();
  233.  
  234.             if (!Jump)
  235.             {
  236.                 game.spriteBatch.Draw(MarioWalk, new Rectangle(this.game.Screen.X / 2, position.Y - 30, 33, 54), new Rectangle(MarioStep * 33, 0, 33, 54), Color.White, 0, Vector2.Zero, marioWalkEffect, 0);
  237.             }
  238.             else
  239.             {
  240.                 game.spriteBatch.Draw(MarioJump, new Rectangle((game.Screen.X / 2)-32, (game.Screen.Y / 2) - 102, 128, 128), new Rectangle(MarioJumpIndex * 128, 0, 128, 128), Color.White, 0, Vector2.Zero, marioWalkEffect, 0);
  241.             }
  242.  
  243.             game.spriteBatch.End();
  244.         }
  245.  
  246.         #region No idea
  247.         public int DrawOrder { get { return 1; } }
  248.         public bool Visible { get { return true; } }
  249.         public event EventHandler<EventArgs> DrawOrderChanged;
  250.         public event EventHandler<EventArgs> VisibleChanged;
  251.         #endregion
  252.  
  253.         public SpriteEffects marioWalkEffect { get; set; }
  254.     }
  255. }
  256.  
  257.  
  258. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  259.  
  260. using System;
  261. using System.Collections.Generic;
  262. using System.Linq;
  263. using Microsoft.Xna.Framework;
  264. using Microsoft.Xna.Framework.Audio;
  265. using Microsoft.Xna.Framework.Content;
  266. using Microsoft.Xna.Framework.GamerServices;
  267. using Microsoft.Xna.Framework.Graphics;
  268. using Microsoft.Xna.Framework.Input;
  269. using Microsoft.Xna.Framework.Media;
  270.  
  271. namespace GrafikaRuchoma_03b
  272. {
  273.     public class Game1 : Game
  274.     {
  275.         public GraphicsDeviceManager graphics;
  276.         public SpriteBatch spriteBatch;
  277.  
  278.         int Score;
  279.         int Health = 3;
  280.         SpriteFont Font;
  281.  
  282.         public Point Screen { get { return new Point(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); } }
  283.  
  284.         public Game1()
  285.         {
  286.             graphics = new GraphicsDeviceManager(this);
  287.             Content.RootDirectory = "Content";
  288.  
  289.             graphics.PreferredBackBufferWidth = 800;
  290.             graphics.PreferredBackBufferHeight = 600;
  291.             IsMouseVisible = true;
  292.             Window.AllowUserResizing = true;
  293.         }
  294.  
  295.         protected override void Initialize()
  296.         {
  297.             // TODO: Add your initialization logic here
  298.  
  299.             base.Initialize();
  300.         }
  301.  
  302.         protected override void LoadContent()
  303.         {
  304.             // Create a new SpriteBatch, which can be used to draw textures.
  305.             spriteBatch = new SpriteBatch(GraphicsDevice);
  306.  
  307.             Font = Content.Load<SpriteFont>("font");
  308.  
  309.             background = new Background(this);
  310.             mario = new Player(this);
  311.  
  312.             this.Components.Add(background);
  313.             this.Components.Add(mario);
  314.  
  315.             // TODO: use this.Content to load your game content here
  316.         }
  317.  
  318.         protected override void UnloadContent()
  319.         {
  320.             // TODO: Unload any non ContentManager content here
  321.         }
  322.  
  323.         protected override void Update(GameTime gameTime)
  324.         {
  325.             var mouseState = Mouse.GetState();
  326.             var keyboardState = Keyboard.GetState();
  327.             var mousePos = new Point(mouseState.X, mouseState.Y);
  328.  
  329.             // Allows the game to exit
  330.             if (keyboardState.IsKeyDown(Keys.Escape))
  331.                 this.Exit();
  332.  
  333.  
  334.             //if (background.map[background.mapOffset + (Screen.X / 2)][])
  335.             //{
  336.  
  337.             //}
  338.  
  339.             // TODO: Add your update logic here
  340.  
  341.             base.Update(gameTime);
  342.         }
  343.  
  344.         protected override void Draw(GameTime gameTime)
  345.         {
  346.             GraphicsDevice.Clear(Color.Black);
  347.  
  348.             // TODO: Add your drawing code here
  349.  
  350.             base.Draw(gameTime);
  351.  
  352.             spriteBatch.Begin();
  353.             spriteBatch.DrawString(Font, "Wynik: " + Score, new Vector2(50, Screen.Y - 50), Color.Yellow);
  354.             spriteBatch.DrawString(Font, "Zycia: ", new Vector2(200, Screen.Y - 50), Color.Yellow);
  355.  
  356.             for (int i = 0; i < Health; i++)
  357.             {
  358.                 spriteBatch.DrawString(Font, "*", new Vector2(270+((i+1)*20), Screen.Y - 50), Color.Red);
  359.             }
  360.  
  361.             spriteBatch.End();
  362.         }
  363.  
  364.         public Background background { get; set; }
  365.  
  366.         public Player mario { get; set; }
  367.     }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement