1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Controls; |
---|
6 | using Jypeli.Effects; |
---|
7 | using Jypeli.Widgets; |
---|
8 | |
---|
9 | public class Syonti : PhysicsGame |
---|
10 | { |
---|
11 | PhysicsObject pelaaja; |
---|
12 | |
---|
13 | Vector nopeusOikealle = new Vector( 200, 0); |
---|
14 | Vector nopeusVasemmalle = new Vector(-200, 0); |
---|
15 | Vector nopeusYlos = new Vector( 0, 200); |
---|
16 | Vector nopeusAlas = new Vector( 0, -200); |
---|
17 | |
---|
18 | // oletus, min, max |
---|
19 | IntMeter pelaajanHealth = new IntMeter(10, 0, 10); |
---|
20 | |
---|
21 | public override void Begin() |
---|
22 | { |
---|
23 | SetWindowSize(800, 600); |
---|
24 | Level.Size = new Vector(800, 600); |
---|
25 | |
---|
26 | LuoPelaaja(); |
---|
27 | |
---|
28 | // Tehdään muut pallot kenttään: |
---|
29 | LuoUseitaPalloja(50, Color.Beige, "kerattava"); |
---|
30 | |
---|
31 | LuoUseitaPalloja(10, Color.Black, "pahis"); |
---|
32 | |
---|
33 | |
---|
34 | Label pisteNaytto = new Label(); |
---|
35 | pisteNaytto.X = Screen.Left + 100; |
---|
36 | pisteNaytto.Y = Screen.Top - 100; |
---|
37 | pisteNaytto.TextColor = Color.Black; |
---|
38 | pisteNaytto.Color = Color.White; |
---|
39 | |
---|
40 | pisteNaytto.BindTo(pelaajanHealth); |
---|
41 | pisteNaytto.IntFormatString = "Elämää: {0:D1}"; |
---|
42 | Add(pisteNaytto); |
---|
43 | |
---|
44 | |
---|
45 | Level.CreateBorders(); |
---|
46 | LisaaNappaimet(); |
---|
47 | } |
---|
48 | |
---|
49 | void LisaaNappaimet() |
---|
50 | { |
---|
51 | // Poistumisnäppäin |
---|
52 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
53 | // Helppinäppäin: |
---|
54 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
55 | |
---|
56 | // Pelaajan nuolinäppäimet: |
---|
57 | Keyboard.Listen(Key.Right, ButtonState.Down, AsetaNopeus, "Liiku oikealle", pelaaja, nopeusOikealle); |
---|
58 | Keyboard.Listen(Key.Right, ButtonState.Released, AsetaNopeus, "Liiku oikealle", pelaaja, Vector.Zero); |
---|
59 | Keyboard.Listen(Key.Left, ButtonState.Down, AsetaNopeus, "Liiku vasemmalle", pelaaja, nopeusVasemmalle); |
---|
60 | Keyboard.Listen(Key.Left, ButtonState.Released, AsetaNopeus, "Liiku vasemmalle", pelaaja, Vector.Zero); |
---|
61 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Liiku ylös", pelaaja, nopeusYlos); |
---|
62 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, "Liiku ylös", pelaaja, Vector.Zero); |
---|
63 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Liiku alas", pelaaja, nopeusAlas); |
---|
64 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, "Liiku alas", pelaaja, Vector.Zero); |
---|
65 | } |
---|
66 | |
---|
67 | void AsetaNopeus(PhysicsObject hahmo, Vector nopeus) |
---|
68 | { |
---|
69 | hahmo.Velocity = nopeus; |
---|
70 | } |
---|
71 | |
---|
72 | void LuoPelaaja() |
---|
73 | { |
---|
74 | pelaaja = LuoPallo(0.0, 0.0, 30.0, Color.Red); |
---|
75 | Add(pelaaja, 1); |
---|
76 | pelaajanHealth.LowerLimit += PelaajaHaviaa; |
---|
77 | |
---|
78 | AddCollisionHandler(pelaaja, "kerattava", PelaajaOsuuKerattavaan); |
---|
79 | AddCollisionHandler(pelaaja, "pahis", PelaajaOsuuPahikseen); |
---|
80 | } |
---|
81 | |
---|
82 | void PelaajaHaviaa() |
---|
83 | { |
---|
84 | Explosion rajahdys = new Explosion(200); |
---|
85 | rajahdys.Position = pelaaja.Position; |
---|
86 | Add(rajahdys); |
---|
87 | |
---|
88 | pelaaja.Destroy(); |
---|
89 | MessageDisplay.Add("Hävisit!"); |
---|
90 | } |
---|
91 | |
---|
92 | void PelaajaOsuuKerattavaan(PhysicsObject tormaaja, PhysicsObject kohde) |
---|
93 | { |
---|
94 | kohde.Destroy(); |
---|
95 | tormaaja.Size = tormaaja.Size + new Vector(10, 10); |
---|
96 | } |
---|
97 | |
---|
98 | void PelaajaOsuuPahikseen(PhysicsObject tormaaja, PhysicsObject kohde) |
---|
99 | { |
---|
100 | if (tormaaja.Width > 35) |
---|
101 | { |
---|
102 | tormaaja.Size = tormaaja.Size - new Vector(30, 30); |
---|
103 | } |
---|
104 | pelaajanHealth.Value -= 1; |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | void LuoUseitaPalloja(int maara, Color vari, string tagiteksti) |
---|
109 | { |
---|
110 | int i = 0; |
---|
111 | while (i < maara) |
---|
112 | { |
---|
113 | double x = RandomGen.NextDouble(Level.Left, Level.Right); |
---|
114 | double y = RandomGen.NextDouble(Level.Bottom, Level.Top); |
---|
115 | PhysicsObject p = LuoPallo(x, y, 20.0, vari); |
---|
116 | p.Tag = tagiteksti; |
---|
117 | Add(p); |
---|
118 | i++; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | PhysicsObject LuoPallo(double x, double y, double sade, Color vari) |
---|
123 | { |
---|
124 | PhysicsObject pallura = new PhysicsObject(sade * 2.0, sade * 2.0, Shape.Circle); |
---|
125 | pallura.Position = new Vector(x, y); |
---|
126 | pallura.Color = vari; |
---|
127 | pallura.Tag = "pelaaja"; |
---|
128 | return pallura; |
---|
129 | } |
---|
130 | } |
---|