Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 GrafikaRuchoma_03b
- {
- public class Background : GameComponent, IDrawable
- {
- public Game1 game { get; set; }
- Random rnd = new Random();
- Texture2D Bricks;
- Texture2D Floor;
- int OffsetX;
- public List<List<bool>> map;
- public int mapOffset = 0;
- public Background(Game game)
- : base(game)
- {
- this.game = game as Game1;
- Bricks = game.Content.Load<Texture2D>("brick");
- Floor = game.Content.Load<Texture2D>("granite");
- OffsetX = 0;
- map = new List<List<bool>>();
- int i = 0;
- for (int x = -Floor.Width; x < this.game.Screen.X + (Floor.Width*50); x += Floor.Width)
- {
- map.Add(new List<bool>());
- for (int y = (this.game.Screen.Y / 2) + Floor.Height; y < (this.game.Screen.Y) + (Floor.Height*50); y += Floor.Height)
- {
- map[i].Add(rnd.Next(0, 2) == 1);
- }
- i++;
- }
- }
- public override void Initialize()
- {
- base.Initialize();
- }
- public override void Update(GameTime gameTime)
- {
- var keyboard = Keyboard.GetState();
- if (keyboard.IsKeyDown(Keys.Left) && mapOffset > 0)
- {
- OffsetX+=3;
- }
- else if (keyboard.IsKeyDown(Keys.Right))
- {
- OffsetX-=3;
- }
- if (OffsetX >= Bricks.Width / 2)
- {
- OffsetX = -Bricks.Width / 2;
- mapOffset--;
- }
- else if (OffsetX <= -Bricks.Width / 2)
- {
- OffsetX = Bricks.Width / 2;
- mapOffset++;
- map.Add(new List<bool>());
- for (int x = -Floor.Width; x < this.game.Screen.X + Floor.Width; x += Floor.Width)
- {
- map[map.Count - 1].Add(rnd.Next(0, 2) == 1);
- }
- }
- base.Update(gameTime);
- }
- public void Draw(GameTime gameTime)
- {
- game.spriteBatch.Begin();
- for (int y = -Bricks.Height; y < (game.Screen.Y / 3) + Bricks.Height; y += Bricks.Height)
- {
- for (int x = -Bricks.Width; x < game.Screen.X + Bricks.Width; x += Bricks.Width)
- {
- game.spriteBatch.Draw(Bricks, new Rectangle(x + OffsetX, y, Bricks.Width, Bricks.Height), Color.White);
- }
- }
- int i = mapOffset;
- for (int x = -Floor.Width; x < game.Screen.X + Floor.Width; x += Floor.Width)
- {
- int j = 0;
- for (int y = (game.Screen.Y / 2) + Floor.Height; y < (game.Screen.Y) + Floor.Height; y += Floor.Height)
- {
- if (map[i][j])
- game.spriteBatch.Draw(Floor, new Rectangle(x + OffsetX, y, Floor.Width, Floor.Height), Color.White);
- j++;
- }
- i++;
- }
- game.spriteBatch.End();
- }
- #region No idea
- public int DrawOrder { get { return 1; } }
- public bool Visible { get { return true; } }
- public event EventHandler<EventArgs> DrawOrderChanged;
- public event EventHandler<EventArgs> VisibleChanged;
- #endregion
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 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 GrafikaRuchoma_03b
- {
- public class Player : GameComponent, IDrawable
- {
- Point position;
- int FrameRate = 16;
- int timeSinceLastFrame;
- bool Jump;
- Game1 game;
- Texture2D MarioWalk;
- int MarioStep;
- Texture2D MarioJump;
- int MarioJumpIndex;
- public Player(Game game)
- : base(game)
- {
- this.game = game as Game1;
- MarioWalk = game.Content.Load<Texture2D>("mario");
- MarioJump = game.Content.Load<Texture2D>("mario-skok");
- MarioStep = 0;
- MarioJumpIndex = 0;
- Jump = false;
- marioWalkEffect = SpriteEffects.None;
- position = new Point(this.game.Screen.X / 2, this.game.Screen.Y / 2);
- }
- public override void Initialize()
- {
- // TODO: Add your initialization code here
- base.Initialize();
- }
- public override void Update(GameTime gameTime)
- {
- var keyboard = Keyboard.GetState();
- timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
- if (keyboard.IsKeyDown(Keys.Right) && !Jump)
- {
- marioWalkEffect = SpriteEffects.None;
- if (timeSinceLastFrame > FrameRate)
- {
- timeSinceLastFrame = 0;
- MarioStep++;
- if (MarioStep > 3)
- {
- MarioStep = 0;
- }
- }
- }
- else if (keyboard.IsKeyDown(Keys.Left) && !Jump)
- {
- marioWalkEffect = SpriteEffects.FlipHorizontally;
- if (timeSinceLastFrame > FrameRate)
- {
- timeSinceLastFrame = 0;
- MarioStep--;
- if (MarioStep < 0)
- {
- MarioStep = 3;
- }
- }
- }
- if (keyboard.IsKeyDown(Keys.Up) || Jump)
- {
- Jump = true;
- if (timeSinceLastFrame > FrameRate*2)
- {
- timeSinceLastFrame = 0;
- MarioJumpIndex++;
- }
- if (MarioJumpIndex > 4)
- {
- Jump = false;
- timeSinceLastFrame = 0;
- MarioJumpIndex = 0;
- }
- }
- base.Update(gameTime);
- }
- public void Draw(GameTime gameTime)
- {
- game.spriteBatch.Begin();
- if (!Jump)
- {
- 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);
- }
- else
- {
- 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);
- }
- game.spriteBatch.End();
- }
- #region No idea
- public int DrawOrder { get { return 1; } }
- public bool Visible { get { return true; } }
- public event EventHandler<EventArgs> DrawOrderChanged;
- public event EventHandler<EventArgs> VisibleChanged;
- #endregion
- public SpriteEffects marioWalkEffect { get; set; }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 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 GrafikaRuchoma_03b
- {
- public class Game1 : Game
- {
- public GraphicsDeviceManager graphics;
- public SpriteBatch spriteBatch;
- int Score;
- int Health = 3;
- SpriteFont Font;
- public Point Screen { get { return new Point(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); } }
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- graphics.PreferredBackBufferWidth = 800;
- graphics.PreferredBackBufferHeight = 600;
- IsMouseVisible = true;
- Window.AllowUserResizing = true;
- }
- 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);
- Font = Content.Load<SpriteFont>("font");
- background = new Background(this);
- mario = new Player(this);
- this.Components.Add(background);
- this.Components.Add(mario);
- // TODO: use this.Content to load your game content here
- }
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- protected override void Update(GameTime gameTime)
- {
- var mouseState = Mouse.GetState();
- var keyboardState = Keyboard.GetState();
- var mousePos = new Point(mouseState.X, mouseState.Y);
- // Allows the game to exit
- if (keyboardState.IsKeyDown(Keys.Escape))
- this.Exit();
- //if (background.map[background.mapOffset + (Screen.X / 2)][])
- //{
- //}
- // TODO: Add your update logic here
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.Black);
- // TODO: Add your drawing code here
- base.Draw(gameTime);
- spriteBatch.Begin();
- spriteBatch.DrawString(Font, "Wynik: " + Score, new Vector2(50, Screen.Y - 50), Color.Yellow);
- spriteBatch.DrawString(Font, "Zycia: ", new Vector2(200, Screen.Y - 50), Color.Yellow);
- for (int i = 0; i < Health; i++)
- {
- spriteBatch.DrawString(Font, "*", new Vector2(270+((i+1)*20), Screen.Y - 50), Color.Red);
- }
- spriteBatch.End();
- }
- public Background background { get; set; }
- public Player mario { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement