source: 2011/31/AleksiR/OmaPeli/OmaPeli/OmaPeli/Peli.cs @ 2550

Revision 2550, 11.2 KB checked in by almarimp, 12 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 Peli : PhysicsGame
10{
11   
12    List<Label> valikonKohdat;
13
14    PhysicsObject Alus1;
15    PhysicsObject Alus2;
16    PhysicsObject tahti;
17    PhysicsObject tahti1;
18    PhysicsObject tahti2;
19    LaserGun pyssy;
20    LaserGun pyssy2;
21    IntMeter pisteLaskuri1;
22    IntMeter pisteLaskuri2;
23
24    Image alus1Kuva = LoadImage("Alus1");
25    Image alus2Kuva = LoadImage("Alus2");
26    Image tahtiKuva = LoadImage("Tahti");
27
28    public override void Begin()
29    {
30        Valikko();
31    }
32
33    void Valikko()
34    {
35        ClearAll();
36        valikonKohdat = new List<Label>();
37        Level.BackgroundColor = Color.Black;
38        Level.Background.CreateStars();
39
40        Label kohta1 = new Label("Aloita uusi peli");
41        kohta1.Position = new Vector(0, 80);
42        valikonKohdat.Add(kohta1);
43
44        Label kohta2 = new Label("Ohjeet");
45        kohta2.Position = new Vector(0, 40);
46        valikonKohdat.Add(kohta2);
47
48        Label kohta3= new Label("Lopeta");
49        kohta3.Position = new Vector(0, 0);
50        valikonKohdat.Add(kohta3);
51
52        foreach (Label valikonKohta in valikonKohdat)
53        {
54            Add(valikonKohta);
55        }
56
57        Mouse.ListenOn(kohta1, MouseButton.Left, ButtonState.Pressed, AloitaUusiPeli, null);
58        Mouse.ListenOn(kohta2, MouseButton.Left, ButtonState.Pressed, ShowControlHelp, null);
59        Mouse.ListenOn(kohta3, MouseButton.Left, ButtonState.Pressed, Exit, null);
60
61        Mouse.IsCursorVisible = true;
62        Mouse.ListenMovement(1.0, valikossaLiikkuminen, null);
63        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "");
64    }
65
66    void valikossaLiikkuminen(AnalogState hiirenTila)
67    {
68        foreach (Label kohta in valikonKohdat)
69        {
70            if (Mouse.IsCursorOn(kohta))
71            {
72                kohta.TextColor = Color.Red;
73            }
74            else
75            {
76                kohta.TextColor = Color.GreenYellow;
77            }
78
79        }
80    }
81
82    void AloitaPeli()
83    {
84        ClearAll();
85        AloitaUusiPeli();
86    }
87
88
89    void AloitaUusiPeli()
90    {
91        ClearAll();
92        Nappaimet();
93        LuoAlus1();
94        LuoAlus2();
95        LuoLaskuri1();
96        LuoLaskuri2();
97        LuoKentta();
98        LuoTahdet();
99
100        FollowerBrain aly1 = new FollowerBrain();
101        aly1.Target = Alus1;
102        aly1.FollowAlways = true;
103        aly1.TargetFollowDistance = 750;
104        aly1.Speed = 50;
105
106        FollowerBrain aly2 = new FollowerBrain();
107        aly2.Target = Alus2;
108        aly2.FollowAlways = true;
109        aly2.TargetFollowDistance = 500;
110        aly2.Speed = 50;
111
112        tahti1 = new PhysicsObject(25, 25);
113        tahti1.Shape = Shape.Star;
114        tahti1.Color = Color.Yellow;
115        tahti1.Y = -300;
116        tahti1.X = -100;
117        tahti1.Brain = aly1;
118        tahti1.Tag = "Tähti";
119        tahti1.IgnoresCollisionResponse = false;
120
121        Add(tahti1);
122
123        tahti2 = new PhysicsObject(25, 25);
124        tahti2.Shape = Shape.Star;
125        tahti2.Color = Color.Yellow;
126        tahti2.Y = -300;
127        tahti2.X = 100;
128        tahti2.Brain = aly2;
129        tahti2.Tag = "Tähti";
130        tahti2.IgnoresCollisionResponse = false;
131
132        Add(tahti2);
133
134    }
135
136    void LuoKentta()
137    {
138        Level.CreateBorders(1.0, false);
139        Level.BackgroundColor = Color.Black;
140        Level.Background.CreateStars();
141        Level.Background.FitToLevel();
142
143        Gravity = new Vector(0, -50);
144
145        IsFullScreen = true;
146        Camera.ZoomToLevel();
147    }
148
149    void LuoTahdet()
150    {
151        int i = 0;
152
153        while (i < 2)
154        {
155            LuoUusiTahti();
156            i++;
157        }
158
159    }
160
161    void LuoAlus1()
162    {
163        Alus1 = new PhysicsObject(10, 30);
164        Alus1.Shape = Shape.Triangle;
165        Alus1.Color = Color.DarkRed;
166        Alus1.CanRotate = false;
167        Alus1.IgnoresExplosions = true;
168        Alus1.Restitution = 0;
169        Alus1.X = -250;
170        Alus1.Tag = "Alus";
171        Alus1.Image = tahtiKuva;
172        Add(Alus1);
173
174        pyssy = new LaserGun(20, 5);
175        pyssy.Angle += Angle.FromDegrees(90);
176        pyssy.Shoot();
177        pyssy.ProjectileCollision = LaserOsuu;
178        pyssy.AmmoIgnoresGravity = true;
179        pyssy.InfiniteAmmo = true;
180        pyssy.MaxAmmoLifetime = TimeSpan.MaxValue;
181        pyssy.IsVisible = false;
182        Alus1.Add(pyssy);
183    }
184
185    void LuoAlus2()
186    {
187        Alus2 = new PhysicsObject(10, 30);
188        Alus2.Shape = Shape.Triangle;
189        Alus2.Color = Color.ForestGreen;
190        Alus2.CanRotate = false;
191        Alus2.IgnoresExplosions = true;
192        Alus2.Restitution = 0;
193        Alus2.X = 250;
194        Alus2.Tag = "Alus";
195        Alus2.Image = tahtiKuva;
196        Add(Alus2);
197
198
199        pyssy2 = new LaserGun(20, 5);
200        pyssy2.Angle += Angle.FromDegrees(90);
201        pyssy2.Shoot();
202        pyssy2.MaxAmmoLifetime = TimeSpan.MaxValue;
203        pyssy2.ProjectileCollision = Laser2Osuu;
204        pyssy2.AmmoIgnoresGravity = true;
205        pyssy2.InfiniteAmmo = true;
206        pyssy2.IsVisible = false;
207        Alus2.Add(pyssy2);
208    }
209
210    void Nappaimet()
211    {
212
213        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Valikko, "Palaa valikkoon");
214
215        Keyboard.Listen(Key.Left, ButtonState.Down,LiikutaPelaajaa1, "Pelaaja 1 liikkuu vasemmalle", new Vector(-500, 0));
216        Keyboard.Listen(Key.Right, ButtonState.Down,LiikutaPelaajaa1, "Pelaaja 1 liikkuu oikealle", new Vector(500, 0));
217        Keyboard.Listen(Key.Up, ButtonState.Down,LiikutaPelaajaa1, "Pelaaja1 liikkuu ylös", new Vector(0, 500));
218        Keyboard.Listen(Key.Down, ButtonState.Down,LiikutaPelaajaa1, "Pelaaja 1 liikkuu alas", new Vector(0, -500));
219
220        Keyboard.Listen(Key.A, ButtonState.Down,LiikutaPelaajaa2, "Pelaaja 2 liikkuu vasemmalle", new Vector(-500, 0));
221        Keyboard.Listen(Key.D, ButtonState.Down, LiikutaPelaajaa2, "Pelaaja 2 liikkuu oikealle", new Vector(500, 0));
222        Keyboard.Listen(Key.W, ButtonState.Down, LiikutaPelaajaa2, "Pelaaja 2 liikkuu ylös", new Vector(0, 500));
223        Keyboard.Listen(Key.S, ButtonState.Down, LiikutaPelaajaa2, "Pelaaja 2 liikkuu alas", new Vector(0, -500));
224         
225        Keyboard.Listen(Key.Space, ButtonState.Down, Ampuu, null);
226        Keyboard.Listen(Key.Tab, ButtonState.Down, Ampuu2, null);
227    }
228
229    void Ampuu()
230    {
231        pyssy.Shoot();
232    }
233
234    void Ampuu2()
235    {
236        pyssy2.Shoot();
237    }
238
239    void LiikutaPelaajaa1(Vector vektori1)
240    {
241        Alus1.Push(vektori1);
242    }
243
244    void LiikutaPelaajaa2(Vector vektori2)
245    {
246        Alus2.Push(vektori2);
247    }
248
249    protected override void Update(Time time)
250    {
251        base.Update(time);
252        if (Alus1 != null && Alus2 != null)
253        {
254        Alus1.Angle = Alus1.Velocity.Angle - Angle.FromDegrees(90);
255        Alus2.Angle = Alus2.Velocity.Angle - Angle.FromDegrees(90);
256        }
257    }
258
259
260    void LuoUusiTahti()
261    {
262        TagFollowerBrain aly = new TagFollowerBrain("Alus", 5000);
263        aly.TargetFollowDistance = 500;
264
265        tahti = new PhysicsObject(30, 30);
266        tahti.Shape = Shape.Star;
267        tahti.Color = RandomGen.NextColor();
268        tahti.Y = RandomGen.NextDouble(-400, 400);
269        tahti.X = RandomGen.NextDouble(-400, 400);
270        tahti.Brain = aly;
271        tahti.Tag = "Tähti";
272        tahti.IgnoresGravity = true;
273        tahti.Restitution = 10;
274        AddCollisionHandler(tahti, tahtiTormasi);
275        Add(tahti);
276    }
277
278
279    void LuoLaskuri1()
280    {
281        pisteLaskuri1 = new IntMeter(0);
282        pisteLaskuri1.MaxValue = 1000;
283        pisteLaskuri1.UpperLimit += Laskuri1Max;
284
285        Label pisteNaytto = new Label();
286        pisteNaytto.X = Screen.Left + 100;
287        pisteNaytto.Y = Screen.Top - 100;
288        pisteNaytto.TextColor = Color.Red;
289
290        pisteNaytto.BindTo(pisteLaskuri1);
291        Add(pisteNaytto);
292
293        Label pisteTeksti = new Label("Pisteitä: ");
294        pisteTeksti.X = Screen.Left + 50;
295        pisteTeksti.Y = Screen.Top - 100;
296        pisteTeksti.TextColor = Color.Red;
297        Add(pisteTeksti);
298    }
299
300    void LuoLaskuri2()
301    {
302        pisteLaskuri2 = new IntMeter(0);
303        pisteLaskuri2.MaxValue = 1000;
304        pisteLaskuri2.UpperLimit += Laskuri2Max;
305
306        Label pisteNaytto2 = new Label();
307        pisteNaytto2.X = Screen.Right - 100;
308        pisteNaytto2.Y = Screen.Top - 100;
309        pisteNaytto2.TextColor = Color.Maroon;
310
311        pisteNaytto2.BindTo(pisteLaskuri2);
312        Add(pisteNaytto2);
313
314        Label pisteTeksti2 = new Label("Pisteitä: ");
315        pisteTeksti2.X = Screen.Right - 150;
316        pisteTeksti2.Y = Screen.Top - 100;
317        pisteTeksti2.TextColor = Color.GreenYellow;
318        Add(pisteTeksti2);
319    }
320
321
322    void LaserOsuu(PhysicsObject ammus, PhysicsObject kohde)
323    {
324        Explosion rajahdys = new Explosion(2.5);
325        rajahdys.Position = ammus.Position;
326        Add(rajahdys);
327        ammus.Destroy();
328
329        if (kohde.IsDestroyed)
330        {
331            return;
332        }
333
334        if (kohde.Tag.ToString() == "Tähti")
335        {
336            ammus.Destroy();
337            kohde.Destroy();
338            LuoUusiTahti();
339            pisteLaskuri1.Value += 10;
340        }
341
342        if (kohde == Alus2)
343        {
344            ammus.Destroy();
345            pisteLaskuri2.Value -= 10;
346        }
347
348    }
349
350    void Laser2Osuu(PhysicsObject ammus, PhysicsObject kohde)
351    {
352        Explosion rajahdys = new Explosion(2.5);
353        rajahdys.Position = ammus.Position;
354        Add(rajahdys);
355        ammus.Destroy();
356
357        if (kohde.IsDestroyed)
358        {
359            return;
360        }
361
362        if (kohde.Tag.ToString() == "Tähti")
363        {
364            ammus.Destroy();
365            kohde.Destroy();
366            LuoUusiTahti();
367            pisteLaskuri1.Value += 10;
368        }
369
370        if (kohde == Alus1)
371        {
372            ammus.Destroy();
373            pisteLaskuri1.Value -= 10;
374        }
375
376    }
377
378
379    void tahtiTormasi(PhysicsObject tahti, PhysicsObject kohde)
380    {
381        if (kohde == Alus2)
382        {
383            tahti.Destroy();
384            pisteLaskuri2.Value -= 100;
385            LuoUusiTahti();
386        }
387
388        if (kohde == Alus1)
389        {
390            tahti.Destroy();
391            pisteLaskuri1.Value -= 100;
392            LuoUusiTahti();
393        }
394
395    }
396
397
398    void Laskuri1Max(int arvo)
399    {
400        MessageDisplay.TextColor = Color.Fuchsia;
401        MessageDisplay.Add("Pelaaja 1 voitti pelin. Uusi peli alkaa 5 sekunnin kuluttua.");
402        Alus1.Destroy();
403        Alus2.Destroy();
404        Timer.SingleShot(5.0, ajastinLaukesi);
405    }
406
407    void Laskuri2Max(int arvo)
408    {
409        MessageDisplay.TextColor = Color.Aqua;
410        MessageDisplay.Add("Pelaaja 2 voitti pelin. Uusi peli alkaa 5 sekunnin kuluttua.");
411        Alus1.Destroy();
412        Alus2.Destroy();
413        Timer.SingleShot(5.0, ajastinLaukesi);
414    }
415
416    void ajastinLaukesi()
417    {
418        ClearAll();
419        AloitaUusiPeli();
420    }
421
422}
Note: See TracBrowser for help on using the repository browser.