diff --git a/NukkuMatti -peli b/NukkuMatti -peli new file mode 100644 index 0000000000000000000000000000000000000000..cdb94874869a22684c5d4397c16fbccb277bb0d2 --- /dev/null +++ b/NukkuMatti -peli @@ -0,0 +1,199 @@ +using System; +using System.Collections.Generic; +using Jypeli; + +// Eero Salmen tekemä tasohyppelypeli osana ohjelmointi 1 -kurssia. Pelin nimi on NukkuMatti. +// Pelaajan tehtävänä on kuljettaa NukkuMattia tasolta toiselle, tippumatta alas. + +public class Tasohyppely : PhysicsGame +{ + // Muutama vakio arvo määritelty pelin mekaniikalle, jotta ne on helpommin hallittavissa samassa paikassa + + private const double GravityStrength = -800; + private const double HyppyVoima = 1100; + private const double TasoLeveys = 100; + private const double TasoKorkeus = 10; + private const double PelaajaWidth = 100; + private const double PelaajaHeight = 100; + private const double AlkuperainenHyppyNopeus = 1000; + private const double TasoPoistoMarginaali = 200; + private const double StartingHeight = -20; + + private PhysicsObject pelaaja; + private bool peliKaynnissa = true; + private GameObject taustakuva; + private Label gameOverTeksti; + private int score = 0; + private List<PhysicsObject> tasot = new List<PhysicsObject>(); + + public override void Begin() + { + // Asetetaan painovoima + Gravity = new Vector(0, GravityStrength); + + // Luodaan kentän elementit + LuoKentta(); + LisaaOhjaimet(); + LuoGameOverTeksti(); + + // Ajastin sille, kyinka usein uusia tasoja ilmestyy pelikenttään + Timer tasoAjastin = new Timer(); + tasoAjastin.Interval = 0.8; + tasoAjastin.Timeout += LuoTaso; + tasoAjastin.Start(); + + // Hyppynopeus + pelaaja.Velocity = new Vector(0, AlkuperainenHyppyNopeus); + } + + private void LuoKentta() + { + LuoTausta(); + LuoPelaaja(); + + // Asetetaan pelaajan ominaisuudet ja lisätään kentälle + pelaaja.Position = new Vector(0, StartingHeight); + pelaaja.CanRotate = false; + pelaaja.Restitution = 0; + Add(pelaaja); + + // Luodaan aloitustaso + PhysicsObject aloitusTaso = PhysicsObject.CreateStaticObject(200, TasoKorkeus); + aloitusTaso.Position = new Vector(0, -70); + aloitusTaso.Color = Color.White; + aloitusTaso.Tag = "taso"; + Add(aloitusTaso); + + // Laitetaan kamera seuraamaan pelaajaa + Camera.Follow(pelaaja); + + // Lisätään törmäysominaisuus + AddCollisionHandler(pelaaja, "taso", OsuiTasoon); + + // Luodaan muutama aloitustaso + for (int i = 0; i < 4; i++) + LuoTaso(); + } + + private void LuoTausta() + { + // Lisätään taustakuva ja sijoitetaan se taustalle + taustakuva = new GameObject(Level.Width * 2, Level.Height * 2); + taustakuva.Image = LoadImage("taivas.jpeg"); + taustakuva.Position = Camera.Position; + Add(taustakuva, -1); + } + + private void LuoGameOverTeksti() + { + // Asetetaan pelin päättymis teksti + gameOverTeksti = new Label("HÄVISIT PELIN"); + gameOverTeksti.TextColor = Color.Red; + gameOverTeksti.Font = Font.DefaultBold; + gameOverTeksti.Position = new Vector(0, 100); + gameOverTeksti.IsVisible = false; + Add(gameOverTeksti); + } + + private void LuoPelaaja() + { + //Luodaan hahmo + pelaaja = new PhysicsObject(PelaajaWidth, PelaajaHeight); + pelaaja.Image = LoadImage("nukkumatti.png"); + pelaaja.Restitution = 0; + } + + private void LuoTaso() + { + //Luodaan tasoja + double x = RandomGen.NextDouble(Level.Left + 100, Level.Right - 100); + double y = Camera.ScreenToWorld(new Vector(0, Screen.Height / 2)).Y + RandomGen.NextDouble(10, 10); + + PhysicsObject taso = PhysicsObject.CreateStaticObject(TasoLeveys, TasoKorkeus); + taso.Position = new Vector(x, y); + taso.Color = Color.White; + taso.Tag = "taso"; + Add(taso); + + tasot.Add(taso); + } + + private void LisaaOhjaimet() + { + // Asetetaan komennot näppäimistöltä + Keyboard.Listen(Key.Left, ButtonState.Down, Liikuta, null, -400.0); + Keyboard.Listen(Key.Right, ButtonState.Down, Liikuta, null, 400.0); + Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu pelistä"); + } + + private void Liikuta(double nopeus) + { + if (peliKaynnissa) + pelaaja.Velocity = new Vector(nopeus, pelaaja.Velocity.Y); + } + + private void OsuiTasoon(PhysicsObject pelaaja, PhysicsObject taso) + { + // Törmäys pelaajan ja tason välillä + if (pelaaja.Velocity.Y > 0 && pelaaja.Position.Y < taso.Top) + { + pelaaja.IgnoresCollisionResponse = true; + return; + } + + if (pelaaja.Velocity.Y <= 0 && Math.Abs(pelaaja.Bottom - taso.Top) < 5) + { + pelaaja.Y = taso.Top + pelaaja.Height / 2; + pelaaja.Velocity = new Vector(pelaaja.Velocity.X, HyppyVoima); + } + + pelaaja.IgnoresCollisionResponse = false; + } + + private void PoistaPoistuvatTasot() + { + // Turhat tasot poistetaan + for (int i = tasot.Count - 1; i >= 0; i--) + { + if (tasot[i].Y < Camera.ScreenToWorld(new Vector(0, -Screen.Height / 2)).Y - TasoPoistoMarginaali) + { + tasot[i].Destroy(); + tasot.RemoveAt(i); + } + } + } + + private void LopetaPeli() + { + // Pelin lopettamiskomento + + if (peliKaynnissa) + { + peliKaynnissa = false; + gameOverTeksti.IsVisible = true; + Timer.SingleShot(3.0, Exit); + } + } + + protected override void Update(Time time) + { + base.Update(time); + + // Taustakuva päivittyy kameran liikkeen mukaisesti + taustakuva.Position = taustakuva.Position + (Camera.Position - taustakuva.Position) * 0.1; + + PoistaPoistuvatTasot(); + + if (pelaaja.Y < Level.Bottom - 100) + LopetaPeli(); + } +} + +public class Program +{ + public static void Main() + { + using (var peli = new Tasohyppely()) + peli.Run(); + } +} \ No newline at end of file diff --git a/NukkuMatti.sln b/NukkuMatti.sln deleted file mode 100644 index 1f8ec326405d45df4ebf2db4fdde703a7c4bdf3b..0000000000000000000000000000000000000000 --- a/NukkuMatti.sln +++ /dev/null @@ -1,16 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NukkuMatti", "NukkuMatti\NukkuMatti.csproj", "{8F811F58-345B-4BC6-83EA-12DD4B395340}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {8F811F58-345B-4BC6-83EA-12DD4B395340}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8F811F58-345B-4BC6-83EA-12DD4B395340}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8F811F58-345B-4BC6-83EA-12DD4B395340}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8F811F58-345B-4BC6-83EA-12DD4B395340}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection -EndGlobal