Ohjelmointi 1/2014, luento16 esimerkkeja

HangryDog/HangryDog/HangryDog.cs

1 using System;
2 using System.Collections.Generic;
3 using Jypeli;
4 using Jypeli.Assets;
5 using Jypeli.Controls;
6 using Jypeli.Effects;
7 using Jypeli.Widgets;
8 
9 /// @author Vesa Lappalainen, kuvat ja äänet: Panu Lappalainen
10 /// @version 24.10.2014
11 /// <summary>
12 /// Esimerkki luokista ja perinnästä
13 /// </summary>
14 public class HangryDog : PhysicsGame
15 {
16  public override void Begin()
17  {
18  Koira koira = new Koira(this,200, Vector.Zero,"Musti");
19  LuoOhjaimet(koira, 100, Key.Up, Key.Down, Key.Left, Key.Right);
20 
21  Kissa kissa = new Kissa(this,200, new Vector(100,200),"Mirri");
22  LuoOhjaimet(kissa, 100, Key.W, Key.Z, Key.A, Key.S);
23 
24  Timer timer = new Timer();
25  timer.Interval = 0.500;
26  timer.Timeout +=LuoRuokaa;
27  timer.Start();
28 
29  Level.CreateBorders();
30 
31 
32  PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
33  Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
34  Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Avustus");
35 
36  Camera.ZoomToLevel();
37  LoadImage("");
38  }
39 
40 
41  void LuoOhjaimet(Elain elain, double voima, Key up, Key down, Key left, Key right)
42  {
43  Keyboard.Listen(up, ButtonState.Down, elain.Liiku, elain.Nimi + " up", new Vector(0, voima));
44  Keyboard.Listen(down, ButtonState.Down, elain.Liiku, elain.Nimi + " down", new Vector(0, -voima));
45  Keyboard.Listen(left, ButtonState.Down, elain.Liiku, elain.Nimi + " left", new Vector(-voima, 0));
46  Keyboard.Listen(right, ButtonState.Down, elain.Liiku, elain.Nimi + " right", new Vector(voima, 0));
47  }
48 
49 
50  void LuoRuokaa()
51  {
52  // var luu = new Luu(this, 50, 20, RandomGen.NextVector(Level.BoundingRect));
53  var juusto = new Juusto(this, 50, RandomGen.NextVector(Level.BoundingRect));
54  var hiiri = new Hiiri(this, 70, RandomGen.NextVector(Level.BoundingRect),juusto);
55  }
56 }
[Doc-muoto] [Alkuperäinen .cs]

HangryDog/HangryDog/Luokat.cs

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Jypeli;
6 using Jypeli.Assets;
7 using Jypeli.Controls;
8 using Jypeli.Effects;
9 using Jypeli.Widgets;
10 
11 public class Efektit
12 {
13  public static void Savuta(PhysicsGame peli, IPhysicsObject olio, bool useShock = false, double kerroin = 10)
14  {
15 #if !MonoGame
16  Smoke savu = new Smoke();
17  savu.MaximumLifetime = TimeSpan.FromSeconds(3);
18  savu.Position = olio.Position;
19  peli.Add(savu); olio.Destroy();
20  // savu.FadeOut(5); // ei tuhoa oliota efektin jälkeen
21  Timer timer = new Timer();
22  timer.Interval = 3;
23  timer.Start();
24  timer.Timeout += delegate()
25  {
26  savu.Destroy();
27  timer.Stop();
28  };
29  // savunAani.Play();
30 #endif
31  }
32 }
33 
34 
35 public class Syotava : PhysicsObject
36 {
37  public Syotava(PhysicsGame peli, double w, Vector p, string tunniste, int elinaika)
38  : base(w, w)
39  {
40  this.Position = p;
41  this.Image = Game.LoadImage(tunniste);
42  this.Height = this.Width * this.Image.Height / this.Image.Width;
43  peli.Add(this);
44  this.Tag = tunniste;
45  this.LifetimeLeft = new TimeSpan(10000 * elinaika);
46  }
47 }
48 
49 
50 public class Luu : Syotava
51 {
52  public Luu(PhysicsGame peli, double w, Vector p)
53  : base(peli, w, p, "luu", 1000)
54  {
55  this.Destroyed += delegate() { Efektit.Savuta(peli, this); };
56  }
57 }
58 
59 
60 public class Juusto : Syotava
61 {
62  public Juusto(PhysicsGame peli, double w, Vector p)
63  : base(peli, w, p, "juusto", 5000)
64  {
65  // this.Destroyed += delegate() { Savuta(peli, this); };
66  }
67 }
68 
69 
70 public class Elain : PhysicsObject // PlatformCharacter
71 {
72  private string nimi;
73  public string Nimi { get { return nimi; } }
74 
75  private bool vasen = false;
76 
77  public Elain(PhysicsGame peli, double w, Vector p, string nimi, string tunniste)
78  : base(w, w)
79  {
80  this.Position = p;
81  this.Image = Game.LoadImage(tunniste);
82  this.Tag = tunniste;
83  this.Height = this.Width * this.Image.Height / this.Image.Width;
84  this.nimi = nimi;
85  Label label = new Label(nimi);
86  this.Add(label);
87  peli.Add(this);
88  CanRotate = false;
89  }
90 
91  public virtual void Liiku(Vector suunta)
92  {
93  //this.Hit(new Vector(0,suunta.Y));
94  //Walk(suunta.X);
95  this.Hit(suunta);
96  bool nytVasen = suunta.X < 0;
97  if (nytVasen ^ vasen) this.MirrorImage();
98  vasen = nytVasen;
99  }
100 
101 
102  public virtual void Aantele()
103  {
104 
105  }
106 
107 
108  public virtual void Syo(PhysicsObject kuka, PhysicsObject kenet)
109  {
110  kenet.Destroy();
111  this.Size += new Vector(10, 10);
112  this.Aantele();
113  }
114 }
115 
116 
117 public class Koira : Elain
118 {
119  public Koira(PhysicsGame peli, double w, Vector p, string nimi = "")
120  : base(peli, w, p, nimi, "koira")
121  {
122  peli.AddCollisionHandler(this, "luu", Syo);
123  }
124 
125 
126  public override void Aantele()
127  {
128  Game.PlaySound("KoiranAani1");
129  }
130 }
131 
132 
133 public class Kissa : Elain
134 {
135  public Kissa(PhysicsGame peli, double w, Vector p, string nimi = "")
136  : base(peli, w, p, nimi, "kissa")
137  {
138  peli.AddCollisionHandler(this, "hiiri", Syo);
139  }
140 
141  public override void Aantele()
142  {
143  Game.PlaySound("KissanAani01");
144  }
145 
146 }
147 
148 
149 public class Hiiri : Elain
150 {
151  public Hiiri(PhysicsGame peli, double w, Vector p, Juusto juusto)
152  : base(peli, w, p, "", "hiiri")
153  {
154  this.LifetimeLeft = new TimeSpan(0, 0, 0, 5);
155  peli.AddCollisionHandler(this, "juusto", Syo);
156  this.Destroyed += delegate() { new Luu(peli, Width * 0.8, Position); };
157  FollowerBrain brain = new FollowerBrain(juusto);
158  this.Brain = brain;
159  }
160 
161  public override void Syo(PhysicsObject kuka, PhysicsObject kenet)
162  {
163  base.Syo(kuka, kenet);
164  this.LifetimeLeft = new TimeSpan(0, 0, 0, 15);
165  }
166 
167  public override void Aantele()
168  {
169  Game.PlaySound("Naps");
170  }
171 }
[Doc-muoto] [Alkuperäinen .cs]