source: 2012/27/EemilK/Survive untill U die!/Survive untill U die!/Survive untill U die!/Survive_untill_U_die_.cs @ 3634

Revision 3634, 11.6 KB checked in by eekrkolu, 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 Survive_untill_U_die_ : PhysicsGame
10{
11    ScoreList topLista = new ScoreList(1000, false, 0);
12
13
14    Vector oikealle = new Vector(100, 0);
15    Vector vasemmalle = new Vector(-100, 0);
16    Vector ylos = new Vector(0, 1);
17    PhysicsObject vihollinen;
18    PhysicsObject enemy;
19    PhysicsObject pallo;
20    PhysicsObject pallo2;
21    PhysicsObject vasenReuna;
22    PhysicsObject oikeaReuna;
23    PlasmaCannon pyssy;
24    PlasmaCannon pyssy2;
25    ExplosionSystem rajahdys;
26    ExplosionSystem rajahdysPelaaja;
27
28    IntMeter laskuri;
29    IntMeter laskuri2;
30    IntMeter laskuri23;
31
32    Image kuva = LoadImage("lawn_summer_rf_0001_large");
33    public override void Begin()
34    {
35        if (DataStorage.Exists("Ending"))
36            topLista = DataStorage.Load<ScoreList>(topLista, "Ending");
37
38        rajahdysPelaaja = new ExplosionSystem(LoadImage("atom bomb"), 100);
39        rajahdysPelaaja.MaxScale = 400;
40        rajahdysPelaaja.MinScale = 340;
41        Add(rajahdysPelaaja);
42        int i = 0;
43        while (i < 100)
44        {
45            LuoPalloja(RandomGen.NextDouble(-400, 400), RandomGen.NextDouble(600, 200));
46            i++;
47            LuoPallot(RandomGen.NextDouble(-400, 400), RandomGen.NextDouble(600, 200));
48            i++;
49        }
50        LuoPistelaskuri();
51        LuoPistelaskuri2();
52        LuoPistelaskuri3();
53
54        pyssy = new PlasmaCannon(20, 20);
55        pyssy.InfiniteAmmo = true;
56        pyssy2 = new PlasmaCannon(20, 20);
57        pyssy2.InfiniteAmmo = true;
58        Level.Background.Image = kuva;
59
60        // Pelaaja 1 (vasemman puoleinen)
61        pallo = new Pelaaja(170, 170);
62        pallo.MakeStatic();
63        pallo.Shape = Shape.Circle;
64        pallo.Image = LoadImage("p1");
65        pallo.Angle = Angle.RightAngle;
66        pallo.RotateImage = false;
67        pallo.Tag = "pallo";
68        pallo.X = -240;
69        pallo.Y = -200;
70        pallo.Destroying += delegate { rajahdysPelaaja.AddEffect(pallo.Position, 100); };
71        AddCollisionHandler(pallo, "vihu", PelaajaOsuu);
72
73        Add(pallo);
74        pallo.Add(pyssy);
75
76        // Pelaaja 2 (oikean puoleinen)
77        pallo2 = new Pelaaja(170, 170);
78        pallo2.MakeStatic();
79        pallo2.Shape = Shape.Circle;
80        pallo2.Image = LoadImage("p2");
81        pallo2.Angle = Angle.RightAngle;
82        pallo2.RotateImage = false;
83        pallo2.Tag = "pallo2";
84        pallo2.X = 240;
85        pallo2.Y = -200;
86        Add(pallo2);
87        pallo2.Add(pyssy2);
88        pallo2.Destroyed += delegate { rajahdysPelaaja.AddEffect(pallo2.Position, 100); };
89        AddCollisionHandler(pallo2, "vihu", PelaajaOsuu);
90        rajahdys = new ExplosionSystem(LoadImage("hitler"), 10);
91        Add(rajahdys);
92
93
94        vasenReuna = Level.CreateLeftBorder();
95        vasenReuna.KineticFriction = 0.0;
96        vasenReuna.Restitution = 1.0;
97        vasenReuna.IsVisible = false;
98        oikeaReuna = Level.CreateRightBorder();
99        oikeaReuna.KineticFriction = 0.0;
100        oikeaReuna.Restitution = 1.0;
101        oikeaReuna.IsVisible = false;
102        Level.CreateBorders();
103        Camera.ZoomToLevel();
104        Keyboard.Listen(Key.D, ButtonState.Down, Liiku, "pelaaja1: oikealle", pallo, oikealle);
105        Keyboard.Listen(Key.D, ButtonState.Released, Liiku, null, pallo, Vector.Zero);
106        Keyboard.Listen(Key.A, ButtonState.Down, Liiku2, "pelaaja1: vasemmalle", pallo, vasemmalle);
107        Keyboard.Listen(Key.A, ButtonState.Released, Liiku2, null, pallo, Vector.Zero);
108        Keyboard.Listen(Key.Right, ButtonState.Down, Liiku3, "pelaaja2: oikealle", pallo2, oikealle);
109        Keyboard.Listen(Key.Right, ButtonState.Released, Liiku3, null, pallo2, Vector.Zero);
110        Keyboard.Listen(Key.Left, ButtonState.Down, Liiku4, "pelaaja2: vasemmalle", pallo2, vasemmalle);
111        Keyboard.Listen(Key.Left, ButtonState.Released, Liiku4, null, pallo2, Vector.Zero);
112        Keyboard.Listen(Key.W, ButtonState.Down, AmmuAseella, "pelaaja1: Ammu");
113        Keyboard.Listen(Key.Up, ButtonState.Down, AmmuAseella2, "pelaaja2: Ammu");
114        Keyboard.Listen(Key.Q, ButtonState.Pressed, heitaKranu, "pelaaja1: heitä Kranu");
115        Keyboard.Listen(Key.RightControl, ButtonState.Pressed, heitäKranu2, "pelaaja2: heitä Kranu");
116
117        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
118        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
119
120    }
121    void LuoPalloja(double x, double y)
122    {
123        vihollinen = new Vihu(70, 70);
124        vihollinen.Shape = Shape.Circle;
125        vihollinen.Image = LoadImage("nazi");
126        vihollinen.X = x;
127        vihollinen.Tag = "vihu";
128        vihollinen.Y = y;
129        vihollinen.Restitution = 1.0;
130        Add(vihollinen);
131
132        FollowerBrain aivot = new FollowerBrain("pallo");
133        vihollinen.Brain = aivot;
134        aivot.Active = true;
135        aivot.Speed = 10;
136
137    }
138    void LuoPallot(double x, double y)
139    {
140        enemy = new Vihu(70, 70);
141        enemy.Shape = Shape.Circle;
142        enemy.Image = LoadImage("nazi");
143        enemy.Tag = "vihu";
144        enemy.X = x;
145        enemy.Y = y;
146        enemy.Restitution = 1.0;
147        Add(enemy);
148
149        FollowerBrain aivot2 = new FollowerBrain("pallo2");
150        enemy.Brain = aivot2;
151        aivot2.Active = true;
152        aivot2.Speed = 10;
153
154    }
155    void TallennaPisteet(Window sender)
156    {
157        DataStorage.Save<ScoreList>(topLista, "Ending");
158    }
159
160    void Liiku(PhysicsObject pallo, Vector oikealle)
161    {
162        if (oikealle == Vector.Zero || pallo.Right >= Level.Right)
163        {
164            pallo.Velocity = Vector.Zero;
165            return;
166        }
167        pallo.Velocity = oikealle;
168    }
169    void Liiku2(PhysicsObject pallo, Vector vasemmalle)
170    {
171        if (vasemmalle == Vector.Zero || pallo.Left <= Level.Left)
172        {
173            pallo.Velocity = Vector.Zero;
174            return;
175        }
176        pallo.Velocity = vasemmalle;
177    }
178    void Liiku3(PhysicsObject pallo2, Vector oikealle)
179    {
180        if (oikealle == Vector.Zero || pallo2.Right >= Level.Right)
181        {
182            pallo2.Velocity = Vector.Zero;
183            return;
184        }
185        pallo2.Velocity = oikealle;
186    }
187    void Liiku4(PhysicsObject pallo2, Vector vasemmalle)
188    {
189        if (vasemmalle == Vector.Zero || pallo2.Left <= Level.Left)
190        {
191            pallo2.Velocity = Vector.Zero;
192            return;
193
194        }
195        pallo2.Velocity = vasemmalle;
196    }
197    void AmmuAseella()
198    {
199        PhysicsObject panos = pyssy.Shoot();
200
201        if (panos != null)
202        {
203            panos.Color = Color.Blue;
204            panos.Size *= 1;
205            AddCollisionHandler(panos, delegate(PhysicsObject tormaaja, PhysicsObject kohde)
206            {
207                tormaaja.Destroy();
208                if (kohde.Tag.ToString() == "vihu")
209                {
210                    (kohde as Vihu).elamat.Value--;
211                    if ((kohde as Vihu).elamat.Value == 0)
212                    {
213                        rajahdys.AddEffect(kohde.Position, 1);
214                        laskuri.Value += 1;
215                        laskuri23.Value += 1;
216                        LuoPalloja(RandomGen.NextDouble(-400, 400), RandomGen.NextDouble(700, 200));
217                        LuoPallot(RandomGen.NextDouble(-400, 400), RandomGen.NextDouble(700, 200));
218                    }
219                }
220               
221            });
222        }
223    }
224    void AmmuAseella2()
225    {
226
227        PhysicsObject ammus2 = pyssy2.Shoot();
228
229        if (ammus2 != null)
230        {
231            ammus2.Color = Color.Red;
232            ammus2.Size *= 1;
233            AddCollisionHandler(ammus2, delegate(PhysicsObject tormaaja, PhysicsObject kohde)
234            {
235                tormaaja.Destroy();
236                if (kohde.Tag.ToString() == "vihu")
237                {
238                    (kohde as Vihu).elamat.Value--;
239                    if ((kohde as Vihu).elamat.Value == 0)
240                    {
241                        rajahdys.AddEffect(kohde.Position, 1);
242                        laskuri2.Value += 1;
243                        laskuri23.Value += 1;
244                        LuoPalloja(RandomGen.NextDouble(-400, 400), RandomGen.NextDouble(600, 200));
245                        LuoPallot(RandomGen.NextDouble(-400, 400), RandomGen.NextDouble(600, 200));
246                       
247                    }
248                }
249            });
250        }
251    }
252
253
254    void heitaKranu()
255    {
256        Grenade kranaatti;
257        kranaatti = new Grenade(10.0);
258        kranaatti.Angle = Angle.RightAngle;
259        pallo.Throw(kranaatti, Angle.Zero, 500);
260
261    }
262    void heitäKranu2()
263    {
264        Grenade kranaatti2;
265        kranaatti2 = new Grenade(10.0);
266        kranaatti2.Angle = Angle.RightAngle;
267        pallo2.Throw(kranaatti2, Angle.Zero, 500);
268
269    }
270
271    void LuoPistelaskuri()
272    {
273       
274        laskuri = new IntMeter(0);
275
276        Label pisteNaytto = new Label();
277        pisteNaytto.X = Screen.LeftSafe + 100;
278        pisteNaytto.Y = Screen.BottomSafe + 100;
279        pisteNaytto.TextColor = Color.Blue;
280        pisteNaytto.Color = Color.Red;
281
282        pisteNaytto.BindTo(laskuri);
283        pisteNaytto.Title = "p1: kills";
284        Add(pisteNaytto);
285}
286    void LuoPistelaskuri2()
287    {
288       
289        laskuri2 = new IntMeter(0);
290
291        Label pisteNaytto2 = new Label();
292        pisteNaytto2.X = Screen.RightSafe - 100;
293        pisteNaytto2.Y = Screen.BottomSafe + 100;
294        pisteNaytto2.TextColor = Color.Red;
295        pisteNaytto2.Color = Color.Blue;
296
297        pisteNaytto2.BindTo(laskuri2);
298        pisteNaytto2.Title = "p2: kills"; 
299        Add(pisteNaytto2);
300    }
301    void LuoPistelaskuri3()
302    {
303       
304        laskuri23 = new IntMeter(0);
305
306        Label pisteNaytto23 = new Label();
307        pisteNaytto23.X = 0;
308        pisteNaytto23.Y = Screen.BottomSafe + 100;
309        pisteNaytto23.TextColor = Color.Black;
310        pisteNaytto23.Color = Color.Green;
311       
312        pisteNaytto23.BindTo(laskuri23);
313        pisteNaytto23.Title = "Total Kills";
314        Add(pisteNaytto23);
315    }
316   
317    void PelaajaOsuu(PhysicsObject pelaaja, PhysicsObject kohde)
318    {
319        (pelaaja as Pelaaja).hp.Value--;
320        kohde.Destroy();
321        rajahdys.AddEffect(kohde.Position, 100);
322        Timer.SingleShot(1.0, delegate
323        {
324            if (pallo.IsDestroyed && pallo2.IsDestroyed)
325            {
326                PeliPaattyy();
327            }
328        });
329    }
330
331    void PeliPaattyy()
332    {
333        ClearAll();
334        HighScoreWindow topIkkuna = new HighScoreWindow(
335                     "Parhaat pisteet",
336                     "Onneksi olkoon, pääsit Top 1k listalle %p! Syötä nimesi:mutta oot silti paska, koska kuolit!",
337                     topLista, laskuri23.Value);
338        topIkkuna.Closed += TallennaPisteet;
339        Add(topIkkuna);
340
341
342    }
343
344}
345
346public class Vihu : PhysicsObject
347{
348    public IntMeter elamat;
349
350    public Vihu(double width, double height)
351        : base(width, height)
352    {
353        elamat = new IntMeter(10, 0, 10);
354        elamat.LowerLimit += delegate { this.Destroy(); };
355    }
356}
357   
358
359
360
361public class Pelaaja : PhysicsObject
362    {
363        public IntMeter hp;
364
365        public Pelaaja(double width, double height)
366            : base(width, height)
367        {
368            hp = new IntMeter(20, 0, 20);
369           
370            hp.LowerLimit += delegate { this.Destroy(); };
371        }
372    }   
Note: See TracBrowser for help on using the repository browser.