From 6a96d44161f21822624c19d383d8f405ae957f40 Mon Sep 17 00:00:00 2001 From: Kopperoinen Paulus Juho <paulus.j.kopperoinen@student.jyu.fi> Date: Thu, 23 Nov 2023 12:28:25 +0200 Subject: [PATCH] Sain vihdoin omena-arvonta mekanismin toimimaan kunnolla ja hoidin kaikki keltaiset varoitukset pois --- Matopeli/Matopeli/Matopeli.cs | 308 ++++++++++++++++------------------ Matopeli/Matopeli/Ohjelma.cs | 2 +- 2 files changed, 149 insertions(+), 161 deletions(-) diff --git a/Matopeli/Matopeli/Matopeli.cs b/Matopeli/Matopeli/Matopeli.cs index a6b886b..288c5be 100644 --- a/Matopeli/Matopeli/Matopeli.cs +++ b/Matopeli/Matopeli/Matopeli.cs @@ -1,12 +1,6 @@ -using System; + using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography.X509Certificates; using Jypeli; -using Jypeli.Assets; -using Jypeli.Controls; -using Jypeli.Widgets; - namespace matopeli; /// @author Paulus Kopperoinen @@ -15,9 +9,9 @@ namespace matopeli; /// Perinteinen matopeli, jossa tavoitteena kasvattaa matoa mahdollisimman suureksi omenia syömällä. /// </summary> -public class matopeli : PhysicsGame +public class Matopeli : PhysicsGame { - private static readonly string[] tasonkuva = { + private static readonly string[] Tasonkuva = { "#####################", "# #", "# #", @@ -37,35 +31,35 @@ public class matopeli : PhysicsGame "#o #", "#####################", }; - private Objektit Mato; - private Objektit Omena; - private Objektit MatoMuu; + private Objektit _mato; + private Objektit _omena; + private Objektit _matoMuu; - public IntMeter pistelaskuri; + public IntMeter Pistelaskuri; public IntMeter Ennatyslaskuri; public List<Vector> MatoSijainti = new List<Vector>(); public List<PhysicsObject> MatoKeho = new List<PhysicsObject>(); - public int leveys; - public int korkeus; + public int Leveys; + public int Korkeus; public double Nopeus; - private Vector OmenaAlkuPaikka; + private Vector _omenaAlkuPaikka; - private static readonly Image seinakuva = LoadImage("seina.png"); - private static readonly Image matopaakuva = LoadImage("matopaa.png"); - private readonly Image matokehokuva = LoadImage("matokeho.png"); - private readonly Image omenakuva = LoadImage("omena.png"); + private static readonly Image Seinakuva = LoadImage("seina.png"); + private static readonly Image Matopaakuva = LoadImage("matopaa.png"); + private readonly Image _matokehokuva = LoadImage("matokeho.png"); + private readonly Image _omenakuva = LoadImage("omena.png"); - private bool SuuntaYlos; - private bool SuuntaAlas; - private bool SuuntaOikea; - private bool SuuntaVasen; + private bool _suuntaYlos; + private bool _suuntaAlas; + private bool _suuntaOikea; + private bool _suuntaVasen; - private bool MatoLiikkunut; + private bool _matoLiikkunut; - private bool OmenaKentalla; + private bool _omenaKentalla; public override void Begin() @@ -83,11 +77,11 @@ public class matopeli : PhysicsGame MatoSijainti.Clear(); ClearAll(); Asetukset(); - TileMap tiles = TileMap.FromStringArray(tasonkuva); + TileMap tiles = TileMap.FromStringArray(Tasonkuva); tiles['#'] = LuoSeina; tiles['m'] = LuoMato; - tiles.Insert(leveys,korkeus); ; - OmenaAlkuPaikka = Mato.Position; + tiles.Insert(Leveys,Korkeus); + _omenaAlkuPaikka = _mato.Position; Asetaohjaimet(); LuoAikalaskuri(); LuoPistelaskuri(); @@ -113,7 +107,7 @@ public class matopeli : PhysicsGame /// </summary> void LuoPistelaskuri() { - pistelaskuri = new IntMeter(0); + Pistelaskuri = new IntMeter(0); Label pistenaytto = new Label(); pistenaytto.X = Screen.Left + 500; @@ -122,7 +116,7 @@ public class matopeli : PhysicsGame pistenaytto.Color = Color.White; pistenaytto.Title = "Pisteet: "; - pistenaytto.BindTo(pistelaskuri); + pistenaytto.BindTo(Pistelaskuri); Add(pistenaytto); } @@ -132,15 +126,15 @@ public class matopeli : PhysicsGame /// </summary> private void Asetukset() { - leveys = 40; - korkeus = 40; - SuuntaYlos = false; - SuuntaAlas = false; - SuuntaOikea = true; - SuuntaVasen = false; + Leveys = 40; + Korkeus = 40; + _suuntaYlos = false; + _suuntaAlas = false; + _suuntaOikea = true; + _suuntaVasen = false; Nopeus = 0.1; - MatoLiikkunut = false; - OmenaKentalla = true; + _matoLiikkunut = false; + _omenaKentalla = true; } @@ -158,16 +152,16 @@ public class matopeli : PhysicsGame /// <returns></returns> private PhysicsObject LuoMato() { - Mato = new Objektit(leveys-5, korkeus-5); - Mato.Image = matopaakuva; - Add(Mato); - Mato.Tag = "mato"; + _mato = new Objektit(Leveys-5, Korkeus-5); + _mato.Image = Matopaakuva; + Add(_mato); + _mato.Tag = "mato"; - AddCollisionHandler(Mato, "omena", delegate(IPhysicsObject Mato, IPhysicsObject Omena) {MatoSyoOmenan(); }); - AddCollisionHandler(Mato, "seina", delegate(IPhysicsObject Mato, IPhysicsObject seina) {MatoKuolee(); }); - AddCollisionHandler(Mato, "matokeho", delegate(IPhysicsObject Mato, IPhysicsObject matokeho) {MatoKuolee(); }); + AddCollisionHandler(_mato, "omena", delegate(IPhysicsObject _, IPhysicsObject _) {MatoSyoOmenan(); }); + AddCollisionHandler(_mato, "seina", delegate(IPhysicsObject _, IPhysicsObject _) {MatoKuolee(); }); + AddCollisionHandler(_mato, "matokeho", delegate(IPhysicsObject _, IPhysicsObject _) {MatoKuolee(); }); - return Mato; + return _mato; } @@ -175,17 +169,16 @@ public class matopeli : PhysicsGame /// Luodaan omena johonkin satunnaiseen kohtaan kartalla, joka ei ole samassa kohtaa madon kanssa. /// </summary> /// <returns></returns> - private PhysicsObject LuoOmena() + private void LuoOmena() { Vector paikka = ArvoOmenaPaikka(); - Omena = new Objektit(korkeus-10, leveys-10); - Omena.Shape = Shape.Circle; - Omena.Position = paikka; - Omena.Image = omenakuva; - Omena.Tag = "omena"; - Add(Omena); - OmenaKentalla = true; - return Omena; + _omena = new Objektit(Korkeus-10, Leveys-10); + _omena.Shape = Shape.Circle; + _omena.Position = paikka; + _omena.Image = _omenakuva; + _omena.Tag = "omena"; + Add(_omena); + _omenaKentalla = true; } @@ -195,43 +188,27 @@ public class matopeli : PhysicsGame /// <returns></returns> 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; - + Vector omenaPaikka; + double x; + double y; + y = RandomGen.NextInt(-10, 5); - y = OmenaAlkuPaikka.Y + y * leveys; + y = _omenaAlkuPaikka.Y + y * Leveys; + + x = RandomGen.NextInt(-8, 8); + x = _omenaAlkuPaikka.X + x * Leveys; - for (int i = 0; i < 100; i++) + if (x - _mato.X < Leveys && y - _mato.Y < Leveys) { y = RandomGen.NextInt(-10, 5); - y = OmenaAlkuPaikka.Y + y * leveys; + 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; - } - } + x = RandomGen.NextInt(-8, 8); + x = _omenaAlkuPaikka.X + x * Leveys; } - OmenaPaikka = new Vector(x, y); - return OmenaPaikka; + + omenaPaikka = new Vector(x, y); + return omenaPaikka; } @@ -241,12 +218,12 @@ public class matopeli : PhysicsGame /// <returns></returns> private PhysicsObject LuoSeina() { - PhysicsObject Seina = new PhysicsObject(leveys, korkeus); - Seina.MakeStatic(); - Seina.Color = Color.Green; - Seina.Image = seinakuva; - Seina.Tag = "seina"; - return Seina; + PhysicsObject seina = new PhysicsObject(Leveys, Korkeus); + seina.MakeStatic(); + seina.Color = Color.Green; + seina.Image = Seinakuva; + seina.Tag = "seina"; + return seina; } @@ -255,14 +232,25 @@ public class matopeli : PhysicsGame /// </summary> public PhysicsObject LuoMatoKeho() { - MatoMuu = new Objektit(leveys-6, korkeus-6); - MatoMuu.Tag = "matokeho"; - MatoMuu.Image = matokehokuva; - Vector Paikka = MatoSijainti[MatoSijainti.Count -1 - MatoKeho.Count]; - MatoMuu.Position = Paikka; - Add(MatoMuu); - MatoKeho.Add(MatoMuu); - return MatoMuu; + _matoMuu = new Objektit(Leveys-6, Korkeus-6); + _matoMuu.Tag = "matokeho"; + _matoMuu.Image = _matokehokuva; + Vector paikka = MatoSijainti[MatoSijainti.Count -1 - MatoKeho.Count]; + _matoMuu.Position = paikka; + Add(_matoMuu); + MatoKeho.Add(_matoMuu); + AddCollisionHandler(_matoMuu, "omena", delegate(IPhysicsObject _, IPhysicsObject _) {LuoUusiOmena(); }); + return _matoMuu; + } + + + /// <summary> + /// Jos omena luodaan madon kehon sisään, tämä aliohjelma luo uuden omenan vanhan tilalle + /// </summary> + private void LuoUusiOmena() + { + _omena.Destroy(); + _omenaKentalla = false; } @@ -271,9 +259,9 @@ public class matopeli : PhysicsGame /// </summary> private void MatoSyoOmenan() { - Omena.Destroy(); - pistelaskuri.Value += 1; - OmenaKentalla = false; + _omena.Destroy(); + Pistelaskuri.Value += 1; + _omenaKentalla = false; } @@ -283,19 +271,19 @@ public class matopeli : PhysicsGame private void VaihdaSuuntaYlos() { - if (SuuntaAlas==true) + if (_suuntaAlas) { - SuuntaOikea = false; - SuuntaVasen = false; - SuuntaYlos = false; - SuuntaAlas = true; + _suuntaOikea = false; + _suuntaVasen = false; + _suuntaYlos = false; + _suuntaAlas = true; } else { - SuuntaOikea = false; - SuuntaVasen = false; - SuuntaYlos = true; - SuuntaAlas = false; + _suuntaOikea = false; + _suuntaVasen = false; + _suuntaYlos = true; + _suuntaAlas = false; } } @@ -305,19 +293,19 @@ public class matopeli : PhysicsGame /// </summary> private void VaihdaSuuntaAlas() { - if (SuuntaYlos==true) + if (_suuntaYlos) { - SuuntaOikea = false; - SuuntaVasen = false; - SuuntaYlos = true; - SuuntaAlas = false; + _suuntaOikea = false; + _suuntaVasen = false; + _suuntaYlos = true; + _suuntaAlas = false; } else { - SuuntaOikea = false; - SuuntaVasen = false; - SuuntaYlos = false; - SuuntaAlas = true; + _suuntaOikea = false; + _suuntaVasen = false; + _suuntaYlos = false; + _suuntaAlas = true; } } @@ -327,20 +315,20 @@ public class matopeli : PhysicsGame /// </summary> private void VaihdaSuuntaOikea() { - if (SuuntaVasen == true) + if (_suuntaVasen) { - SuuntaOikea = false; - SuuntaVasen = true; - SuuntaYlos = false; - SuuntaAlas = false; + _suuntaOikea = false; + _suuntaVasen = true; + _suuntaYlos = false; + _suuntaAlas = false; } else { - SuuntaOikea = true; - SuuntaVasen = false; - SuuntaYlos = false; - SuuntaAlas = false; + _suuntaOikea = true; + _suuntaVasen = false; + _suuntaYlos = false; + _suuntaAlas = false; } } @@ -351,19 +339,19 @@ public class matopeli : PhysicsGame /// </summary> private void VaihdaSuuntaVasen() { - if (SuuntaOikea==true) + if (_suuntaOikea) { - SuuntaOikea = true; - SuuntaVasen = false; - SuuntaYlos = false; - SuuntaAlas = false; + _suuntaOikea = true; + _suuntaVasen = false; + _suuntaYlos = false; + _suuntaAlas = false; } else { - SuuntaOikea = false; - SuuntaVasen = true; - SuuntaYlos = false; - SuuntaAlas = false; + _suuntaOikea = false; + _suuntaVasen = true; + _suuntaYlos = false; + _suuntaAlas = false; } } @@ -385,21 +373,21 @@ public class matopeli : PhysicsGame /// </summary> private void PaivitaKentta() { - if (MatoLiikkunut == false) + if (_matoLiikkunut == false) { return; } - if (OmenaKentalla == false) + if (_omenaKentalla == false) { LuoMatoKeho(); LuoOmena(); } - MatoSijainti.Add(new Vector(Mato.X,Mato.Y)); + MatoSijainti.Add(new Vector(_mato.X,_mato.Y)); LiikutaMatoa(); LiikutaKehoa(); - MatoSijainti.Add(new Vector(Mato.X,Mato.Y)); + MatoSijainti.Add(new Vector(_mato.X,_mato.Y)); } @@ -408,31 +396,31 @@ public class matopeli : PhysicsGame /// </summary> private void LiikutaMatoa() { - MatoLiikkunut = false; - if (SuuntaYlos==true) + _matoLiikkunut = false; + if (_suuntaYlos) { - Mato.Y = Mato.Y + leveys; - Mato.Angle = Angle.FromDegrees(90); + _mato.Y = _mato.Y + Leveys; + _mato.Angle = Angle.FromDegrees(90); } - if (SuuntaAlas==true) + if (_suuntaAlas) { - Mato.Y= Mato.Y - leveys; - Mato.Angle = Angle.FromDegrees(270); + _mato.Y= _mato.Y - Leveys; + _mato.Angle = Angle.FromDegrees(270); } - if (SuuntaOikea==true) + if (_suuntaOikea) { - Mato.X= Mato.X + leveys; - Mato.Angle = Angle.FromDegrees(0); + _mato.X= _mato.X + Leveys; + _mato.Angle = Angle.FromDegrees(0); } - if (SuuntaVasen==true) + if (_suuntaVasen) { - Mato.X= Mato.X - leveys; - Mato.Angle = Angle.FromDegrees(180); + _mato.X= _mato.X - Leveys; + _mato.Angle = Angle.FromDegrees(180); } - MatoLiikkunut = true; + _matoLiikkunut = true; } @@ -445,7 +433,7 @@ public class matopeli : PhysicsGame { return; } - MatoLiikkunut = false; + _matoLiikkunut = false; MatoKeho[0].X = MatoSijainti[MatoSijainti.Count - 1].X; MatoKeho[0].Y = MatoSijainti[MatoSijainti.Count - 1].Y; @@ -455,7 +443,7 @@ public class matopeli : PhysicsGame MatoKeho[i].Y = MatoSijainti[MatoSijainti.Count - 2*i].Y; } - MatoLiikkunut = true; + _matoLiikkunut = true; } diff --git a/Matopeli/Matopeli/Ohjelma.cs b/Matopeli/Matopeli/Ohjelma.cs index 8f6b899..7c031c6 100644 --- a/Matopeli/Matopeli/Ohjelma.cs +++ b/Matopeli/Matopeli/Ohjelma.cs @@ -19,7 +19,7 @@ namespace Matopeli [STAThread] static void Main() { - using (var game = new matopeli.matopeli()) + using (var game = new matopeli.Matopeli()) { game.Run(); } -- GitLab