public override void Control(GameHistory history) { if (Keyboard.IsKeyPressed(Keyboard.Key.W)) { dir = (int)state.up; currentSpeed = speedBackward; sprite.TextureRect = new IntRect(0, 0, 32, 32); } if (Keyboard.IsKeyPressed(Keyboard.Key.S)) { dir = (int)state.down; currentSpeed = speedForward; sprite.TextureRect = new IntRect(32, 0, 32, 32); } if (Keyboard.IsKeyPressed(Keyboard.Key.D)) { dir = (int)state.right; currentSpeed = speedForward; sprite.TextureRect = new IntRect(96, 0, 32, 32); } if (Keyboard.IsKeyPressed(Keyboard.Key.A)) { dir = (int)state.left; currentSpeed = speedBackward; sprite.TextureRect = new IntRect(64, 0, 32, 32); } }
public virtual void Control(GameHistory history) { if (Keyboard.IsKeyPressed(Keyboard.Key.Up)) { dir = (int)state.up; currentSpeed = speedBackward; sprite.TextureRect = new IntRect(0, 0, 32, 32); } if (Keyboard.IsKeyPressed(Keyboard.Key.Down)) { dir = (int)state.down; currentSpeed = speedForward; sprite.TextureRect = new IntRect(32, 0, 32, 32); } if (Keyboard.IsKeyPressed(Keyboard.Key.Right)) { dir = (int)state.right; currentSpeed = speedForward; sprite.TextureRect = new IntRect(96, 0, 32, 32); } if (Keyboard.IsKeyPressed(Keyboard.Key.Left)) { dir = (int)state.left; currentSpeed = speedBackward; sprite.TextureRect = new IntRect(64, 0, 32, 32); } if (Keyboard.IsKeyPressed(Keyboard.Key.LControl)) { if (isShoot == false) { bullet = new Bullet("bullet.png", Color.Green, x, y, 0.4F, dir); isShoot = true; } } if (Keyboard.IsKeyPressed(Keyboard.Key.Num1)) { history.AddHistory(MementoSave()); } if (Keyboard.IsKeyPressed(Keyboard.Key.Num2)) { MementoLoad(history.GetHistory()); } }
//Основыне методы public virtual void update(float time, int[,] tileMap, ref RenderWindow a, ref FieldMap h, Player1 ob1, Player2 ob2, GameHistory history) { Control(history); switch (dir) { case (int)state.right: dx = currentSpeed; dy = 0; break; case (int)state.left: dx = -currentSpeed; dy = 0; break; case (int)state.down: dx = 0; dy = currentSpeed; break; case (int)state.up: dx = 0; dy = -currentSpeed; break; default: break; } x += dx * time; y += dy * time; currentSpeed = 0; //Тестируемый блок СorrectionPosition(); sprite.Position = new Vector2f(x, y); if (PlayersCollisium(ob1, ob2)) { if (dir == 1) { sprite.Position = new Vector2f(x += 2, y); } else if (dir == 0) { sprite.Position = new Vector2f(x -= 2, y); } if (dir == 3) { sprite.Position = new Vector2f(x, y += 2); } else if (dir == 2) { sprite.Position = new Vector2f(x, y -= 2); } } interactionWithMap(tileMap, ref a); }
public override void update(float time, int[,] tileMap, ref RenderWindow a, ref FieldMap h, Player1 ob1, Player2 ob2, GameHistory history) { Control(history); switch (dir) { case 0: dx = currentSpeed; dy = 0; break; case 1: dx = -currentSpeed; dy = 0; break; case 2: dx = 0; dy = currentSpeed; break; case 3: dx = 0; dy = -currentSpeed; break; default: break; } x += dx * time; y += dy * time; currentSpeed = 0; СorrectionPosition(); sprite.Position = new Vector2f(x, y); if (PlayersCollisium(ob1, ob2)) { if (dir == 1) { sprite.Position = new Vector2f(x += 2, y); } else if (dir == 0) { sprite.Position = new Vector2f(x -= 2, y); } if (dir == 3) { sprite.Position = new Vector2f(x, y += 2); } else if (dir == 2) { sprite.Position = new Vector2f(x, y -= 2); } } interactionWithMap(tileMap, ref a); }
static void Main(string[] args) { //------------------------------------------------Bound Code------------------------------------------------// window = new RenderWindow(new VideoMode(1280, 800), "Battle City"); window.SetVerticalSyncEnabled(true); window.Closed += WinClosed; Image icon = new Image("..\\Source\\Textures\\panzer.png"); window.SetIcon(64, 64, icon.Pixels); musicThemes = new [] { new Music("..\\Source\\Sounds\\theme1.ogg"), new Music("..\\Source\\Sounds\\theme2.ogg") }; Thread musicThread = new Thread(new ThreadStart(SwitchMusic)); musicThread.Start(); GameHistory history = new GameHistory(); //------------------------------------------------Game Code------------------------------------------------// FieldMap map = new FieldMap("map.png"); Player1 player1 = new Player1("players1.png", "Vitaliy", true, Color.Green, 544, 732); Player2 player2 = new Player2("players1.png", "Davidiy", true, Color.Red, 672, 38); Clock clock = new Clock(); Debug f = new Debug(); while (window.IsOpen) { window.DispatchEvents(); float time = clock.ElapsedTime.AsMicroseconds(); clock.Restart(); time = time / 800; player1.update(time, map.tileMap, ref window, ref map, player1, player2, history); player2.update(time, map.tileMap, ref window, ref map, player1, player2, history); if (player1.isShoot) { player1.bullet.update(time, map.tileMap, ref window, player1, player2); } f.FConsole("P1 \n X - " + player1.X + " Y - " + player1.Y + "\n\nP2 \n X - " + player2.X + " Y - " + player2.Y); //window.Clear(Color.Black); //------------------------------------------------Draw Code------------------------------------------------// map.Draw(ref window); window.Draw(player1.Sprite); window.Draw(player2.Sprite); if (player1.isShoot) { window.Draw(player1.bullet.Sprite); } f.Print(); //------------------------------------------------Show Code------------------------------------------------// window.Display(); } }