C# xna-ban készítek egy sokoban játékot. A pályát egy 2 dimenziós tömb segítségével építettem fel de a tömbben nem tudom hogyan kéne mozgassam a játékost, ötletek?
int[,] mezo = new int[,]{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,3,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,2,1,0,1,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,0,1,1,1,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,3,0,0,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,3,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
Várj egy gombnyomásra. Nézd meg, hogy az annak megfelelő pozíció szabad-e v. törölhető-e. Ha igen, akkor tedd át oda a figurád.
Ha nem szabad, akkor láda van-e ott. Ha igen, akkor nézd meg, hogy az irányban a következő mező szabad-e. Ha igen, akkor a láda menjen az üresre, figura a láda helyére.
A többi esetben ne csinálj semmit.
Miután mozdult az embered hajtsd végre a leeső dolgokat mozgató algoritmust.
Köszönöm a gyors választ. Az igásság az hogy fejben énis valahogy így képzelem viszont sajnos a megvalósítani nem tudom hogy kéne.itt vannak a próbálkozásaim :
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 Sokoban_starwars
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D palya;
Rectangle palya1;
Texture2D jatekos;
Rectangle jatekos1;
Texture2D hatter;
Texture2D lezerkard;
Rectangle lezerkard1;
Vector2 pozicio = new Vector2(230, 190);
const int meret = 10;
int[,] mezo = new int[,]{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,3,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,2,1,0,1,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,0,1,1,1,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,3,0,0,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,3,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
graphics.IsFullScreen = true;
graphics.ApplyChanges();
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
palya = Content.Load<Texture2D>("palya1");
palya1 = new Rectangle(500, 500, 20, 20);
hatter = Content.Load<Texture2D>("hatter1");
jatekos = Content.Load<Texture2D>("jatekos");
jatekos1 = new Rectangle(74, 74, 74, 74);
lezerkard = Content.Load<Texture2D>("lezarkard");
lezerkard1 = new Rectangle(500, 500, 20, 20);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
KeyboardState keybState = Keyboard.GetState();
if (keybState.IsKeyDown(Keys.Escape))
this.Exit();
/*
if (keybState.IsKeyDown(Keys.Left) && jatekos1.X >= 0)
jatekos1.X -= 74;
if (keybState.IsKeyDown(Keys.Right))
jatekos1.X += 74;
if (keybState.IsKeyDown(Keys.Up))
jatekos1.Y -= 74;
if (keybState.IsKeyDown(Keys.Down))
jatekos1.Y += 74;
*/
jatekos1 = new Rectangle((int)pozicio.X,(int)pozicio.Y, 74, 74);
if (Keyboard.GetState().IsKeyDown(Keys.D))
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(hatter, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), null, Color.White); // Pálya
for (int i = 0; i < 13; i++)
{
for (int j = 0; j <24; j++)
{
if (mezo[i, j] == 1)
{
palya1 = new Rectangle(j * 74, i * 74, 74, 74);
spriteBatch.Draw(palya, palya1, Color.White);
}
}
}
/*
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 24; j++)
{
if (mezo[i, j] == 2)
{
jatekos1 = new Rectangle(j * 74, i * 74, 74, 74);
spriteBatch.Draw(jatekos, jatekos1, Color.White);
KeyboardState keybState = Keyboard.GetState();
if (keybState.IsKeyDown(Keys.Left) && jatekos1.X =mezo[i,j] ==0)
jatekos1.X -= 10;
else if (keybState.IsKeyDown(Keys.Right))
jatekos1.X += 10;
else if (keybState.IsKeyDown(Keys.Up))
jatekos1.Y -= 10;
else if (keybState.IsKeyDown(Keys.Down))
jatekos1.Y += 10;
}
}
}
*/
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 24; j++)
{
if (mezo[i, j] == 3)
{
lezerkard1 = new Rectangle(j * 74, i * 74, 74, 74);
spriteBatch.Draw(lezerkard, lezerkard1, Color.White);
}
}
}
spriteBatch.Draw(jatekos, jatekos1, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Nagyon nehézkes ez a kód, nem objektum orientált és tele van magic numberekkel.
Nem csoda, hogy nem átlátható.
Nem guglizni kellene, hanem gondolkozni.
Most azt nem tudod ellenőrizni, hogy falnál van-e? Vagy, hogy amerre tolni akar mi van abba az irányba?
Miért nem?
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D palya;
Rectangle palya1;
Texture2D jatekos;
Rectangle jatekos1;
Texture2D hatter;
Texture2D lezerkard;
Rectangle lezerkard1;
const int meret = 10;
int[,] mezo = new int[,]{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,3,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,2,1,0,1,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,0,1,1,1,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,3,0,0,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,3,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
Tehát ezen tömbön belül szeretném a "jatekost" mozgatni de hogyan?
Kapcsolódó kérdések:
Minden jog fenntartva © 2024, www.gyakorikerdesek.hu
GYIK | Szabályzat | Jogi nyilatkozat | Adatvédelem | Cookie beállítások | WebMinute Kft. | Facebook | Kapcsolat: info(kukac)gyakorikerdesek.hu
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!