From deb3caf6e608ec2e11ee0e6d82545b56ed75ffeb Mon Sep 17 00:00:00 2001
From: Kopperoinen Paulus <paulus.j.kopperoinen@student.jyu.fi>
Date: Wed, 1 Nov 2023 20:43:39 +0200
Subject: [PATCH] =?UTF-8?q?Lis=C3=A4sin=20peliin=20omenan,=20joka=20syntyy?=
 =?UTF-8?q?=20uudelleen=20kentt=C3=A4=C3=A4n,=20kun=20se=20sy=C3=B6d=C3=A4?=
 =?UTF-8?q?=C3=A4n.=20P=C3=A4=C3=A4tin=20lis=C3=A4t=C3=A4=20fyysiset=20sei?=
 =?UTF-8?q?n=C3=A4t=20takaisin,=20koska=20en=20oikein=20osannut=20tehd?=
 =?UTF-8?q?=C3=A4=20kent=C3=A4lle=20muuten=20rajoja.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Matopeli/Matopeli/Matopeli.cs | 99 ++++++++++++++++++++++++++++++++---
 Matopeli/Matopeli/Ohjelma.cs  |  6 ++-
 2 files changed, 96 insertions(+), 9 deletions(-)

diff --git a/Matopeli/Matopeli/Matopeli.cs b/Matopeli/Matopeli/Matopeli.cs
index b958796..75ee4a7 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 2a2c9de..8f6b899 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
-- 
GitLab