diff --git a/Matopeli/Matopeli/Matopeli.cs b/Matopeli/Matopeli/Matopeli.cs index b958796ba9477c8c4cee35882dad5e77225dc8b1..75ee4a7594a0a3ed46f122122fe005feb7849d1a 100644 --- a/Matopeli/Matopeli/Matopeli.cs +++ b/Matopeli/Matopeli/Matopeli.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Security.Cryptography.X509Certificates; using Jypeli; @@ -14,11 +15,36 @@ namespace matopeli; /// </summary> public class matopeli : PhysicsGame { + private static readonly string[] tasonkuva = { + "#################", + "# #", + "# #", + "# #", + "# #", + "# #", + "# #", + "# #", + "# #", + "# #", + "# #", + "# #", + "# #", + "#################", + }; private Objektit Mato; + private Objektit Omena; + public double MatoX; public double MatoY; + + public int LuoOmenaX; + public int LuoOmenaY; + public int SuuntaX; public int SuuntaY; + + public int leveys = 60; + public int korkeus = 60; private static readonly Image seinakuva = LoadImage("seina.png"); private static readonly Image matopaakuva = LoadImage("matopaa.png"); @@ -30,7 +56,8 @@ public class matopeli : PhysicsGame private bool SuuntaAlas = false; private bool SuuntaOikea = false; private bool SuuntaVasen = false; - + + private bool OmenaKentalla = false; public override void Begin() { // Kirjoita ohjelmakoodisi tähän @@ -43,11 +70,16 @@ public class matopeli : PhysicsGame public void AloitaUusiPeli() { + Level.CreateBorders(0.0,true,Color.Green); ClearAll(); SuuntaAlas = true; LuoMato(); Asetaohjaimet(); LuoAikalaskuri(); + + TileMap tiles = TileMap.FromStringArray(tasonkuva); + tiles['#'] = LuoSeina; + tiles.Insert(leveys,korkeus); } private void Asetaohjaimet() { @@ -56,16 +88,51 @@ public class matopeli : PhysicsGame Keyboard.Listen(Key.Left, ButtonState.Pressed, VaihdaSuuntaVasen , "Turn left"); Keyboard.Listen(Key.Right, ButtonState.Pressed, VaihdaSuuntaOikea, "Turn right"); } - public PhysicsObject LuoMato() + private PhysicsObject LuoMato() { - Mato = new Objektit(60, 60, new Vector(0,0)); - Mato.Color = Color.Charcoal; - Mato.Position = new Vector(0, 0); + Mato = new Objektit(leveys, korkeus, new Vector(0,0)); + Mato.Image = matopaakuva; Add(Mato); Mato.Tag = "mato"; + AddCollisionHandler(Mato, "omena", delegate(IPhysicsObject Mato, IPhysicsObject Omena) {MatoSyoOmenan(); }); return Mato; } + private PhysicsObject LuoOmena() + { + Vector paikka = Level.GetRandomPosition(); + if (paikka.X-Mato.X<20) + { + if (paikka.Y-Mato.Y<20) + { + paikka = Level.GetRandomPosition(); + } + } + + Omena = new Objektit(korkeus, leveys, paikka); + Omena.Shape = Shape.Circle; + Omena.Position = paikka; + Omena.Image = omenakuva; + Omena.Tag = "omena"; + Add(Omena); + OmenaKentalla = true; + return Omena; + } + private PhysicsObject LuoSeina() + { + PhysicsObject Seina = PhysicsObject.CreateStaticObject(leveys, korkeus); + Seina.Color = Color.Green; + Seina.Image = seinakuva; + Seina.Tag = "seina"; + return Seina; + } + + private void MatoSyoOmenan() + { + Omena.Destroy(); + OmenaKentalla = false; + } + private void VaihdaSuuntaYlos() { @@ -143,28 +210,46 @@ public class matopeli : PhysicsGame private void LuoAikalaskuri() { Timer aikalaskuri = new Timer(); - aikalaskuri.Interval = 0.008; - aikalaskuri.Timeout += LiikutaMatoa; + aikalaskuri.Interval = 0.01; + aikalaskuri.Timeout += PaivitaKentta; aikalaskuri.Start(); } + private void PaivitaKentta() + { + if (Math.Abs(Mato.X)-Math.Abs(Mato.X)<0.001 && Math.Abs(Mato.Y)-Math.Abs(Mato.Y)<0.001) + { + LiikutaMatoa(); + } + + if (OmenaKentalla == false) + { + LuoOmena(); + //KasvataMatoa(); + } + } + private void LiikutaMatoa() { if (SuuntaYlos==true) { Mato.Y++; + Mato.Angle = Angle.FromDegrees(90); } if (SuuntaAlas==true) { Mato.Y--; + Mato.Angle = Angle.FromDegrees(270); } if (SuuntaOikea==true) { Mato.X++; + Mato.Angle = Angle.FromDegrees(0); } if (SuuntaVasen==true) { Mato.X--; + Mato.Angle = Angle.FromDegrees(180); } diff --git a/Matopeli/Matopeli/Ohjelma.cs b/Matopeli/Matopeli/Ohjelma.cs index 2a2c9de28152a9a4c841c39d30360b04fc5ffff7..8f6b89994f7a2b8e21af7b44d834934dffd5b34b 100644 --- a/Matopeli/Matopeli/Ohjelma.cs +++ b/Matopeli/Matopeli/Ohjelma.cs @@ -19,8 +19,10 @@ namespace Matopeli [STAThread] static void Main() { - using var game = new Matopeli(); - game.Run(); + using (var game = new matopeli.matopeli()) + { + game.Run(); + } } } } \ No newline at end of file