Dobrý den mám problém program mi vzdy padne pri pokusu zmacknuti talacitka pochybu. Problém je někde v Player Update kdyz se pokousim o pohyb. Zde prikladam Tridy: Sprite, Game1, PlayerGame1
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 Snake_game_Projekt
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D Pozadi;
Texture2D SnakeTexture;
Rectangle prostred;
List<Sprite> sprites = new List<Sprite>();//List je dynam pole.
Player player;
public SpriteBatch SpriteBatch
{
get { return spriteBatch; }
set { spriteBatch = value; }
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Pozadi = Content.Load<Texture2D>("Background");
SnakeTexture = Content.Load<Texture2D>("Snake");
prostred = new Rectangle((graphics.PreferredBackBufferWidth - SnakeTexture.Width) / 2,
(graphics.PreferredBackBufferHeight - SnakeTexture.Height) / 2, SnakeTexture.Width, SnakeTexture.Height);
player = new Player(SnakeTexture, prostred, Color.White, 0.15f, sprites);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
//Nacitani z klavesnice
KeyboardState ks = Keyboard.GetState();
player.Update(ks, gameTime);
foreach (Sprite s in sprites)
{
s.Update(ks, gameTime);
}
//Konec
ks = Exit(ks);
base.Update(gameTime);
}
private KeyboardState Exit(KeyboardState ks)
{
//Ukonceni programu
if (ks.IsKeyDown(Keys.Escape))
{
this.Exit();
}
return ks;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
for (int x = 0; x < graphics.PreferredBackBufferWidth; x += Pozadi.Width)
{
for (int y = 0; y < graphics.PreferredBackBufferHeight; y += Pozadi.Height)
{
spriteBatch.Draw(Pozadi, new Rectangle(x, y, Pozadi.Width, Pozadi.Height), Color.White);
}
}
//Vykreslovani hada
player.Draw(spriteBatch);
foreach (Sprite s in sprites)
s.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Sprite
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Snake_game_Projekt
{
class Sprite
{
public Texture2D Textura;
public Vector2 Pozice;
public Color colors;
public double timer;
public float SnakeSpeed = 0.15f;
public float x = Math.Pi;
public Rectangle Rectangle;
public Sprite(Texture2D Textura, Rectangle rectangle, Color color)
{
this.Textura = Textura;
this.Rectangle = rectangle;
this.Pozice = new Vector2(rectangle.X, rectangle.Y);
this.colors = color;
}
public Vector2 GetPozice()
{
return Pozice;
}
public Rectangle GetRectangle()
{
return new Rectangle((int)Pozice.X, (int)Pozice.Y, (int)Textura.Width, (int)Textura.Height);
}
public virtual void Update(KeyboardState ks, GameTime gameTime)
{
Pozice = new Vector2(Rectangle.X, Rectangle.Y);
}
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Textura, Pozice, Color.White);
}
}
}
Player
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Snake_game_Projekt
{
class Player : Sprite
{
public int Zivoty { get; set; }
public float Rychlost { get; set; }
private bool keyPressed;
private List<Sprite> Pl;
public Player(Texture2D textura, Rectangle rec, Color color, float Rychlo, List<Sprite> sprites)
: base(textura, rec, color)
{
this.Textura = textura;
this.Rectangle = rec;
this.colors = color;
this.Rychlost = Rychlo;
this.Pl = sprites;
}
public override void Update(KeyboardState ks, GameTime gameTime)
{
//Zmena smeru hada
if (ks.IsKeyDown(Keys.Left))
x = (x - 0.5f*MathHelper.TwoPi) % MathHelper.TwoPi;
else if (ks.IsKeyDown(Keys.Right))
x = (x + 0.5f) % MathHelper.TwoPi;
//Ochrana proti Sekanim
timer = gameTime.ElapsedGameTime.TotalMilliseconds;
Pl[0].Pozice.X += (float)(SnakeSpeed * timer * Math.Sin(x));
Pl[0].Pozice.Y += (float)(SnakeSpeed * timer * Math.Cos(x));
Pl[0].Rectangle.X = (int)Pl[0].Pozice.X;
Pl[0].Rectangle.Y = (int)Pl[0].Pozice.Y;
}
}
}
|