public bool Update(ref float overflowX) { bool endReached = false; if (IsAlive) { float deltaX = Velocity.X * GfxTools.Win.deltaTime; float deltaY = Velocity.Y * GfxTools.Win.deltaTime; Position.X += deltaX; Position.Y += deltaY; float maxX = Position.X + width / 2; float minX = Position.X - width / 2; if (maxX > GfxTools.Win.width - distToSide) { overflowX = maxX - (GfxTools.Win.width - distToSide); endReached = true; } else if (minX < distToSide) { overflowX = minX - distToSide; endReached = true; } //sprite.Translate(deltaX, deltaY); TranslateSprite(new Vector2(deltaX, deltaY)); if (Position.Y + height / 2 >= Game.GetPlayer().GetPosition().Y) { EnemyMgr.Landed = true; } else if (CanShoot) { nextShoot -= GfxTools.Win.deltaTime; if (nextShoot <= 0) { EnemyMgr.Shoot(this); nextShoot = RandomGenerator.GetRandom(1, 8); } } } else if (IsVisible) { for (int i = 0; i < sprite.Length; i++) { sprite[i].update(); } } return(endReached); }
public void Update() { float deltaX = speed * GfxTools.Win.deltaTime; position.X += deltaX; float maxX = position.X + width / 2; float minX = position.X - width / 2; if (maxX > GfxTools.Win.width - distToSide) { float overflowX = maxX - (GfxTools.Win.width - distToSide); position.X -= overflowX; deltaX -= overflowX; } else if (minX < distToSide) { float overflowX = minX - distToSide; position.X -= overflowX; deltaX -= overflowX; } //rectangles update //baseRect.Translate(deltaX, 0); //cannonRect.Translate(deltaX, 0); spriteobj.Translate(deltaX, 0); for (int i = 0; i < bulletPlayer.Length; i++) { if (bulletPlayer[i].IsAlive) { bulletPlayer[i].Update(); if (Game.BarriersCollides(bulletPlayer[i].Position, bulletPlayer[i].GetWidth())) { bulletPlayer[i].IsAlive = false; } if (EnemyMgr.CollideWithBullet(bulletPlayer[i])) { bulletPlayer[i].IsAlive = false; } } } }
//test static Game() { window = new Window(1900, 1000, "Space Invaders", PixelFormat.RGB); score = 0; GfxTools.Init(window); Vector2 playerPos; playerPos.X = window.width / 2; playerPos.Y = window.height - 20; EnemyMgr.Init(45, 3); switcher = 0; player = new Player(playerPos, new Color(22, 22, 200)); scoretext = new spriteText(new Vector2(220, 48), "000000"); PlayerSound.Play("Assets/spaceship.wav", "suono"); //music = new SoundPlayer(); //music.SoundLocation = "Assets/spaceship.wav"; //music.Load(); //music.PlayLooping(); bg = new backgorund("Assets/space_3.png"); bg2 = new backgorundMenu("Assets/space_menu.png"); m = new Menu(); barriers = new Barrier[3]; for (int i = 0; i < barriers.Length; i++) { barriers[i] = new Barrier(new Vector2((GfxTools.Win.width) * (i + 1) / 4, (GfxTools.Win.height / 2) + 300)); } //test }
public static void Play() { while (window.opened) { if (flag == 0) { //GfxTools.Clean(); bg2.DrawBg(); m.draw(); window.Blit(); if (GfxTools.Win.mouseLeft) { if (m.clickButton() == 1) { flag = 1; } else if (m.clickButton() == 2) { return; } } } else if (flag == 1) { totalTime += GfxTools.Win.deltaTime; //GfxTools.Clean(); bg.DrawBg(); //Input ; if (window.GetKey(KeyCode.Esc)) { return; } player.Input(); //Update EnemyMgr.Update(); player.Update(); if (!player.IsAlive || EnemyMgr.GetAlives() <= 0 || EnemyMgr.Landed) { Thread.Sleep(3000); return; } //Draw scoretext.Draw(); EnemyMgr.Draw(); player.Draw(); Game.DrawBarrier(); window.Blit(); } } }