Nejčastěji odpovídající uživatel
Moje práce

Dotaz
-
Chci se zeptat, je toto správně?
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 WindowsGame2
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class Sprite : Microsoft.Xna.Framework.DrawableGameComponent //musí být drawable
{
public Texture2D textura; //texturu a pozici dáváme jako public
public Vector2 pozice;
public Sprite(Game game)
: base(game)
{
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
base.Initialize();
}
protected override void LoadContent()
{
base.LoadContent();
}
public Rectangle DejObdelnik() //vrací nám obdélník z textury a pozice
{
return new Rectangle((int)pozice.X, (int)pozice.Y, textura.Width, textura.Height);
}
public bool KolizeS(Sprite druhy) //bool, protože intersects vrací true nebo false
{
return this.DejObdelnik().Intersects(druhy.DejObdelnik()); //vrátí výsledek kolize našeho obdélníku s druhým obdélníkem
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
Game1 g = (Game1)Game;
g.SpriteBatch.Draw(textura, pozice, Color.White); //vykreslení textury
base.Draw(gameTime);
}
}
}
__________________________________
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 WindowsGame2
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class Hrac : Sprite
{
public int sebrano;
public Hrac(Game game)
: base(game)
{
// TODO: Construct any child components here
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
base.Initialize();
}
protected override void LoadContent()
{
textura = Game.Content.Load<Texture2D>("panak"); //načtení obrázku
pozice = new Vector2(100, 100); //vytvoření pozice
base.LoadContent();
}
/// <summary>
/// Allows the game
///
/// component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
Keys[] klavesy = Keyboard.GetState().GetPressedKeys();
if (klavesy.Contains(Keys.Left) && pozice.X > 0)
{
pozice.X -= 4;
}
if (klavesy.Contains(Keys.Right) && pozice.X < Game.Window.ClientBounds.Width - textura.Width)
{
pozice.X += 4;
}
if (klavesy.Contains(Keys.Up) && pozice.Y > 0)
{
pozice.Y -= 4;
}
if (klavesy.Contains(Keys.Down) && pozice.Y < Game.Window.ClientBounds.Height - textura.Height)
{
pozice.Y += 4;
}
List<Coin> mince = Game.Components.OfType<Coin>().ToList();
foreach (Coin c in mince) //zjistí nám kolizi s mincí, přičte nám bod a odstraní minci
{
if (this.KolizeS(c))
{
sebrano++;
Game.Components.Remove(c);
}
}
base.Update(gameTime);
}
}
}
_____________________________________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 WindowsGame2
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class Coin : Sprite
{
int x;
int y;
public Coin(Game game, int x, int y)
: base(game)
{
this.x = x;
this.y = y;
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
base.Initialize();
}
protected override void LoadContent()
{
textura = Game.Content.Load<Texture2D>("coin"); //načtení obrázku
pozice = new Vector2(x, y); //vytvoření pozice
base.LoadContent();
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
base.Update(gameTime);
}
}
}
_________________________________________________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 WindowsGame2
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
public SpriteBatch spriteBatch; //spritebatch zapouzdříme, nebo k nemu napíšeme public
public SpriteFont SpriteFont1; //kvůli výpisu stringů
Hrac h;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 800; //velikost okna
graphics.PreferredBackBufferHeight = 600;
Content.RootDirectory = "Content";
}
public SpriteBatch SpriteBatch
{
set { spriteBatch = value; }
get { return spriteBatch; }
}
/// <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()
{
// TODO: Add your initialization logic here
h = new Hrac(this); //vytvoření hráče
Components.Add(h); //přidání hráče do hry
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
Components.Add(new Coin(this, rnd.Next(0, 700), rnd.Next(0, 500)));
}
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);
SpriteFont1 = Content.Load<SpriteFont>("SpriteFont1");
// 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
Keys[] klavesy = Keyboard.GetState().GetPressedKeys();
if (klavesy.Contains(Keys.Escape))
{
this.Exit();
}
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <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);
// TODO: Add your drawing code here
spriteBatch.Begin(); //musí být begin a end
base.Draw(gameTime); //vykreslení všech věcí ve hře
spriteBatch.DrawString(SpriteFont1, "Sebrano: " + h.sebrano, new Vector2(10, 10), Color.Yellow);
spriteBatch.End();
}
}
}
Děkuji
Odpovědi
-
Ne. :-)
Není zač. :-)
Robert Haken, Microsoft MVP ASP.NET/IIS, HAVIT, s.r.o., www.havit.cz, http://knowledge-base.havit.cz
- Označen jako odpověď Robert HakenMVP, Moderator pátek 29. května 2015 14:27
Všechny reakce
-
-
Ne. :-)
Není zač. :-)
Robert Haken, Microsoft MVP ASP.NET/IIS, HAVIT, s.r.o., www.havit.cz, http://knowledge-base.havit.cz
- Označen jako odpověď Robert HakenMVP, Moderator pátek 29. května 2015 14:27