Beispiel #1
0
        public static void Play()
        {
            while (window.opened)
            {
                if (!player.IsAlive || EnemyMgr.AllGone || EnemyMgr.Landed)
                {
                    return;
                }

                totalTime += GfxTools.Win.deltaTime;
                GfxTools.Clean();
                //Input
                if (window.GetKey(KeyCode.Esc))
                {
                    return;
                }

                player.Input();

                //Update
                EnemyMgr.Update();
                player.Update();

                //Draw
                for (int i = 0; i < barriers.Length; i++)
                {
                    barriers[i].Draw();
                }
                EnemyMgr.Draw();
                player.Draw();
                DrawGUI();

                window.Blit();
            }
        }
Beispiel #2
0
        static Game()
        {
            window  = new Window(800, 600, "Space Invaders", PixelFormat.RGB);
            gravity = 555.0f;
            GfxTools.Init(window);

            Vector2 playerPos;

            playerPos.X = window.width / 2;
            playerPos.Y = window.height - 30;

            EnemyMgr.Init(24, 3);

            player = new Player(playerPos, new Color(0, 0, 0));
            heart  = new Sprite("Assets/heart.png");

            barriers = new Barrier[3];

            for (int i = 0; i < barriers.Length; i++)
            {
                barriers[i] = new Barrier(window.width * (i + 1) / 4, (int)playerPos.Y - 80);
            }



            scoreSprite = new SpriteText(new Vector2(window.width / 4, 20), "000000");
        }
Beispiel #3
0
        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);
            sprite.Translate(deltaX, 0);

            for (int i = 0; i < bullets.Length; i++)
            {
                if (bullets[i].IsAlive)
                {
                    bullets[i].Update();

                    if (Game.BarriersCollides(bullets[i].Position, bullets[i].GetWidth() / 2))
                    {
                        bullets[i].IsAlive = false;
                    }
                    else if (EnemyMgr.CollideWithBullet(bullets[i]))
                    {
                        bullets[i].IsAlive = false;
                    }
                }
            }
        }
Beispiel #4
0
        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);
                    //Position.X -= overflowX;
                    //deltaX -= overflowX;
                    //velocity.X = -velocity.X;
                    endReached = true;
                }
                else if (minX < distToSide)
                {
                    overflowX = minX - distToSide;
                    //Position.X -= overflowX;
                    //deltaX -= overflowX;
                    //velocity.X = -velocity.X;
                    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, 15);
                    }
                }
            }
            else if (IsVisible)//he's dead but visible
            {
                for (int i = 0; i < sprite.Length; i++)
                {
                    if (sprite[i].IsVisibile)
                    {
                        sprite[i].Update();
                        if (sprite[i].IsVisibile == false)
                        {
                            visiblePixels--;
                            if (visiblePixels <= 0)
                            {
                                IsVisible = false;
                                EnemyMgr.OnAlienDisappears();
                            }
                        }
                    }
                }
            }
            return(endReached);
        }