source: 2012/30/AnttiS/EksyksissäAvaruudessa/EksyksissäAvaruudessa/EksyksissäAvaruudessa/EksyksissäAvaruudessa.cs @ 3772

Revision 3772, 9.0 KB checked in by juiitamm, 11 years ago (diff)

Talletus.

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class EksyksissäAvaruudessa : PhysicsGame
10{
11    int kenttaNro = 1;
12    PhysicsObject Alus;
13    IntMeter pistelaskuri;
14    IntMeter elamaLaskuri;
15
16    EasyHighScore topLista = new EasyHighScore();
17
18    public override void Begin()
19    {
20        pistelaskuri = null;
21        elamaLaskuri = null;
22        SeuraavaKentta();
23    }
24    void SeuraavaKentta()
25    {
26        int pisteet = 0;
27        int elamat = 3;
28
29        if (pistelaskuri != null)
30        {
31            pisteet = pistelaskuri.Value;
32            elamat = elamaLaskuri.Value;
33        }
34
35        ClearAll();
36        LuoAlus();
37        AsetaOhjaimet();
38        LuoTausta();
39        Luopistlaskuri();
40        LuoElamalaskuri();
41        LuoPistepallo();
42
43        pistelaskuri.Value = pisteet;
44        elamaLaskuri.Value = elamat;
45
46        if (kenttaNro == 1) SijoitaOliot1();
47        else if (kenttaNro == 2) SijoitaOliot2();
48        else if (kenttaNro == 3) SijoitaOliot3();
49        else if (kenttaNro == 4) SijoitaOliot4();
50        else if (kenttaNro == 5) ViimeinenKentta();
51        else if (kenttaNro == 6) luotoplista();
52        else Exit();
53
54        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
55    }
56
57    void LuoAlus()
58    {
59        Alus = new PhysicsObject(130, 40);
60        Alus.LinearDamping = 0.5;
61        Alus.CanRotate = false;
62        Alus.Image = LoadImage("avaruusalus");
63        Alus.X = -400;
64        Alus.Y = 0;
65        Alus.Restitution = 1.0;
66        Add(Alus);
67
68        AddCollisionHandler(Alus, "UFO", Kuole);
69        AddCollisionHandler(Alus, "pistepallo", KeraaPistepallo);
70    }
71    PhysicsObject LuoMaali(double x, double y)
72    {
73        PhysicsObject Maali = PhysicsObject.CreateStaticObject(169, 94);
74        Maali.Image = LoadImage("MAALI");
75        Add(Maali);
76        AddCollisionHandler(Alus, Maali, VaihdaKenttaa);
77        Maali.X = x;
78        Maali.Y = y;
79        return Maali;
80    }
81    void AsetaOhjaimet()
82    {
83
84        Keyboard.Listen(Key.Left, ButtonState.Down, KaannaAlusta, null, Angle.FromDegrees(1));
85        Keyboard.Listen(Key.Right, ButtonState.Down, KaannaAlusta, null, Angle.FromDegrees(-1));
86        Keyboard.Listen(Key.Up, ButtonState.Down, LiikutaAlusta, null, 2000.0);
87        Keyboard.Listen(Key.Down, ButtonState.Down, LiikutaAlusta, null, -2000.0);
88
89    }
90    void LiikutaAlusta(double nopeus)
91    {
92        Vector vektori = Vector.FromLengthAndAngle(nopeus, Alus.Angle);
93        Alus.Push(vektori);
94    }
95    void LuoTausta()
96    {
97        Level.CreateBorders(1.0, true);
98        Camera.ZoomToLevel();
99        Image taustaKuva = LoadImage("tausta");
100        Level.Background.Image = taustaKuva;
101        Level.Background.FitToLevel();
102    }
103    PhysicsObject LuoMaa(double x, double y)
104    {
105        PhysicsObject Maa = PhysicsObject.CreateStaticObject(207, 201);
106        Maa.Image = LoadImage("maa");
107        AddCollisionHandler(Alus, Maa, VaihdaKenttaa);
108        Maa.X = x;
109        Maa.Y = y;
110        Add(Maa);
111        return Maa;
112    }
113
114    void LuoPlaneetta1(double x, double y)
115    {
116        PhysicsObject planeetta = PhysicsObject.CreateStaticObject(90, 86);
117        Image planeetta1 = LoadImage("planeetta1");
118        planeetta.Image = planeetta1;
119        planeetta.X = x;
120        planeetta.Y = y;
121        planeetta.Restitution = 1.0;
122        Add(planeetta);
123    }
124    void KaannaAlusta(Angle kulma)
125    {
126        Alus.Angle += kulma;
127    }
128    void SijoitaOliot1()
129    {
130        LuoUfo(-200, -300, new Vector(150.0, -150.0));
131        LuoUfo(200, -300, new Vector(150.0, 150.0));
132        LuoPlaneetta1(-200, 50);
133        LuoPlaneetta1(-300, 200);
134        LuoPlaneetta1(-40, 120);
135        LuoPlaneetta1(200, 50);
136        LuoPlaneetta1(90, -200);
137        PhysicsObject maali = LuoMaali(200, -200);
138
139
140
141    }
142    void SijoitaOliot2()
143    {
144        LuoUfo(-200, 300, new Vector(-150.0, -150.0));
145        LuoUfo(200, 300, new Vector(150.0, 150.0));
146        LuoUfo(200, -300, new Vector(150.0, 150.0));
147        LuoPlaneetta1(200, 50);
148        LuoPlaneetta1(-300, 150);
149        LuoPlaneetta1(-40, -120);
150        LuoPlaneetta1(-200, -80);
151        LuoPlaneetta1(-90, 200);
152
153        PhysicsObject maali = LuoMaali(200, 200);
154
155    }
156    void SijoitaOliot3()
157    {
158        LuoUfo(-200, 300, new Vector(-150.0, -150.0));
159        LuoUfo(200, 300, new Vector(150.0, 150.0));
160        LuoUfo(200, -300, new Vector(150.0, 150.0));
161        LuoUfo(0, 0, new Vector(150.0, 150.0));
162        LuoPlaneetta1(200, 50);
163        LuoPlaneetta1(-300, 150);
164        LuoPlaneetta1(-40, -120);
165        LuoPlaneetta1(-200, -80);
166        LuoPlaneetta1(-90, 200);
167
168        PhysicsObject maali = LuoMaali(300, -100);
169
170
171    }
172    void SijoitaOliot4()
173    {
174        LuoUfo(-200, 300, new Vector(-150.0, -150.0));
175        LuoUfo(200, 300, new Vector(150.0, 150.0));
176        LuoUfo(200, -300, new Vector(150.0, 150.0));
177        LuoUfo(0, 0, new Vector(150.0, 150.0));
178        LuoPlaneetta1(400, -200);
179        LuoPlaneetta1(-300, 50);
180        LuoPlaneetta1(-40, 120);
181        LuoPlaneetta1(-200, -200);
182        LuoPlaneetta1(-175, 220);
183
184        PhysicsObject maali = LuoMaali(200, 200);
185
186    }
187
188    void ViimeinenKentta()
189    {
190        LuoUfo(-200, 300, new Vector(-150.0, -150.0));
191        LuoUfo(200, 300, new Vector(150.0, 150.0));
192        LuoUfo(200, -300, new Vector(150.0, 150.0));
193        LuoUfo(0, 0, new Vector(150.0, 150.0));
194        LuoUfo(-200, 400, new Vector(-150.0, -150.0));
195        LuoUfo(400, 400, new Vector(150.0, 150.0));
196        LuoUfo(-200, -300, new Vector(-150.0, -150.0));
197        LuoUfo(200, -300, new Vector(150.0, 150.0));
198        LuoUfo(400, -300, new Vector(150.0, 150.0));
199
200        LuoPlaneetta1(80, -400);
201        LuoPlaneetta1(-400, 100);
202
203        PhysicsObject Maa = LuoMaa(200, 200);
204
205    }
206
207
208    void LuoUfo(double x, double y, Vector suunta)
209    {
210        PhysicsObject Ufo = new PhysicsObject(123, 71);
211        Image ufo = LoadImage("ufo");
212        Ufo.CanRotate = false;
213        Ufo.Image = ufo;
214        Ufo.X = x;
215        Ufo.Y = y;
216        Ufo.Tag = "UFO";
217        Ufo.Restitution = 1.0;
218        Vector impulssi = suunta;
219        Ufo.Hit(impulssi);
220        Add(Ufo);
221
222    }
223
224    void LuoElamalaskuri()
225    {
226        elamaLaskuri = new IntMeter(3);
227        elamaLaskuri.LowerLimit += new Action(pisteLaskuri_LowerLimit);
228
229        Label pisteNaytto = new Label();
230        pisteNaytto.X = Screen.Right - 100;
231        pisteNaytto.Y = Screen.Top - 100;
232        pisteNaytto.TextColor = Color.Blue;
233        pisteNaytto.Color = Color.Red;
234        pisteNaytto.BindTo(elamaLaskuri);
235        Add(pisteNaytto);
236
237        pisteNaytto.Title = "Elämät";
238    }
239
240    void pisteLaskuri_LowerLimit()
241    {
242        Alus.Destroy();
243        Widget kuva = new Widget(LoadImage("game over"));
244        kuva.Width = Screen.Width;
245        kuva.Height = Screen.Height;
246        Add(kuva);
247        luotoplista();
248    }
249    void luotoplista()
250{
251        topLista.EnterAndShow(pistelaskuri.Value);
252        topLista.HighScoreWindow.Closed += delegate { Begin(); };
253        topLista.HighScoreWindow.Color = Color.Snow;
254       
255    }
256    void PelaajaTormasi(PhysicsObject tormaaja, PhysicsObject kohde)
257    {
258        MessageDisplay.Add("Pelaaja törmäsi!");
259    }
260
261    void Kuole(PhysicsObject tormaaja, PhysicsObject kohde)
262    {
263        elamaLaskuri.Value -= 1;
264    }
265    void VaihdaKenttaa(PhysicsObject olio, PhysicsObject maali)
266    {
267        kenttaNro++;
268        SeuraavaKentta();
269    }
270
271    void Luopistlaskuri()
272    {
273        pistelaskuri = new IntMeter(0);
274
275        Label pisteNaytto = new Label();
276        pisteNaytto.X = Screen.Left + 100;
277        pisteNaytto.Y = Screen.Top - 100;
278        pisteNaytto.TextColor = Color.Red;
279        pisteNaytto.Color = Color.Blue;
280
281        pisteNaytto.Title = "Pisteet";
282
283        pisteNaytto.BindTo(pistelaskuri);
284        Add(pisteNaytto);
285    }
286    void LuoPistepallo()
287    {
288        int i = 0;
289
290        while (i < 50)
291        {
292            PhysicsObject pistepallo = new PhysicsObject (20, 19);
293            pistepallo.Image = LoadImage("pistepallo");
294            pistepallo.Tag = "pistepallo";
295            pistepallo.X = RandomGen.NextDouble(Level.Left, Level.Right);
296            pistepallo.Y = RandomGen.NextDouble(Level.Bottom, Level.Top);
297            pistepallo.Restitution = 1.0;
298            Add(pistepallo);
299            i++;
300        }
301    }
302    void KeraaPistepallo(PhysicsObject Alus, PhysicsObject pistepallo)
303    {
304        MessageDisplay.Add("Pelaaja törmäsi!");
305        pistelaskuri.Value += 10;
306        pistepallo.Destroy(); 
307    }
308    void PelaajaKuoli()
309    {
310        Alus.Destroy();
311        topLista.EnterAndShow(pistelaskuri.Value);
312        topLista.HighScoreWindow.Closed += delegate { Begin(); };
313    }
314
315}
316
317
318
319
Note: See TracBrowser for help on using the repository browser.