Пример #1
0
 static void DrawBackground(int level, Raylib_cs.Color bgColor, int score, bool drawScore)
 {
     DrawRectangle(0, screenHeight - 100, screenWidth, 100, bgColor);
     DrawRectangle(40, screenHeight - 150, 200, 50, bgColor);
     DrawRectangle(screenWidth - 240, screenHeight - 150, 200, 50, bgColor);
     DrawRectangle((screenWidth / 2) - 150, screenHeight - 175, 300, 75, bgColor);
     if (drawScore)
     {
         DrawText("Level: " + level.ToString(), 0, 0, 40, YELLOW);
         DrawText(score.ToString(), screenWidth / 2, 0, 40, YELLOW);
     }
 } //draws the rectangles and GUI
Пример #2
0
        public static int Main()
        {
            InitWindow(screenWidth, screenHeight, "Project");
            HideCursor();
            SetTargetFPS(60);
            bool stillGoing = true;

            Raylib_cs.Color background = new Raylib_cs.Color(200, 40, 0, 255);

            //declaring objects to exist
            PlayerCursor      player      = new PlayerCursor();
            Launcher          launcher1   = new Launcher(10, new Vector2(130, screenHeight - 150));
            Launcher          launcher2   = new Launcher(20, new Vector2(screenWidth / 2 - 10, screenHeight - 175));
            Launcher          launcher3   = new Launcher(10, new Vector2(screenWidth - 150, screenHeight - 150));
            Location          loc1        = new Location(new Vector2(275, screenHeight - 125));
            Location          loc2        = new Location(new Vector2(425, screenHeight - 125));
            Location          loc3        = new Location(new Vector2(575, screenHeight - 125));
            Location          loc4        = new Location(new Vector2(975, screenHeight - 125));
            Location          loc5        = new Location(new Vector2(1125, screenHeight - 125));
            Location          loc6        = new Location(new Vector2(1275, screenHeight - 125));
            List <Projectile> projectiles = new List <Projectile>();
            List <Incoming>   inbound     = new List <Incoming>();
            int   currentLevel            = 1;
            float incomingTime            = 0;
            int   remainingIncoming       = 20;
            float incomingSpeed           = 4;
            int   score           = 0;
            float levelTime       = 0;
            int   redirectCounter = 0;
            float incomingDelay   = 3f;
            bool  inGame          = false;
            bool  displayingScore = false;
            Menu  menu            = Menu.Start;
            int   highScore       = 0;

            if (File.Exists(Environment.CurrentDirectory + @"\highScore.txt"))
            {
                highScore = int.Parse(File.ReadAllText(Environment.CurrentDirectory + @"\highScore.txt"));
            }

            while (!WindowShouldClose() && stillGoing)  // Game loop continues until ESC pressed, window closed, or the boolean becomes false
            {
                List <GameObject> gameObjects = new List <GameObject>();
                {
                    {
                        gameObjects.Add(loc1);
                        gameObjects.Add(loc2);
                        gameObjects.Add(loc3);
                        gameObjects.Add(loc4);
                        gameObjects.Add(loc5);
                        gameObjects.Add(loc6);
                    }
                    foreach (Projectile pro in projectiles)
                    {
                        gameObjects.Add(pro);
                    }
                    foreach (Incoming IN in inbound)
                    {
                        gameObjects.Add(IN);
                    }
                }//fills the list

                BeginDrawing();
                ClearBackground(BLACK);
                DrawBackground(currentLevel, background, score, inGame);

                if (inGame)
                {
                    {
                        loc1.Draw();
                        loc2.Draw();
                        loc3.Draw();
                        loc4.Draw();
                        loc5.Draw();
                        loc6.Draw();
                    }// draws the locations
                    {
                        launcher1.DisplayText();
                        launcher2.DisplayText();
                        launcher3.DisplayText();
                    }//displays remaining projectiles on the launchers
                    for (int i = 0; i < projectiles.Count; i++)
                    {
                        if (!projectiles[i].active)
                        {
                            projectiles.RemoveAt(i);
                        }
                    }//removes projectiles no longer active
                    {
                        if (!(loc1.destroyed && loc2.destroyed && loc3.destroyed && loc4.destroyed && loc5.destroyed && loc6.destroyed))
                        {
                            if (IsKeyPressed(Raylib_cs.KeyboardKey.KEY_ONE) && launcher1.remaining > 0)
                            {
                                projectiles.Add(new Projectile(launcher1.speed, launcher1.pos, GetMousePosition()));
                                launcher1.remaining--;
                            }
                            if (IsKeyPressed(Raylib_cs.KeyboardKey.KEY_TWO) && launcher2.remaining > 0)
                            {
                                projectiles.Add(new Projectile(launcher2.speed, launcher2.pos, GetMousePosition()));
                                launcher2.remaining--;
                            }
                            if (IsKeyPressed(Raylib_cs.KeyboardKey.KEY_THREE) && launcher3.remaining > 0)
                            {
                                projectiles.Add(new Projectile(launcher3.speed, launcher3.pos, GetMousePosition()));
                                launcher3.remaining--;
                            }
                        }
                    }// spawns projectiles on key press

                    foreach (GameObject g in gameObjects)
                    {
                        g.Update();
                        g.Draw();
                    }//draws and updates the list

                    for (int i = 0; i < inbound.Count; i++)
                    {
                        bool cont = false;
                        foreach (Projectile pro in projectiles)
                        {
                            if (pro.exploding && Vector2.Distance(inbound[i].pos, pro.pos) < 50)
                            {
                                inbound.RemoveAt(i);
                                score += 10;
                                cont   = true;
                                Console.Beep(180, 75);
                            }
                        }
                        if (cont)
                        {
                            continue;
                        }

                        if (inbound[i].pos.Y > screenHeight - 125)
                        {
                            if (!inbound[i].redirected)
                            {
                                switch (inbound[i].target.X)
                                {
                                case 300:
                                    loc1.Destroy();
                                    inbound.RemoveAt(i);
                                    break;

                                case 450:
                                    loc2.Destroy();
                                    inbound.RemoveAt(i);
                                    break;

                                case 600:
                                    loc3.Destroy();
                                    inbound.RemoveAt(i);
                                    break;

                                case 1000:
                                    loc4.Destroy();
                                    inbound.RemoveAt(i);
                                    break;

                                case 1150:
                                    loc5.Destroy();
                                    inbound.RemoveAt(i);
                                    break;

                                case 1300:
                                    loc6.Destroy();
                                    inbound.RemoveAt(i);
                                    break;
                                }
                            }
                            else
                            {
                                switch (inbound[i].actualTarget.X)
                                {
                                case 300:
                                    loc1.Destroy();
                                    inbound.RemoveAt(i);
                                    break;

                                case 450:
                                    loc2.Destroy();
                                    inbound.RemoveAt(i);
                                    break;

                                case 600:
                                    loc3.Destroy();
                                    inbound.RemoveAt(i);
                                    break;

                                case 1000:
                                    loc4.Destroy();
                                    inbound.RemoveAt(i);
                                    break;

                                case 1150:
                                    loc5.Destroy();
                                    inbound.RemoveAt(i);
                                    break;

                                case 1300:
                                    loc6.Destroy();
                                    inbound.RemoveAt(i);
                                    break;
                                }
                            }
                        }
                    }//checks if incomings are landed or intercepted

                    incomingTime += GetFrameTime();
                    if (incomingTime > incomingDelay)
                    {
                        Random random          = new Random();
                        int    incomingTarget  = random.Next(1, 7);
                        int    incomingTarget2 = random.Next(1, 7);


                        bool validTarget  = false;
                        bool validTarget2 = false;

                        if (!(loc1.destroyed && loc2.destroyed && loc3.destroyed && loc4.destroyed && loc5.destroyed && loc6.destroyed) && remainingIncoming > 0)
                        {
                            if (redirectCounter < 100)
                            {
                                while (!validTarget)
                                {
                                    switch (incomingTarget)
                                    {
                                    case 1:
                                        if (!loc1.destroyed)
                                        {
                                            inbound.Add(new Incoming(incomingSpeed, loc1.pos + new Vector2(25, 0)));
                                            validTarget = true;
                                        }
                                        break;

                                    case 2:
                                        if (!loc2.destroyed)
                                        {
                                            inbound.Add(new Incoming(incomingSpeed, loc2.pos + new Vector2(25, 0)));
                                            validTarget = true;
                                        }
                                        break;

                                    case 3:
                                        if (!loc3.destroyed)
                                        {
                                            inbound.Add(new Incoming(incomingSpeed, loc3.pos + new Vector2(25, 0)));
                                            validTarget = true;
                                        }
                                        break;

                                    case 4:
                                        if (!loc4.destroyed)
                                        {
                                            inbound.Add(new Incoming(incomingSpeed, loc4.pos + new Vector2(25, 0)));
                                            validTarget = true;
                                        }
                                        break;

                                    case 5:
                                        if (!loc5.destroyed)
                                        {
                                            inbound.Add(new Incoming(incomingSpeed, loc5.pos + new Vector2(25, 0)));
                                            validTarget = true;
                                        }
                                        break;

                                    case 6:
                                        if (!loc6.destroyed)
                                        {
                                            inbound.Add(new Incoming(incomingSpeed, loc6.pos + new Vector2(25, 0)));
                                            validTarget = true;
                                        }
                                        break;
                                    }//decides where each Incoming is pointed
                                    incomingTarget = random.Next(1, 7);
                                }
                                incomingTime = 0;
                                remainingIncoming--;
                                Console.Beep(90, 75);
                                redirectCounter += currentLevel * 2;
                            }
                            else
                            {
                                Vector2 vect1 = new Vector2(0, 0);
                                Vector2 vect2 = new Vector2(0, 0);
                                while (!validTarget)
                                {
                                    switch (incomingTarget)
                                    {
                                    case 1:
                                        if (!loc1.destroyed)
                                        {
                                            vect1       = loc1.pos + new Vector2(25, 0);
                                            validTarget = true;
                                        }
                                        break;

                                    case 2:
                                        if (!loc2.destroyed)
                                        {
                                            vect1       = loc2.pos + new Vector2(25, 0);
                                            validTarget = true;
                                        }
                                        break;

                                    case 3:
                                        if (!loc3.destroyed)
                                        {
                                            vect1       = loc3.pos + new Vector2(25, 0);
                                            validTarget = true;
                                        }
                                        break;

                                    case 4:
                                        if (!loc4.destroyed)
                                        {
                                            vect1       = loc4.pos + new Vector2(25, 0);
                                            validTarget = true;
                                        }
                                        break;

                                    case 5:
                                        if (!loc5.destroyed)
                                        {
                                            vect1       = loc5.pos + new Vector2(25, 0);
                                            validTarget = true;
                                        }
                                        break;

                                    case 6:
                                        if (!loc6.destroyed)
                                        {
                                            vect1       = loc5.pos + new Vector2(25, 0);
                                            validTarget = true;
                                        }
                                        break;
                                    }
                                    incomingTarget = random.Next(1, 7);
                                }
                                while (!validTarget2)
                                {
                                    switch (incomingTarget2)
                                    {
                                    case 1:
                                        if (!loc1.destroyed)
                                        {
                                            vect2        = loc1.pos + new Vector2(25, 0);
                                            validTarget2 = true;
                                        }
                                        break;

                                    case 2:
                                        if (!loc2.destroyed)
                                        {
                                            vect2        = loc2.pos + new Vector2(25, 0);
                                            validTarget2 = true;
                                        }
                                        break;

                                    case 3:
                                        if (!loc3.destroyed)
                                        {
                                            vect2        = loc3.pos + new Vector2(25, 0);
                                            validTarget2 = true;
                                        }
                                        break;

                                    case 4:
                                        if (!loc4.destroyed)
                                        {
                                            vect2        = loc4.pos + new Vector2(25, 0);
                                            validTarget2 = true;
                                        }
                                        break;

                                    case 5:
                                        if (!loc5.destroyed)
                                        {
                                            vect2        = loc5.pos + new Vector2(25, 0);
                                            validTarget2 = true;
                                        }
                                        break;

                                    case 6:
                                        if (!loc6.destroyed)
                                        {
                                            vect2        = loc5.pos + new Vector2(25, 0);
                                            validTarget2 = true;
                                        }
                                        break;
                                    }
                                    incomingTarget2 = random.Next(1, 7);
                                }
                                inbound.Add(new Incoming(incomingSpeed, vect1, vect2));
                                incomingTime = 0;
                                remainingIncoming--;
                                Console.Beep(90, 75);
                                redirectCounter = 0;
                            }
                        }
                        else if (!(loc1.destroyed && loc2.destroyed && loc3.destroyed && loc4.destroyed && loc5.destroyed && loc6.destroyed) && remainingIncoming == 0)
                        {
                            levelTime += GetFrameTime();
                            if (levelTime > 3)
                            {
                                currentLevel++;

                                score += 5 * launcher1.remaining;
                                score += 5 * launcher3.remaining;
                                score += 10 * launcher2.remaining;
                                if (!loc1.destroyed)
                                {
                                    score += 15;
                                }
                                if (!loc2.destroyed)
                                {
                                    score += 15;
                                }
                                if (!loc3.destroyed)
                                {
                                    score += 15;
                                }
                                if (!loc4.destroyed)
                                {
                                    score += 15;
                                }
                                if (!loc5.destroyed)
                                {
                                    score += 15;
                                }
                                if (!loc6.destroyed)
                                {
                                    score += 15;
                                }


                                launcher1.remaining = 10;
                                launcher2.remaining = 10;
                                launcher3.remaining = 10;
                                loc1.destroyed      = false;
                                loc2.destroyed      = false;
                                loc3.destroyed      = false;
                                loc4.destroyed      = false;
                                loc5.destroyed      = false;
                                loc6.destroyed      = false;
                                incomingSpeed      *= 1.05f;
                                if (incomingSpeed > 20f)
                                {
                                    incomingSpeed = 20f;
                                }
                                incomingDelay *= 0.95f;
                                if (incomingDelay < 0.25f)
                                {
                                    incomingDelay = 0.25f;
                                }
                                background        = new Raylib_cs.Color(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255), 255);
                                incomingTime      = 0;
                                remainingIncoming = 20;
                                levelTime         = 0;

                                for (int i = 5; i < 51; i += 5)
                                {
                                    if (currentLevel > i)
                                    {
                                        remainingIncoming++;
                                    }
                                }

                                if (currentLevel > 50)
                                {
                                    int additionalIncoming = currentLevel - 50;

                                    launcher1.remaining += additionalIncoming;
                                    launcher3.remaining += additionalIncoming;
                                    remainingIncoming   += remainingIncoming * 2;
                                }
                            }
                        } //advancing levels
                    }     //generates Incomings, ends level if the allotted number have been spawned

                    if (loc1.destroyed && loc2.destroyed && loc3.destroyed && loc4.destroyed && loc5.destroyed && loc6.destroyed)
                    {
                        DrawText("GAME END", (screenWidth / 2) - 500, (screenHeight / 2) - 100, 200, new Raylib_cs.Color(100, 0, 0, 255));
                        if (IsKeyPressed(Raylib_cs.KeyboardKey.KEY_ENTER))
                        {
                            if (highScore < score)
                            {
                                highScore = score;
                                File.WriteAllText(Environment.CurrentDirectory + @"\highScore.txt", highScore.ToString());
                            }
                            inGame = false;
                        }
                    }//the game over screen
                }
                else
                {
                    DrawTitle(menu, displayingScore, highScore);
                    if (IsKeyPressed(Raylib_cs.KeyboardKey.KEY_ENTER))
                    {
                        switch (menu)
                        {
                        case Menu.Start:
                        {
                            inGame            = true;
                            currentLevel      = 1;
                            incomingTime      = 0;
                            remainingIncoming = 20;
                            incomingSpeed     = 4;
                            score             = 0;
                            levelTime         = 0;
                            redirectCounter   = 0;
                            incomingDelay     = 3f;
                            loc1.destroyed    = false;
                            loc2.destroyed    = false;
                            loc3.destroyed    = false;
                            loc4.destroyed    = false;
                            loc5.destroyed    = false;
                            loc6.destroyed    = false;
                        }
                        break;

                        case Menu.Quit:
                            stillGoing = false;
                            break;

                        case Menu.Score:
                            displayingScore = !displayingScore;
                            break;
                        }
                    }
                    if (!displayingScore)
                    {
                        if (IsKeyPressed(Raylib_cs.KeyboardKey.KEY_UP))
                        {
                            switch (menu)
                            {
                            case Menu.Start:
                                menu = Menu.Score;
                                break;

                            case Menu.Quit:
                                menu = Menu.Start;
                                break;

                            case Menu.Score:
                                menu = Menu.Quit;
                                break;
                            }
                        }
                        if (IsKeyPressed(Raylib_cs.KeyboardKey.KEY_DOWN))
                        {
                            switch (menu)
                            {
                            case Menu.Start:
                                menu = Menu.Quit;
                                break;

                            case Menu.Quit:
                                menu = Menu.Score;
                                break;

                            case Menu.Score:
                                menu = Menu.Start;
                                break;
                            }
                        }
                    }
                }//game hasn't started yet


                player.Update();
                player.Draw();

                EndDrawing();
            }
            CloseWindow();

            return(0);
        }