コード例 #1
0
ファイル: Program.cs プロジェクト: Bone155/Homework
        public static int Main()
        {
            // Initialization
            //--------------------------------------------------------------------------------------
            Random rand   = new Random();
            Player player = new Player();

            Pickup[] pickup  = new Pickup[70];
            Enemy[]  enemies = new Enemy[50];
            Enemy    enemy   = new Enemy();
            Gun      gun     = new Gun();

            int screenWidth  = 1600;
            int screenHeight = 900;

            int health = player.health;
            int ammo   = player.ammo;
            int score  = player.score;
            int timer  = 0;

            //bool winState = false;
            //rl.InitAudioDevice();
            //var pick = rl.LoadAudioStream("powerUp1.oog");

            rl.InitWindow(screenWidth, screenHeight, "Assessment");

            //Create enemies
            Enemy.EnemyTexture("bat.png");
            for (int Enidx = 0; Enidx < enemies.Length; Enidx++)
            {
                enemies[Enidx]            = new Enemy();
                enemies[Enidx].Position.x = rand.Next(20, screenWidth - 20);
                enemies[Enidx].Position.y = rand.Next(30, screenHeight - 20);
            }

            //Create pickups
            Ammo.SetTexture("platformPack_item001.png");
            Health.SetTexture("platformPack_item017.png");
            Score.SetTexture("platformPack_item009.png");
            for (int idx = 0; idx < pickup.Length; idx++)
            {
                int pickChoice = rand.Next(0, 3);
                if (pickChoice == 0)
                {
                    pickup[idx]            = new Ammo();
                    pickup[idx].Position.x = rand.Next(20, screenWidth - 20);
                    pickup[idx].Position.y = rand.Next(30, screenHeight - 20);
                }
                if (pickChoice == 1)
                {
                    pickup[idx]            = new Health();
                    pickup[idx].Position.x = rand.Next(20, screenWidth - 20);
                    pickup[idx].Position.y = rand.Next(30, screenHeight - 20);
                }
                if (pickChoice == 2)
                {
                    pickup[idx]            = new Score();
                    pickup[idx].Position.x = rand.Next(20, screenWidth - 20);
                    pickup[idx].Position.y = rand.Next(30, screenHeight - 20);
                }
            }

            player.Position.x = rand.Next(20, screenWidth - 20);
            player.Position.y = rand.Next(30, screenHeight - 20);

            rl.SetTargetFPS(60);
            //--------------------------------------------------------------------------------------

            // Main game loop
            while (!rl.WindowShouldClose())    // Detect window close button or ESC key
            {
                // Update
                //----------------------------------------------------------------------------------
                // TODO: Update your variables here
                //----------------------------------------------------------------------------------
                player.RunUpdate();
                timer++;
                // Draw
                //----------------------------------------------------------------------------------
                rl.BeginDrawing();

                rl.ClearBackground(Color.LIGHTGRAY);
                rl.DrawText("Score: " + score, 50, 50, 12, Color.WHITE);
                rl.DrawText("Health: " + health, 50, 75, 12, Color.WHITE);
                rl.DrawText("Time: " + timer / 60, screenWidth - 200, 50, 12, Color.WHITE);
                rl.DrawText("Ammo: " + ammo, screenWidth - 200, 80, 12, Color.WHITE);

                //if (timer/60 >= 30 && winState == false)
                //{
                //    rl.DrawText("Game Over", 250, 50, 20, Color.ORANGE);
                //    rl.DrawText("BOI", 250, 75, 20, Color.ORANGE);
                //    player.speed = 0;
                //}
                player.Draw();
                foreach (Enemy en in enemies)
                {
                    if (en.isEnabled)
                    {
                        en.Draw();
                        if (CheckCollisionV2(player, en))
                        {
                            health--;
                            score++;
                        }

                        //if (en.Position.x > player.Position.x)
                        //    en.Position.x-= 3;
                        //if (en.Position.x < player.Position.x)
                        //    en.Position.x+= 3;
                        //if (en.Position.y > player.Position.y)
                        //    en.Position.y-= 3;
                        //if (en.Position.y < player.Position.y)
                        //    en.Position.y+= 3;

                        if (health <= 0)
                        {
                            health = 0;
                            rl.DrawText("Game Over", 250, 50, 20, Color.ORANGE);
                            rl.DrawText("BOI", 250, 75, 20, Color.ORANGE);
                            player.speed = 0;
                        }
                    }
                }

                foreach (Pickup pick in pickup)
                {
                    if (pick.Enabled)
                    {
                        switch (pick.up)
                        {
                        case Pickup.PickupType.Ammo:
                            ((Ammo)pick).Draw();
                            ammo += CheckCollisionV1(player, pick) ? 1 : 0;    //adds one to variable
                            break;

                        case Pickup.PickupType.Health:
                            ((Health)pick).Draw();
                            health += CheckCollisionV1(player, pick) ? 1 : 0;
                            break;

                        case Pickup.PickupType.Score:
                            ((Score)pick).Draw();
                            if (CheckCollisionV1(player, pick))
                            {
                                score += rand.Next(1, 11);
                            }
                            break;

                        default:
                            break;
                        }

                        //rl.PlayAudioStream(pick);
                        //score.score += CheckCollisionV1(player, pick) ? 1 : 0;
                    }
                }
                //Player screen Warps
                if (player.Position.x <= 0)
                {
                    player.Position.x = screenWidth - 1;
                }
                if (player.Position.x >= screenWidth)
                {
                    player.Position.x = 0;
                }
                if (player.Position.y <= 0)
                {
                    player.Position.y = screenHeight - 1;
                }
                if (player.Position.y >= screenHeight)
                {
                    player.Position.y = 0;
                }

                rl.EndDrawing();
                //----------------------------------------------------------------------------------
                //if (score.score >= 10)
                //{
                //    rl.DrawText("Oh goody, you survived", 250, 50, 20, Color.ORANGE);
                //    //winState = true;
                //    player.speed = 0;
                //}
            }

            // De-Initialization
            //--------------------------------------------------------------------------------------
            rl.CloseWindow();        // Close window and OpenGL context
            //--------------------------------------------------------------------------------------

            return(0);
        }
コード例 #2
0
        public void Init()
        {
            stopwatch.Start();
            lastTime = stopwatch.ElapsedMilliseconds;

            InitAudioDevice();
            snd_drive  = LoadSound("Resources/Sound/drive.ogg");
            snd_rotate = LoadSound("Resources/Sound/rotate.ogg");
            snd_fire   = LoadSound("Resources/Sound/fire.ogg");
            snd_load   = LoadSound("Resources/Sound/reload.ogg");


            for (int p = 0; p < 9; p++)
            {
                float b = (obsPositions.Length / 2);
                float c = p - b;
                obsPositions[p] = new Vector3((GetScreenWidth() / 2) + (p * 64) - (b * 64), (GetScreenHeight() / 2) + (((float)Math.Pow(c, 3)) + (9 * c)), 0);
            }

            Texture2D body1     = LoadTexture("Resources/tankBody_blue_outline.png");
            Texture2D body1a    = LoadTexture("Resources/tankBody_red_outline.png");
            Texture2D body2     = LoadTexture("Resources/tankBody_darkLarge_outline.png");
            Texture2D body3     = LoadTexture("Resources/tankBody_huge_outline.png");
            Texture2D bodyS     = LoadTexture("Resources/tankBody_dark.png");
            Texture2D barrel1   = LoadTexture("Resources/tankBlue_barrel1_outline.png");
            Texture2D barrel2   = LoadTexture("Resources/tankBlue_barrel2_outline.png");
            Texture2D barrel3   = LoadTexture("Resources/tankBlue_barrel3_outline.png");
            Texture2D s_barrel1 = LoadTexture("Resources/specialBarrel1_outline.png");
            Texture2D s_barrel2 = LoadTexture("Resources/specialBarrel2_outline.png");
            Texture2D s_barrel3 = LoadTexture("Resources/specialBarrel3_outline.png");
            Texture2D s_barrel4 = LoadTexture("Resources/specialBarrel4_outline.png");
            Texture2D s_barrel5 = LoadTexture("Resources/specialBarrel5_outline.png");
            Texture2D s_barrel6 = LoadTexture("Resources/specialBarrel6_outline.png");
            Texture2D s_barrel7 = LoadTexture("Resources/specialBarrel7_outline.png");
            Texture2D rocketL   = LoadTexture("Resources/barrelGrey_side.png");
            Texture2D bullet1   = LoadTexture("Resources/bulletDark1_outline.png");
            Texture2D bullet2   = LoadTexture("Resources/bulletDark2_outline.png");
            Texture2D bullet3   = LoadTexture("Resources/bulletDark3_outline.png");
            Texture2D bulletM   = LoadTexture("Resources/bulletDark1.png");
            Texture2D rocket1   = LoadTexture("Resources/spaceMissiles_003.png");
            Texture2D rocket2   = LoadTexture("Resources/spaceMissiles_006.png");
            Texture2D obs1      = LoadTexture("Resources/barricadeWood.png");
            Texture2D obs2      = LoadTexture("Resources/sandbagBeige.png");

            Body bodyBasic = new Body(body1, 100f, 1f, 100f, 0f);

            bodyBasic.Rename("Cruiser tank");
            Body bodyBasic2 = new Body(body1a, 100f, 1f, 100f, 0f);

            bodyBasic2.Rename("Cruiser tank");
            Body bodyBig = new Body(body2, 75f, 0.8f, 125f, 8f);

            bodyBig.Rename("Infantry tank");
            Body bodyBigger = new Body(body3, 50f, 0.6f, 150f, -32f);

            bodyBigger.Rename("Super-heavy tank");

            //bodies.Add(bodySmall);
            bodies.Add(bodyBasic);
            bodies.Add(bodyBig);
            bodies.Add(bodyBigger);

            bodies2.Add(bodyBasic2);
            bodies2.Add(bodyBig);
            bodies2.Add(bodyBigger);

            Ammo ammoAvg = new Ammo(bullet1, 1f, 600);
            Ammo ammoFst = new Ammo(bullet3, 0.75f, 700);
            Ammo ammoHvy = new Ammo(bullet2, 1.25f, 500);

            Barrel barrelAvg = new Barrel(s_barrel6, bullet1, 1f, 600, 59, 75, -8f, 0);

            barrelAvg.Rename("105mm cannon");
            barrelAvg.Details(snd_load, 45, false, 0.0f);
            Barrel barrelPrc = new Barrel(s_barrel7, bullet3, 0.75f, 700, 58, 60, -8f, 0);

            barrelPrc.Rename("Self-propelled Sabot");
            barrelPrc.Details(snd_load, 45, false, 0.0f);
            Barrel barrelHwz = new Barrel(s_barrel5, bullet2, 1.25f, 500, 59, 90, -8f, 0);

            barrelHwz.Rename("120mm Howitzer");
            barrelHwz.Details(snd_load, 45, false, 0.0f);
            Barrel barrel45g = new Barrel(s_barrel4, bulletM, 0.5f, 900, 28, 30, -16f, -4f); //20mm machine gun

            barrel45g.Rename("45mm gun");
            barrel45g.Details(snd_load, 45, false, 0.0f);
            Barrel barrelMrl = new Barrel(s_barrel1, rocket1, 2f, 850, 28, 120, -16f, 0);

            barrelMrl.Rename("Rocket launcher");
            barrelMrl.Details(snd_load, 45, false, 0.0f);
            Barrel barrelGui = new Barrel(rocketL, rocket1, 2f, 500, 90, 240, -24f, 0);

            barrelGui.Rename("Guided missile");
            barrelGui.Details(snd_load, 45, true, 3.0f);
            Barrel barrelMac = new Barrel(s_barrel4, bulletM, 0.5f, 900, 28, 30, -16f, -4f);

            barrels.Add(barrelAvg);
            barrels.Add(barrelPrc);
            barrels.Add(barrelHwz);
            barrels.Add(barrel45g);
            barrels.Add(barrelGui);

            body   = bodyBasic;
            barrel = barrelAvg;

            expSpr = new Texture2D[5]
            {
                LoadTexture("Resources/explosion1.png"),
                LoadTexture("Resources/explosion2.png"),
                LoadTexture("Resources/explosion3.png"),
                LoadTexture("Resources/explosion4.png"),
                LoadTexture("Resources/explosion5.png"),
            };
            expSprite.Load(expSpr);
            expSprite_.Load(expSpr);

            bulspr.Load("Resources/bulletBlue1_outline.png");
            bulspr.SetRotate(90 * (float)(Math.PI / 180.0f));
            bulspr.SetPosition(90, 0);

            bulspr_.Load("Resources/bulletBlue1_outline.png");
            bulspr_.SetRotate(90 * (float)(Math.PI / 180.0f));
            bulspr_.SetPosition(90, 0);

            explosionFX.AddChild(expSprite);
            explosionFX.ChangeLifetime(10);
            explosionFX_.AddChild(expSprite_);
            explosionFX_.ChangeLifetime(10);

            bullet.explosion  = explosionFX;
            bullet_.explosion = explosionFX_;

            bullet.AddChild(bulspr);
            bullet_.AddChild(bulspr_);

            bullet.myPoints.Add(new Vector3(-16, 16, 0));
            bullet.myPoints.Add(new Vector3(16, 16, 0));
            bullet.myPoints.Add(new Vector3(16, -16, 0));
            bullet.myPoints.Add(new Vector3(-16, -16, 0));
            bullet_.myPoints.Add(new Vector3(-16, 16, 0));
            bullet_.myPoints.Add(new Vector3(16, 16, 0));
            bullet_.myPoints.Add(new Vector3(16, -16, 0));
            bullet_.myPoints.Add(new Vector3(-16, -16, 0));

            p1.Init(bodies, barrels, 1);
            p2.Init(bodies2, barrels, 2);

            //
            for (int p = 0; p < obsPositions.Length; p++)
            {
                float        half  = obsPositions.Length / 2;
                float        third = obsPositions.Length / 3;
                SceneObject  buh   = new SceneObject(obsHealth, false);
                SpriteObject crap  = new SpriteObject(); crap.Load(obs1);
                buh.AddChild(crap);
                //buh.Rotate(pos.z * ((float)(Math.PI / 180.0f)));
                buh.SetPosition(obsPositions[p].x, obsPositions[p].y);
                crap.SetPosition(-crap.Width / 2, -crap.Height / 2);
                buh.myPoints.Add(new Vector3(-obsRad, obsRad, 0));
                buh.myPoints.Add(new Vector3(obsRad, obsRad, 0));
                buh.myPoints.Add(new Vector3(obsRad, -obsRad, 0));
                buh.myPoints.Add(new Vector3(-obsRad, -obsRad, 0));
                obstacles.Add(buh);
                if (p < (int)third - 1 || p > (int)(third * 2) - 1)
                {
                    crap.Load(obs2);
                }
            }
            //
            p1.Place(new Vector2(GetScreenWidth() / 4, GetScreenHeight() / 4 * 3));
            p2.Place(new Vector2(GetScreenWidth() / 4 * 3, GetScreenHeight() / 4));
        }
コード例 #3
0
        public static int Main()
        {
            // Initialization
            //--------------------------------------------------------------------------------------
            Random rand   = new Random();
            Player player = new Player();

            Bullet[] bullets  = new Bullet[10];
            Pickup[] pickup   = new Pickup[70];
            Enemy[]  enemies  = new Enemy[50];
            string[] contents = File.LoadFile("GO.txt");

            int screenWidth  = 1000;
            int screenHeight = 900;

            int    health = player.health;
            int    ammo   = player.ammo;
            int    score  = player.score;
            int    timer  = 0;
            int    bulletpointer;
            int    gameOverTimer = 0;
            bool   winState      = false;
            string username      = "";

            Console.WriteLine("What is your name?");
            username = Console.ReadLine();

            rl.InitWindow(screenWidth, screenHeight, "Assessment");

            //Create enemies
            Enemy.EnemyTexture("bat.png");

            for (int Enidx = 0; Enidx < enemies.Length; Enidx++)
            {
                enemies[Enidx] = new Enemy();

                enemies[Enidx].Position.x = rand.Next(20, screenWidth - 20);
                enemies[Enidx].Position.y = rand.Next(30, screenHeight - 20);
            }

            //Create bullets
            for (int idx = 0; idx < bullets.Length; idx++)
            {
                bullets[idx] = new Bullet();
            }

            //Create pickups
            Ammo.SetTexture("platformPack_item001.png");
            Health.SetTexture("platformPack_item017.png");
            Score.SetTexture("platformPack_item009.png");

            for (int idx = 0; idx < pickup.Length; idx++)
            {
                int pickChoice = rand.Next(0, 3);

                if (pickChoice == 0)
                {
                    pickup[idx]            = new Ammo();
                    pickup[idx].Position.x = rand.Next(20, screenWidth - 20);
                    pickup[idx].Position.y = rand.Next(30, screenHeight - 20);
                }
                if (pickChoice == 1)
                {
                    pickup[idx]            = new Health();
                    pickup[idx].Position.x = rand.Next(20, screenWidth - 20);
                    pickup[idx].Position.y = rand.Next(30, screenHeight - 20);
                }
                if (pickChoice == 2)
                {
                    pickup[idx]            = new Score();
                    pickup[idx].Position.x = rand.Next(20, screenWidth - 20);
                    pickup[idx].Position.y = rand.Next(30, screenHeight - 20);
                }
            }

            player.Position.x = rand.Next(20, screenWidth - 20);
            player.Position.y = rand.Next(30, screenHeight - 20);

            rl.SetTargetFPS(60);

            //--------------------------------------------------------------------------------------
            // Main game loop
            while (!rl.WindowShouldClose()) // Detect window close button or ESC key
            {
                // Update
                player.RunUpdate();
                timer++;

                // Draw
                rl.BeginDrawing();

                rl.ClearBackground(Color.LIGHTGRAY);
                rl.DrawText("Score: " + score, 50, 50, 12, Color.BLACK);
                rl.DrawText("Health: " + health, 50, 75, 12, Color.BLACK);
                rl.DrawText("Time: " + timer / 60, screenWidth - 200, 50, 12, Color.BLACK);
                rl.DrawText("Ammo: " + ammo, screenWidth - 200, 80, 12, Color.BLACK);

                if (timer / 60 >= 60 && winState == false)
                {
                    rl.DrawText(contents[0], 250, 50, 20, Color.ORANGE);
                    Console.WriteLine(gameOverTimer);

                    player.speed = 0;
                    ++gameOverTimer;

                    if (gameOverTimer > 60 * 5)
                    {
                        File.SaveScore(username, score);
                        return(0);
                    }
                }

                player.Draw();

                bool NotAllEnemiesDead = false;

                foreach (Enemy en in enemies)
                {
                    if (en.Enabled)
                    {
                        NotAllEnemiesDead = true;

                        en.Draw();

                        if (CheckCollisionV2(player, en))
                        {
                            health--;
                        }

                        if (en.Position.x > player.Position.x)
                        {
                            en.Position.x -= 1 + rand.Next(0, 1);
                        }

                        if (en.Position.x < player.Position.x)
                        {
                            en.Position.x += 1 + rand.Next(0, 1);
                        }

                        if (en.Position.y > player.Position.y)
                        {
                            en.Position.y -= 1 + rand.Next(0, 1);
                        }

                        if (en.Position.y < player.Position.y)
                        {
                            en.Position.y += 1 + rand.Next(0, 1);
                        }

                        if (health <= 0)
                        {
                            health = 0;
                            rl.DrawText(contents[0], 250, 50, 20, Color.ORANGE);
                            player.speed = 0;

                            ++gameOverTimer;

                            if (gameOverTimer > 60 * 5)
                            {
                                File.SaveScore(username, score);
                                return(0);
                            }
                        }
                    }
                }

                if (NotAllEnemiesDead == false)
                {
                    //Display win
                    rl.DrawText("You won!", 250, 50, 20, Color.ORANGE);

                    winState = true;
                    ++gameOverTimer;

                    if (gameOverTimer > 60 * 5)
                    {
                        File.SaveScore(username, score);
                        return(0);
                    }
                }

                if (rl.IsKeyPressed(KeyboardKey.KEY_SPACE) && ammo > 0)
                {
                    bulletpointer = 1; //bullet.findDisableBullet(bullets);
                    bullets[bulletpointer].Position.x = player.Position.x + 11;
                    bullets[bulletpointer].Position.y = player.Position.y + 10;
                    bullets[bulletpointer].bulletMove = true;
                    bullets[bulletpointer].Enabled    = true;
                    ammo--;
                }

                foreach (Bullet bu in bullets)
                {
                    if (bu.Enabled)
                    {
                        bu.Draw();

                        if (bu.bulletMove)
                        {
                            bu.Position.x += 5;
                        }

                        for (int i = 0; i < enemies.Length; ++i)
                        {
                            score += CheckCollisionV3(bu, enemies[i]) ? 1 : 0;
                        }

                        if (ammo <= 0)
                        {
                            ammo = 0;

                            bu.Enabled = false;

                            if (ammo > -1)
                            {
                                bu.Enabled = true;
                            }
                        }
                    }
                }

                foreach (Pickup pick in pickup)
                {
                    if (pick.Enabled)
                    {
                        switch (pick.up)
                        {
                        case Pickup.PickupType.Ammo:
                            ((Ammo)pick).Draw();
                            ammo += CheckCollisionV1(player, pick) ? 1 : 0;    //adds one to variable
                            break;

                        case Pickup.PickupType.Health:
                            ((Health)pick).Draw();
                            health += CheckCollisionV1(player, pick) ? 1 : 0;
                            break;

                        case Pickup.PickupType.Score:
                            ((Score)pick).Draw();
                            if (CheckCollisionV1(player, pick))
                            {
                                score += rand.Next(1, 11);
                            }
                            break;

                        default:
                            break;
                        }

                        //rl.PlayAudioStream(pick);
                    }
                }

                //Player screen Warps
                if (player.Position.x <= 0)
                {
                    player.Position.x = screenWidth - 1;
                }

                if (player.Position.x >= screenWidth)
                {
                    player.Position.x = 0;
                }

                if (player.Position.y <= 0)
                {
                    player.Position.y = screenHeight - 1;
                }

                if (player.Position.y >= screenHeight)
                {
                    player.Position.y = 0;
                }

                rl.EndDrawing();
                //----------------------------------------------------------------------------------
            }

            // De-Initialization
            //--------------------------------------------------------------------------------------
            rl.CloseWindow(); // Close window and OpenGL context
            //--------------------------------------------------------------------------------------

            return(0);
        }