source: 2017/24/EljaL/TheMetsäsurvivor/TheMetsäsurvivor/TheMetsäsurvivor/TheMetsäsurvivor.cs @ 8733

Revision 8733, 10.7 KB checked in by npo17_22, 6 years ago (diff)

kokonaan valmis

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class TheMetsäsurvivor : PhysicsGame
10{
11   
12    PhysicsObject ihminen;
13    PhysicsObject karhu;
14    Image ihminenkuva = LoadImage("ihminen");
15    Image karhukuva = LoadImage("karhu");
16    Image puukuva = LoadImage("Puu");
17    Image kivikuva = LoadImage("kivi");
18    Timer aikaLaskuri;
19    SoundEffect karhuaani = LoadSoundEffect("karhuaani");
20    SoundEffect musiikki = LoadSoundEffect("musiikki");
21    SoundEffect loppumusa = LoadSoundEffect("loppumusa");
22
23    public override void Begin()
24    {
25        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
26
27
28        Mouse.IsCursorVisible = true;
29
30        Camera.Zoom(1.0);
31
32        Reuna(10, 4000, -2000, 0);
33        Reuna(10, 4000, 2000, 0);
34        Reuna(4000, 10, 0, 2000);
35        Reuna(4000, 10, 0, -2000);
36
37        ihminen = new PhysicsObject(40, 40);
38        ihminen.CanRotate = true;
39        ihminen.Restitution = 0.0;
40        ihminen.Image = ihminenkuva;
41        ihminen.Shape = Shape.Circle;
42        Add(ihminen);
43
44        FollowerBrain seuraajanAivot = new FollowerBrain(ihminen);
45        RandomMoverBrain satunnaisAivot = new RandomMoverBrain(200);
46        satunnaisAivot.ChangeMovementSeconds = 3;
47
48        karhu = new PhysicsObject(40.0, 40.0);
49        karhu.Color = Color.Brown;
50        karhu.X = RandomGen.NextDouble(2000, -2000);
51        karhu.Y = RandomGen.NextDouble(2000, -2000);
52        karhu.Tag = "karhu";
53        karhu.Brain = seuraajanAivot;
54        karhu.Image = karhukuva;
55        Add(karhu);
56
57        seuraajanAivot.Speed = 100;
58        seuraajanAivot.DistanceFar = 1000;
59        seuraajanAivot.FarBrain = satunnaisAivot;
60        seuraajanAivot.FarBrain = satunnaisAivot;
61
62
63
64
65
66        AddCollisionHandler(ihminen, karhu, karhutormays);
67
68        for (int i = 0; i < RandomGen.NextInt(10, 30); i++)
69        {
70            teekivi();
71        }
72
73        for (int i = 0; i < RandomGen.NextInt(100, 200); i++)
74        {
75            teepuu();
76        }
77
78        LuoPuulaskuri();
79        LuoKivilaskuri();
80        luopojolaskuri();
81        luohplaskuri();
82        luopistegen();
83        LuoAikaLaskuri();
84        luopuugen();
85        luokivigen();
86
87        luokentta();
88
89        Keyboard.Listen(Key.Left, ButtonState.Down, käännäpelaajaav, null);
90        Keyboard.Listen(Key.Right, ButtonState.Down, käännäpelaajaao, null);
91        Keyboard.Listen(Key.Up, ButtonState.Down, käännäpelaajaay, null);
92        Keyboard.Listen(Key.Down, ButtonState.Down, käännäpelaajaaa, null);
93
94        Keyboard.Listen(Key.A, ButtonState.Down, LiikutaPelaajaa, null, new Vector(-50, 0));
95        Keyboard.Listen(Key.D, ButtonState.Down, LiikutaPelaajaa, null, new Vector(50, 0));
96        Keyboard.Listen(Key.W, ButtonState.Down, LiikutaPelaajaa, null, new Vector(0, 50));
97        Keyboard.Listen(Key.S, ButtonState.Down, LiikutaPelaajaa, null, new Vector(0, -50));
98
99        Camera.Follow(ihminen);
100    }
101
102    void LiikutaPelaajaa(Vector vektori)
103    {
104        ihminen.Push(vektori);
105    }
106    void luokentta()
107    {
108
109
110        Level.Background.Color = Color.Wheat;
111
112        AddCollisionHandler(ihminen, "puu", puutormays);
113    }
114    void käännäpelaajaav()
115    {
116        ihminen.Angle = Angle.FromDegrees(-90);
117    }
118    void käännäpelaajaao()
119    {
120        ihminen.Angle = Angle.FromDegrees(90);
121    }
122    void käännäpelaajaay()
123    {
124        ihminen.Angle = Angle.FromDegrees(180);
125    }
126    void käännäpelaajaaa()
127    {
128        ihminen.Angle = Angle.FromDegrees(0);
129    }
130    void teekivi()
131    {
132        PhysicsObject kivi = PhysicsObject.CreateStaticObject(100.0, 100.0);
133        kivi.Color = Color.Gray;
134        kivi.X = RandomGen.NextDouble(-2000, 2000);
135        kivi.Y = RandomGen.NextDouble(-2000, 2000);
136        kivi.Tag = "kivi";
137        kivi.Tag = "tolppa";
138        kivi.Image = kivikuva;
139        kivi.Shape = Shape.Circle;
140        Add(kivi);
141
142
143
144
145
146        AddCollisionHandler(ihminen, kivi, kivitormays);
147
148    }
149    IntMeter puuLaskuri;
150
151    void LuoPuulaskuri()
152    {
153        puuLaskuri = new IntMeter(0);
154
155        Label puuNaytto = new Label();
156        puuNaytto.X = Screen.Left + 100;
157        puuNaytto.Y = Screen.Top - 100;
158        puuNaytto.TextColor = Color.Brown;
159        puuNaytto.Color = Color.Wheat;
160        puuNaytto.Title = "puu";
161
162        puuNaytto.BindTo(puuLaskuri);
163        Add(puuNaytto);
164    }
165    IntMeter kivilaskuri;
166
167    void LuoKivilaskuri()
168    {
169        kivilaskuri = new IntMeter(0);
170
171        Label kiviNaytto = new Label();
172        kiviNaytto.X = Screen.Left + 100;
173        kiviNaytto.Y = Screen.Top - 75;
174        kiviNaytto.TextColor = Color.Gray;
175        kiviNaytto.Color = Color.Wheat;
176        kiviNaytto.Title = "kivi";
177
178        kiviNaytto.BindTo(kivilaskuri);
179        Add(kiviNaytto);
180    }
181    void puutormays(PhysicsObject tormaaja, PhysicsObject puu)
182    {
183        puuLaskuri.Value += 1;
184
185    }
186    void kivitormays(PhysicsObject tormaaja, PhysicsObject kivi)
187    {
188        kivilaskuri.Value += 1;
189    }
190    IntMeter pojolaskuri;
191
192    void luopojolaskuri()
193    {
194        pojolaskuri = new IntMeter(0);
195
196        Label pojonaytto = new Label();
197        pojonaytto.X = Screen.Left + 100;
198        pojonaytto.Y = Screen.Top - 50;
199        pojonaytto.TextColor = Color.Black;
200        pojonaytto.Color = Color.Wheat;
201        pojonaytto.Title = "pojot";
202        if (pojolaskuri > 999)
203        {
204            MessageDisplay.Add("Voitit pelin!");
205        }
206
207
208        pojonaytto.BindTo(pojolaskuri);
209        Add(pojonaytto);
210    }
211    IntMeter hplaskuri;
212
213    void luohplaskuri()
214    {
215        hplaskuri = new IntMeter(10);
216        hplaskuri.MinValue = 0;
217        hplaskuri.LowerLimit += ripped;
218        hplaskuri.AddOverTime(1000000000, 10000000000);
219        hplaskuri.MaxValue = 10;
220
221        Label hpnaytto = new Label();
222        hpnaytto.X = Screen.Left + 100;
223        hpnaytto.Y = Screen.Top - 125;
224        hpnaytto.TextColor = Color.Red;
225        hpnaytto.Color = Color.Wheat;
226        hpnaytto.Title = "hp";
227
228        hpnaytto.BindTo(hplaskuri);
229        Add(hpnaytto);
230    }
231    void karhutormays(PhysicsObject ihminen, PhysicsObject karhu)
232    {
233        hplaskuri.Value -= 1;
234        karhuaani.Play();
235    }
236    void ripped()
237    {
238        MessageDisplay.Add("kuolit");
239        ihminen.Destroy();
240    }
241
242    void teepuu()
243    {
244        PhysicsObject puu = PhysicsObject.CreateStaticObject(100.0, 100.0);
245        puu.X = RandomGen.NextDouble(-2000, 2000);
246        puu.Y = RandomGen.NextDouble(-2000, 2000);
247        puu.Color = Color.Green;
248        puu.Tag = "tolppa";
249        puu.Tag = "puu";
250        puu.Image = puukuva;
251        puu.Shape = Shape.Circle;
252        Add(puu);
253    }
254    void Reuna(double leveys, double korkeus, double x, double y)
255    {
256        PhysicsObject reunapalikka = PhysicsObject.CreateStaticObject(leveys, korkeus);
257        reunapalikka.X = x;
258        reunapalikka.Y = y;
259        reunapalikka.Color = Color.Wheat;
260        Add(reunapalikka);
261
262
263    }
264    IntMeter pistegen;
265
266    void luopistegen()
267    {
268        pistegen = new IntMeter(0);
269        pistegen.MinValue = 0;
270
271
272        Label pistegennaytto = new Label();
273        pistegennaytto.X = Screen.Right + -150;
274        pistegennaytto.Y = Screen.Top - 100;
275        pistegennaytto.TextColor = Color.Blue;
276        pistegennaytto.Color = Color.Wheat;
277        pistegennaytto.Title = "pistegeneraattorit";
278        Keyboard.Listen(Key.I, ButtonState.Pressed, pistegeneraattori, null);
279
280        pistegennaytto.BindTo(pistegen);
281        Add(pistegennaytto);
282    }
283    void LuoAikaLaskuri()
284    {
285        aikaLaskuri = new Timer();
286        aikaLaskuri.Timeout += TestaaAika;
287        aikaLaskuri.Start();
288
289        Label aikaNaytto = new Label();
290        aikaNaytto.TextColor = Color.Black;
291        aikaNaytto.X = Screen.Left + 100;
292        aikaNaytto.Y = Screen.Top + -25;
293        aikaNaytto.Color = Color.Wheat;
294        aikaNaytto.DecimalPlaces = 0;
295
296
297        aikaNaytto.BindTo(aikaLaskuri.SecondCounter);
298        Add(aikaNaytto);
299    }
300    IntMeter puugen;
301
302    void TestaaAika()
303    {
304        if (pojolaskuri > 999)
305        {
306            aikaLaskuri.Pause();
307            loppumusa.Play();
308        }
309    }
310
311    void luopuugen()
312    {
313        puugen = new IntMeter(0);
314
315        Label puugennaytto = new Label();
316        puugennaytto.X = Screen.Right + -150;
317        puugennaytto.Y = Screen.Top - 75;
318        puugennaytto.TextColor = Color.Brown;
319        puugennaytto.Color = Color.Wheat;
320        puugennaytto.Title = "puugeneraattorit";
321        Keyboard.Listen(Key.P, ButtonState.Pressed, puugeneraattori, null);
322
323        puugennaytto.BindTo(puugen);
324        Add(puugennaytto);
325    }
326    IntMeter kivigen;
327
328
329    void luokivigen()
330    {
331        kivigen = new IntMeter(0);
332
333        Label kivigennaytto = new Label();
334        kivigennaytto.X = Screen.Right + -150;
335        kivigennaytto.Y = Screen.Top - 50;
336        kivigennaytto.TextColor = Color.Gray;
337        kivigennaytto.Color = Color.Wheat;
338        kivigennaytto.Title = "kivigeneraattorit";
339        Keyboard.Listen(Key.K, ButtonState.Pressed, kivigenraattori, null);
340
341        kivigennaytto.BindTo(kivigen);
342        Add(kivigennaytto);
343    }
344    void kivigenraattori()
345    {
346        musiikki.Play();
347        if (kivilaskuri > 9)
348        {
349            if (puuLaskuri > 29)
350            {
351                kivigen.Value += 1;
352                kivilaskuri.AddOverTime(1000000, 10000000);
353                kivilaskuri.Value -= 10;
354                puuLaskuri.Value -= 30;
355
356                kivilaskuri.AddOverTime(1000000 * kivigen, 10000000);
357            }
358        }
359
360    }
361    void puugeneraattori()
362    {
363        musiikki.Play();
364        if (puuLaskuri > 9)
365        {
366            if (kivilaskuri > 29)
367            {
368                puugen.Value += 1;
369                puuLaskuri.AddOverTime(1000000, 10000000);
370                puuLaskuri.Value -= 10;
371                kivilaskuri.Value -= 30;
372
373                puuLaskuri.AddOverTime(1000000 * puugen, 10000000);
374
375            }
376        }
377    }
378    void pistegeneraattori()
379    {
380        musiikki.Play();
381        if (puuLaskuri > 99)
382        {
383            if (kivilaskuri > 99)
384            {
385                pistegen.Value += 1;
386                pojolaskuri.AddOverTime(1000000, 10000000);
387                puuLaskuri.Value -= 100;
388                kivilaskuri.Value -= 100;
389
390                    pojolaskuri.AddOverTime(1000000*pistegen, 10000000);
391
392               
393            }
394        }
395    }
396}
Note: See TracBrowser for help on using the repository browser.