From c73376806edb2601050c67de9d0aa8693e7a0282 Mon Sep 17 00:00:00 2001 From: Kopperoinen Paulus <paulus.j.kopperoinen@student.jyu.fi> Date: Thu, 23 Nov 2023 11:24:47 +0200 Subject: [PATCH] =?UTF-8?q?Sain=20muutettua=20omenien=20lis=C3=A4=C3=A4mis?= =?UTF-8?q?en=20sellaiseksi,=20ett=C3=A4=20nyt=20omenatkin=20lis=C3=A4t?= =?UTF-8?q?=C3=A4=C3=A4n=20vain=20tasariveille,=20kun=20ennen=20omena=20sa?= =?UTF-8?q?attoi=20synty=C3=A4=20puoleenv=C3=A4liin=20ruutuja.=20Yritin=20?= =?UTF-8?q?my=C3=B6s=20hieman=20kehitt=C3=A4=C3=A4=20omenoiden=20arpomisme?= =?UTF-8?q?kaniikkaa,=20mutta=20en=20ole=20varma=20toimiiko=20se=20kunnoll?= =?UTF-8?q?a,=20nyt=20peli=20alkaa=20olemaan=20muuten=20valmis,=20paitsi?= =?UTF-8?q?=20aion=20viel=C3=A4=20lis=C3=A4t=C3=A4=20dokumentointia=20ja?= =?UTF-8?q?=20koittaa=20poistaa=20riderin=20antamia=20keltaisia=20ilmoituk?= =?UTF-8?q?sia?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Matopeli/Matopeli/Matopeli.cs | 155 ++++++++++++++++++---------------- 1 file changed, 80 insertions(+), 75 deletions(-) diff --git a/Matopeli/Matopeli/Matopeli.cs b/Matopeli/Matopeli/Matopeli.cs index 66aacfa..fba9182 100644 --- a/Matopeli/Matopeli/Matopeli.cs +++ b/Matopeli/Matopeli/Matopeli.cs @@ -23,7 +23,7 @@ public class matopeli : PhysicsGame "# #", "# #", "# #", - "# o #", + "# m #", "# #", "# #", "# #", @@ -33,7 +33,7 @@ public class matopeli : PhysicsGame "# #", "# #", "# #", - "# #", + "#o #", "#####################", }; private Objektit Mato; @@ -41,6 +41,7 @@ public class matopeli : PhysicsGame private Objektit MatoMuu; public IntMeter pistelaskuri; + public IntMeter Ennatyslaskuri; public List<Vector> MatoSijainti = new List<Vector>(); public List<PhysicsObject> MatoKeho = new List<PhysicsObject>(); @@ -48,6 +49,8 @@ public class matopeli : PhysicsGame public int leveys; public int korkeus; public double Nopeus; + + private Vector OmenaAlkuPaikka; private static readonly Image seinakuva = LoadImage("seina.png"); private static readonly Image matopaakuva = LoadImage("matopaa.png"); @@ -61,8 +64,6 @@ public class matopeli : PhysicsGame private bool MatoLiikkunut; - private bool GameOver; - private bool OmenaKentalla; public override void Begin() { @@ -77,11 +78,13 @@ public class matopeli : PhysicsGame Asetukset(); TileMap tiles = TileMap.FromStringArray(tasonkuva); tiles['#'] = LuoSeina; - tiles['o'] = LuoMato; + tiles['m'] = LuoMato; tiles.Insert(leveys,korkeus); ; + OmenaAlkuPaikka = Mato.Position; Asetaohjaimet(); LuoAikalaskuri(); LuoPistelaskuri(); + //LuoEnnatyslaskuri(); LuoOmena(); } @@ -91,35 +94,50 @@ public class matopeli : PhysicsGame Keyboard.Listen(Key.Down, ButtonState.Pressed, VaihdaSuuntaAlas, "Turn downwards"); Keyboard.Listen(Key.Left, ButtonState.Pressed, VaihdaSuuntaVasen , "Turn left"); Keyboard.Listen(Key.Right, ButtonState.Pressed, VaihdaSuuntaOikea, "Turn right"); - Keyboard.Listen(Key.Enter, ButtonState.Pressed, Begin, "New Game"); + Keyboard.Listen(Key.Enter, ButtonState.Pressed, LiikutaMatoa, "New Game"); } void LuoPistelaskuri() { pistelaskuri = new IntMeter(0); - + Label pistenaytto = new Label(); pistenaytto.X = Screen.Left + 500; pistenaytto.Y = Screen.Top - 10; pistenaytto.TextColor = Color.Black; pistenaytto.Color = Color.White; + pistenaytto.Title = "Pisteet: "; pistenaytto.BindTo(pistelaskuri); Add(pistenaytto); } + void LuoEnnatyslaskuri() + { + Ennatyslaskuri = new IntMeter(0); + + Label Ennatys = new Label(); + Ennatys.X = Screen.Left + 700; + Ennatys.Y = Screen.Top - 10; + Ennatys.TextColor = Color.Black; + Ennatys.Color = Color.White; + Ennatys.Title = "Ennätys:"; + + Ennatys.BindTo(Ennatyslaskuri); + Add(Ennatys); + } + private void Asetukset() { leveys = 40; korkeus = 40; SuuntaYlos = false; - SuuntaAlas = true; - SuuntaOikea = false; + SuuntaAlas = false; + SuuntaOikea = true; SuuntaVasen = false; Nopeus = 0.1; - MatoLiikkunut = true; - GameOver= false; + MatoLiikkunut = false; OmenaKentalla = true; } @@ -147,39 +165,7 @@ public class matopeli : PhysicsGame /// <returns></returns> private PhysicsObject LuoOmena() { - int x = 0; - int y = 0; - - - ///Tarkistetaan ensin, ettei omena mene madon kehon kanssa päällekkäin - for (int i = 0; i < MatoKeho.Count - 1; i++) - { - x = RandomGen.NextInt(-320, 320); - y = RandomGen.NextInt(-320, 320); - - if (x - MatoKeho[i].X < 4) - { - if (y - MatoKeho[i].Y < 4) - { - x = RandomGen.NextInt(-320, 320); - y = RandomGen.NextInt(-320, 320); - } - } - } - - if (x-Mato.X<4) - { - if (y - Mato.Y < 4) - { - x = RandomGen.NextInt(-300, 300); - y = RandomGen.NextInt(-300, 300); - } - - } - - Vector paikka = new Vector(x, y); - - + Vector paikka = ArvoOmenaPaikka(); Omena = new Objektit(korkeus-10, leveys-10); Omena.Shape = Shape.Circle; Omena.Position = paikka; @@ -189,6 +175,49 @@ public class matopeli : PhysicsGame OmenaKentalla = true; return Omena; } + + private Vector ArvoOmenaPaikka() + { + Vector OmenaPaikka; + double x = RandomGen.NextInt(-8, 8); + x = OmenaAlkuPaikka.X + x * leveys; + double y = RandomGen.NextInt(-10, 5); + y = OmenaAlkuPaikka.Y + y * leveys; + + x = RandomGen.NextInt(-8, 8); + x = OmenaAlkuPaikka.X + x * leveys; + + y = RandomGen.NextInt(-10, 5); + y = OmenaAlkuPaikka.Y + y * leveys; + + for (int i = 0; i < 100; i++) + { + y = RandomGen.NextInt(-10, 5); + y = OmenaAlkuPaikka.Y + y * leveys; + + if (x-Mato.X<leveys) + { + if (y-Mato.Y<leveys) + { + if (MatoKeho.Count>0) + { + if (x-MatoMuu.X<leveys) + { + if (y-MatoMuu.Y<leveys) + { + break; + } + } + } + else break; + } + } + } + OmenaPaikka = new Vector(x, y); + return OmenaPaikka; + } + + private PhysicsObject LuoSeina() { PhysicsObject Seina = new PhysicsObject(leveys, korkeus); @@ -211,9 +240,6 @@ public class matopeli : PhysicsGame MatoMuu.Position = Paikka; Add(MatoMuu); MatoKeho.Add(MatoMuu); - - - return MatoMuu; } @@ -302,27 +328,15 @@ public class matopeli : PhysicsGame { Timer aikalaskuri = new Timer(); aikalaskuri.Interval=Nopeus; - if (GameOver == false) - { - aikalaskuri.Start(); - aikalaskuri.Timeout += PaivitaKentta; - } - + aikalaskuri.Start(); + aikalaskuri.Timeout += PaivitaKentta; } /// <summary> /// Kun ajastimesta loppuu aika, siirtyy peli tähän funktioon, jonka tarkoituksena on hahmottaa, mitä kaikkea pelissä on tapahtunut ja miten siihen tulee reagoida. - /// - /// GameOver tarkistaa, että onko mato kuollut /// </summary> private void PaivitaKentta() { - - if (GameOver == true) - { - PaataPeli(); - } - if (MatoLiikkunut == false) { return; @@ -334,21 +348,12 @@ public class matopeli : PhysicsGame LuoOmena(); } - if (GameOver == false) - { - MatoSijainti.Add(new Vector(Mato.X,Mato.Y)); - LiikutaMatoa(); - LiikutaKehoa(); - MatoSijainti.Add(new Vector(Mato.X,Mato.Y)); - } - } - /// <summary> - /// tehdään ohjelma, joka päättää pelin. - /// </summary> - private void PaataPeli() - { - + MatoSijainti.Add(new Vector(Mato.X,Mato.Y)); + LiikutaMatoa(); + LiikutaKehoa(); + MatoSijainti.Add(new Vector(Mato.X,Mato.Y)); + } private void LiikutaMatoa() -- GitLab