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

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