Skip to content
Snippets Groups Projects
Commit 6a96d441 authored by Kopperoinen Paulus Juho's avatar Kopperoinen Paulus Juho
Browse files

Sain vihdoin omena-arvonta mekanismin toimimaan kunnolla ja hoidin kaikki...

Sain vihdoin omena-arvonta mekanismin toimimaan kunnolla ja hoidin kaikki keltaiset varoitukset pois
parent 1fcf3f80
No related branches found
No related tags found
No related merge requests found
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Jypeli; using Jypeli;
using Jypeli.Assets;
using Jypeli.Controls;
using Jypeli.Widgets;
namespace matopeli; namespace matopeli;
/// @author Paulus Kopperoinen /// @author Paulus Kopperoinen
...@@ -15,9 +9,9 @@ namespace matopeli; ...@@ -15,9 +9,9 @@ namespace matopeli;
/// Perinteinen matopeli, jossa tavoitteena kasvattaa matoa mahdollisimman suureksi omenia syömällä. /// Perinteinen matopeli, jossa tavoitteena kasvattaa matoa mahdollisimman suureksi omenia syömällä.
/// </summary> /// </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 ...@@ -37,35 +31,35 @@ public class matopeli : PhysicsGame
"#o #", "#o #",
"#####################", "#####################",
}; };
private Objektit Mato; private Objektit _mato;
private Objektit Omena; private Objektit _omena;
private Objektit MatoMuu; private Objektit _matoMuu;
public IntMeter pistelaskuri; public IntMeter Pistelaskuri;
public IntMeter Ennatyslaskuri; public IntMeter Ennatyslaskuri;
public List<Vector> MatoSijainti = new List<Vector>(); public List<Vector> MatoSijainti = new List<Vector>();
public List<PhysicsObject> MatoKeho = new List<PhysicsObject>(); public List<PhysicsObject> MatoKeho = new List<PhysicsObject>();
public int leveys; public int Leveys;
public int korkeus; public int Korkeus;
public double Nopeus; public double Nopeus;
private Vector OmenaAlkuPaikka; private Vector _omenaAlkuPaikka;
private static readonly Image seinakuva = LoadImage("seina.png"); private static readonly Image Seinakuva = LoadImage("seina.png");
private static readonly Image matopaakuva = LoadImage("matopaa.png"); private static readonly Image Matopaakuva = LoadImage("matopaa.png");
private readonly Image matokehokuva = LoadImage("matokeho.png"); private readonly Image _matokehokuva = LoadImage("matokeho.png");
private readonly Image omenakuva = LoadImage("omena.png"); private readonly Image _omenakuva = LoadImage("omena.png");
private bool SuuntaYlos; private bool _suuntaYlos;
private bool SuuntaAlas; private bool _suuntaAlas;
private bool SuuntaOikea; private bool _suuntaOikea;
private bool SuuntaVasen; private bool _suuntaVasen;
private bool MatoLiikkunut; private bool _matoLiikkunut;
private bool OmenaKentalla; private bool _omenaKentalla;
public override void Begin() public override void Begin()
...@@ -83,11 +77,11 @@ public class matopeli : PhysicsGame ...@@ -83,11 +77,11 @@ public class matopeli : PhysicsGame
MatoSijainti.Clear(); MatoSijainti.Clear();
ClearAll(); ClearAll();
Asetukset(); Asetukset();
TileMap tiles = TileMap.FromStringArray(tasonkuva); TileMap tiles = TileMap.FromStringArray(Tasonkuva);
tiles['#'] = LuoSeina; tiles['#'] = LuoSeina;
tiles['m'] = LuoMato; tiles['m'] = LuoMato;
tiles.Insert(leveys,korkeus); ; tiles.Insert(Leveys,Korkeus);
OmenaAlkuPaikka = Mato.Position; _omenaAlkuPaikka = _mato.Position;
Asetaohjaimet(); Asetaohjaimet();
LuoAikalaskuri(); LuoAikalaskuri();
LuoPistelaskuri(); LuoPistelaskuri();
...@@ -113,7 +107,7 @@ public class matopeli : PhysicsGame ...@@ -113,7 +107,7 @@ public class matopeli : PhysicsGame
/// </summary> /// </summary>
void LuoPistelaskuri() void LuoPistelaskuri()
{ {
pistelaskuri = new IntMeter(0); Pistelaskuri = new IntMeter(0);
Label pistenaytto = new Label(); Label pistenaytto = new Label();
pistenaytto.X = Screen.Left + 500; pistenaytto.X = Screen.Left + 500;
...@@ -122,7 +116,7 @@ public class matopeli : PhysicsGame ...@@ -122,7 +116,7 @@ public class matopeli : PhysicsGame
pistenaytto.Color = Color.White; pistenaytto.Color = Color.White;
pistenaytto.Title = "Pisteet: "; pistenaytto.Title = "Pisteet: ";
pistenaytto.BindTo(pistelaskuri); pistenaytto.BindTo(Pistelaskuri);
Add(pistenaytto); Add(pistenaytto);
} }
...@@ -132,15 +126,15 @@ public class matopeli : PhysicsGame ...@@ -132,15 +126,15 @@ public class matopeli : PhysicsGame
/// </summary> /// </summary>
private void Asetukset() private void Asetukset()
{ {
leveys = 40; Leveys = 40;
korkeus = 40; Korkeus = 40;
SuuntaYlos = false; _suuntaYlos = false;
SuuntaAlas = false; _suuntaAlas = false;
SuuntaOikea = true; _suuntaOikea = true;
SuuntaVasen = false; _suuntaVasen = false;
Nopeus = 0.1; Nopeus = 0.1;
MatoLiikkunut = false; _matoLiikkunut = false;
OmenaKentalla = true; _omenaKentalla = true;
} }
...@@ -158,16 +152,16 @@ public class matopeli : PhysicsGame ...@@ -158,16 +152,16 @@ public class matopeli : PhysicsGame
/// <returns></returns> /// <returns></returns>
private PhysicsObject LuoMato() private PhysicsObject LuoMato()
{ {
Mato = new Objektit(leveys-5, korkeus-5); _mato = new Objektit(Leveys-5, Korkeus-5);
Mato.Image = matopaakuva; _mato.Image = Matopaakuva;
Add(Mato); Add(_mato);
Mato.Tag = "mato"; _mato.Tag = "mato";
AddCollisionHandler(Mato, "omena", delegate(IPhysicsObject Mato, IPhysicsObject Omena) {MatoSyoOmenan(); }); AddCollisionHandler(_mato, "omena", delegate(IPhysicsObject _, IPhysicsObject _) {MatoSyoOmenan(); });
AddCollisionHandler(Mato, "seina", delegate(IPhysicsObject Mato, IPhysicsObject seina) {MatoKuolee(); }); AddCollisionHandler(_mato, "seina", delegate(IPhysicsObject _, IPhysicsObject _) {MatoKuolee(); });
AddCollisionHandler(Mato, "matokeho", delegate(IPhysicsObject Mato, IPhysicsObject matokeho) {MatoKuolee(); }); AddCollisionHandler(_mato, "matokeho", delegate(IPhysicsObject _, IPhysicsObject _) {MatoKuolee(); });
return Mato; return _mato;
} }
...@@ -175,17 +169,16 @@ public class matopeli : PhysicsGame ...@@ -175,17 +169,16 @@ public class matopeli : PhysicsGame
/// Luodaan omena johonkin satunnaiseen kohtaan kartalla, joka ei ole samassa kohtaa madon kanssa. /// Luodaan omena johonkin satunnaiseen kohtaan kartalla, joka ei ole samassa kohtaa madon kanssa.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private PhysicsObject LuoOmena() private void LuoOmena()
{ {
Vector paikka = ArvoOmenaPaikka(); Vector paikka = ArvoOmenaPaikka();
Omena = new Objektit(korkeus-10, leveys-10); _omena = new Objektit(Korkeus-10, Leveys-10);
Omena.Shape = Shape.Circle; _omena.Shape = Shape.Circle;
Omena.Position = paikka; _omena.Position = paikka;
Omena.Image = omenakuva; _omena.Image = _omenakuva;
Omena.Tag = "omena"; _omena.Tag = "omena";
Add(Omena); Add(_omena);
OmenaKentalla = true; _omenaKentalla = true;
return Omena;
} }
...@@ -195,43 +188,27 @@ public class matopeli : PhysicsGame ...@@ -195,43 +188,27 @@ public class matopeli : PhysicsGame
/// <returns></returns> /// <returns></returns>
private Vector ArvoOmenaPaikka() private Vector ArvoOmenaPaikka()
{ {
Vector OmenaPaikka; Vector omenaPaikka;
double x = RandomGen.NextInt(-8, 8); double x;
x = OmenaAlkuPaikka.X + x * leveys; double y;
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 = 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 = RandomGen.NextInt(-10, 5);
y = OmenaAlkuPaikka.Y + y * leveys; y = _omenaAlkuPaikka.Y + y * Leveys;
if (x-Mato.X<leveys) x = RandomGen.NextInt(-8, 8);
{ x = _omenaAlkuPaikka.X + 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; omenaPaikka = new Vector(x, y);
return omenaPaikka;
} }
...@@ -241,12 +218,12 @@ public class matopeli : PhysicsGame ...@@ -241,12 +218,12 @@ public class matopeli : PhysicsGame
/// <returns></returns> /// <returns></returns>
private PhysicsObject LuoSeina() private PhysicsObject LuoSeina()
{ {
PhysicsObject Seina = new PhysicsObject(leveys, korkeus); PhysicsObject seina = new PhysicsObject(Leveys, Korkeus);
Seina.MakeStatic(); seina.MakeStatic();
Seina.Color = Color.Green; seina.Color = Color.Green;
Seina.Image = seinakuva; seina.Image = Seinakuva;
Seina.Tag = "seina"; seina.Tag = "seina";
return Seina; return seina;
} }
...@@ -255,14 +232,25 @@ public class matopeli : PhysicsGame ...@@ -255,14 +232,25 @@ public class matopeli : PhysicsGame
/// </summary> /// </summary>
public PhysicsObject LuoMatoKeho() public PhysicsObject LuoMatoKeho()
{ {
MatoMuu = new Objektit(leveys-6, korkeus-6); _matoMuu = new Objektit(Leveys-6, Korkeus-6);
MatoMuu.Tag = "matokeho"; _matoMuu.Tag = "matokeho";
MatoMuu.Image = matokehokuva; _matoMuu.Image = _matokehokuva;
Vector Paikka = MatoSijainti[MatoSijainti.Count -1 - MatoKeho.Count]; Vector paikka = MatoSijainti[MatoSijainti.Count -1 - MatoKeho.Count];
MatoMuu.Position = Paikka; _matoMuu.Position = paikka;
Add(MatoMuu); Add(_matoMuu);
MatoKeho.Add(MatoMuu); MatoKeho.Add(_matoMuu);
return 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 ...@@ -271,9 +259,9 @@ public class matopeli : PhysicsGame
/// </summary> /// </summary>
private void MatoSyoOmenan() private void MatoSyoOmenan()
{ {
Omena.Destroy(); _omena.Destroy();
pistelaskuri.Value += 1; Pistelaskuri.Value += 1;
OmenaKentalla = false; _omenaKentalla = false;
} }
...@@ -283,19 +271,19 @@ public class matopeli : PhysicsGame ...@@ -283,19 +271,19 @@ public class matopeli : PhysicsGame
private void VaihdaSuuntaYlos() private void VaihdaSuuntaYlos()
{ {
if (SuuntaAlas==true) if (_suuntaAlas)
{ {
SuuntaOikea = false; _suuntaOikea = false;
SuuntaVasen = false; _suuntaVasen = false;
SuuntaYlos = false; _suuntaYlos = false;
SuuntaAlas = true; _suuntaAlas = true;
} }
else else
{ {
SuuntaOikea = false; _suuntaOikea = false;
SuuntaVasen = false; _suuntaVasen = false;
SuuntaYlos = true; _suuntaYlos = true;
SuuntaAlas = false; _suuntaAlas = false;
} }
} }
...@@ -305,19 +293,19 @@ public class matopeli : PhysicsGame ...@@ -305,19 +293,19 @@ public class matopeli : PhysicsGame
/// </summary> /// </summary>
private void VaihdaSuuntaAlas() private void VaihdaSuuntaAlas()
{ {
if (SuuntaYlos==true) if (_suuntaYlos)
{ {
SuuntaOikea = false; _suuntaOikea = false;
SuuntaVasen = false; _suuntaVasen = false;
SuuntaYlos = true; _suuntaYlos = true;
SuuntaAlas = false; _suuntaAlas = false;
} }
else else
{ {
SuuntaOikea = false; _suuntaOikea = false;
SuuntaVasen = false; _suuntaVasen = false;
SuuntaYlos = false; _suuntaYlos = false;
SuuntaAlas = true; _suuntaAlas = true;
} }
} }
...@@ -327,20 +315,20 @@ public class matopeli : PhysicsGame ...@@ -327,20 +315,20 @@ public class matopeli : PhysicsGame
/// </summary> /// </summary>
private void VaihdaSuuntaOikea() private void VaihdaSuuntaOikea()
{ {
if (SuuntaVasen == true) if (_suuntaVasen)
{ {
SuuntaOikea = false; _suuntaOikea = false;
SuuntaVasen = true; _suuntaVasen = true;
SuuntaYlos = false; _suuntaYlos = false;
SuuntaAlas = false; _suuntaAlas = false;
} }
else else
{ {
SuuntaOikea = true; _suuntaOikea = true;
SuuntaVasen = false; _suuntaVasen = false;
SuuntaYlos = false; _suuntaYlos = false;
SuuntaAlas = false; _suuntaAlas = false;
} }
} }
...@@ -351,19 +339,19 @@ public class matopeli : PhysicsGame ...@@ -351,19 +339,19 @@ public class matopeli : PhysicsGame
/// </summary> /// </summary>
private void VaihdaSuuntaVasen() private void VaihdaSuuntaVasen()
{ {
if (SuuntaOikea==true) if (_suuntaOikea)
{ {
SuuntaOikea = true; _suuntaOikea = true;
SuuntaVasen = false; _suuntaVasen = false;
SuuntaYlos = false; _suuntaYlos = false;
SuuntaAlas = false; _suuntaAlas = false;
} }
else else
{ {
SuuntaOikea = false; _suuntaOikea = false;
SuuntaVasen = true; _suuntaVasen = true;
SuuntaYlos = false; _suuntaYlos = false;
SuuntaAlas = false; _suuntaAlas = false;
} }
} }
...@@ -385,21 +373,21 @@ public class matopeli : PhysicsGame ...@@ -385,21 +373,21 @@ public class matopeli : PhysicsGame
/// </summary> /// </summary>
private void PaivitaKentta() private void PaivitaKentta()
{ {
if (MatoLiikkunut == false) if (_matoLiikkunut == false)
{ {
return; return;
} }
if (OmenaKentalla == false) if (_omenaKentalla == false)
{ {
LuoMatoKeho(); LuoMatoKeho();
LuoOmena(); LuoOmena();
} }
MatoSijainti.Add(new Vector(Mato.X,Mato.Y)); MatoSijainti.Add(new Vector(_mato.X,_mato.Y));
LiikutaMatoa(); LiikutaMatoa();
LiikutaKehoa(); 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 ...@@ -408,31 +396,31 @@ public class matopeli : PhysicsGame
/// </summary> /// </summary>
private void LiikutaMatoa() private void LiikutaMatoa()
{ {
MatoLiikkunut = false; _matoLiikkunut = false;
if (SuuntaYlos==true) if (_suuntaYlos)
{ {
Mato.Y = Mato.Y + leveys; _mato.Y = _mato.Y + Leveys;
Mato.Angle = Angle.FromDegrees(90); _mato.Angle = Angle.FromDegrees(90);
} }
if (SuuntaAlas==true) if (_suuntaAlas)
{ {
Mato.Y= Mato.Y - leveys; _mato.Y= _mato.Y - Leveys;
Mato.Angle = Angle.FromDegrees(270); _mato.Angle = Angle.FromDegrees(270);
} }
if (SuuntaOikea==true) if (_suuntaOikea)
{ {
Mato.X= Mato.X + leveys; _mato.X= _mato.X + Leveys;
Mato.Angle = Angle.FromDegrees(0); _mato.Angle = Angle.FromDegrees(0);
} }
if (SuuntaVasen==true) if (_suuntaVasen)
{ {
Mato.X= Mato.X - leveys; _mato.X= _mato.X - Leveys;
Mato.Angle = Angle.FromDegrees(180); _mato.Angle = Angle.FromDegrees(180);
} }
MatoLiikkunut = true; _matoLiikkunut = true;
} }
...@@ -445,7 +433,7 @@ public class matopeli : PhysicsGame ...@@ -445,7 +433,7 @@ public class matopeli : PhysicsGame
{ {
return; return;
} }
MatoLiikkunut = false; _matoLiikkunut = false;
MatoKeho[0].X = MatoSijainti[MatoSijainti.Count - 1].X; MatoKeho[0].X = MatoSijainti[MatoSijainti.Count - 1].X;
MatoKeho[0].Y = MatoSijainti[MatoSijainti.Count - 1].Y; MatoKeho[0].Y = MatoSijainti[MatoSijainti.Count - 1].Y;
...@@ -455,7 +443,7 @@ public class matopeli : PhysicsGame ...@@ -455,7 +443,7 @@ public class matopeli : PhysicsGame
MatoKeho[i].Y = MatoSijainti[MatoSijainti.Count - 2*i].Y; MatoKeho[i].Y = MatoSijainti[MatoSijainti.Count - 2*i].Y;
} }
MatoLiikkunut = true; _matoLiikkunut = true;
} }
......
...@@ -19,7 +19,7 @@ namespace Matopeli ...@@ -19,7 +19,7 @@ namespace Matopeli
[STAThread] [STAThread]
static void Main() static void Main()
{ {
using (var game = new matopeli.matopeli()) using (var game = new matopeli.Matopeli())
{ {
game.Run(); game.Run();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment