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 Peli : PhysicsGame |
---|
10 | { |
---|
11 | List<Label> ValikonKohdat; |
---|
12 | |
---|
13 | Vector nopeusYlos = new Vector(0.0, 400.0); |
---|
14 | Vector nopeusAlas = new Vector(0.0, -400.0); |
---|
15 | |
---|
16 | PhysicsObject pallo; |
---|
17 | |
---|
18 | PhysicsObject maila1; |
---|
19 | PhysicsObject maila2; |
---|
20 | |
---|
21 | IntMeter pelaaja1pisteet; |
---|
22 | IntMeter pelaaja2pisteet; |
---|
23 | |
---|
24 | PhysicsObject vasenReuna; |
---|
25 | PhysicsObject oikeaReuna; |
---|
26 | |
---|
27 | public override void Begin() |
---|
28 | { |
---|
29 | Valikko(); |
---|
30 | } |
---|
31 | |
---|
32 | void Valikko() |
---|
33 | { |
---|
34 | ClearAll(); |
---|
35 | Level.BackgroundColor = Color.DarkGreen; |
---|
36 | ValikonKohdat = new List<Label>(); |
---|
37 | |
---|
38 | Label kohta1 = new Label("Aloita uus peli"); |
---|
39 | kohta1.Position = new Vector(0, 50); |
---|
40 | ValikonKohdat.Add(kohta1); |
---|
41 | |
---|
42 | Label kohta2 = new Label("Lopeta"); |
---|
43 | kohta2.Position = new Vector(0, -30); |
---|
44 | ValikonKohdat.Add(kohta2); |
---|
45 | |
---|
46 | foreach (Label Valikonkohta in ValikonKohdat) |
---|
47 | Add(Valikonkohta); |
---|
48 | |
---|
49 | Mouse.ListenOn(kohta1, MouseButton.Left, ButtonState.Pressed, Aloota, null); |
---|
50 | Mouse.ListenOn(kohta2, MouseButton.Left, ButtonState.Pressed, Lopeta, null); |
---|
51 | |
---|
52 | Mouse.IsCursorVisible = true; |
---|
53 | Mouse.ListenMovement(1.0, ValikossaLiikkuminen, null); |
---|
54 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Lopeta, ""); |
---|
55 | } |
---|
56 | |
---|
57 | void ValikossaLiikkuminen(AnalogState hiirenTila) |
---|
58 | { |
---|
59 | foreach (Label kohta in ValikonKohdat) |
---|
60 | { |
---|
61 | if (Mouse.IsCursorOn(kohta)) |
---|
62 | { |
---|
63 | kohta.TextColor = Color.YellowGreen; |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | kohta.TextColor = Color.Wheat; |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | void Aloota() |
---|
73 | { |
---|
74 | ClearAll(); |
---|
75 | LuoKentta(); |
---|
76 | LisaaLaskurit(); |
---|
77 | AsetaOhjaimet(); |
---|
78 | AloitaPeli(); |
---|
79 | } |
---|
80 | |
---|
81 | void Lopeta() |
---|
82 | { |
---|
83 | Exit(); |
---|
84 | } |
---|
85 | |
---|
86 | void LisaaLaskurit() |
---|
87 | { |
---|
88 | pelaaja1pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
89 | pelaaja2pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
90 | } |
---|
91 | |
---|
92 | IntMeter LuoPisteLaskuri(double x, double y) |
---|
93 | { |
---|
94 | IntMeter laskuri = new IntMeter(0); |
---|
95 | laskuri.MaxValue = 10; |
---|
96 | Label naytto = new Label(); |
---|
97 | naytto.BindTo(laskuri); |
---|
98 | naytto.Y = y; |
---|
99 | naytto.X = x; |
---|
100 | naytto.TextColor = Color.White; |
---|
101 | naytto.BorderColor = Level.BackgroundColor; |
---|
102 | naytto.Color = Level.BackgroundColor; |
---|
103 | Add (naytto); |
---|
104 | return laskuri; |
---|
105 | } |
---|
106 | |
---|
107 | void LuoKentta() |
---|
108 | { |
---|
109 | pallo = new PhysicsObject(50.0,60.0); |
---|
110 | pallo.Shape = Shape.Circle; |
---|
111 | pallo.Color = Color.Gray; |
---|
112 | pallo.X = 200.0; |
---|
113 | pallo.Y = 350.0; |
---|
114 | pallo.Restitution = 1.0; |
---|
115 | Add(pallo); |
---|
116 | |
---|
117 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
118 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
119 | |
---|
120 | vasenReuna = Level.CreateLeftBorder(); |
---|
121 | vasenReuna.Restitution = 1.0; |
---|
122 | vasenReuna.IsVisible = false; |
---|
123 | oikeaReuna = Level.CreateRightBorder(); |
---|
124 | oikeaReuna.Restitution = 1.0; |
---|
125 | oikeaReuna.IsVisible = false; |
---|
126 | PhysicsObject ylaReuna = Level.CreateTopBorder(); |
---|
127 | ylaReuna.Restitution = 1.0; |
---|
128 | ylaReuna.IsVisible = false; |
---|
129 | PhysicsObject alaReuna = Level.CreateBottomBorder(); |
---|
130 | alaReuna.Restitution = 1.0; |
---|
131 | alaReuna.IsVisible = false; |
---|
132 | |
---|
133 | AddCollisionHandler(pallo, KasittelePallonTormays); |
---|
134 | |
---|
135 | Camera.ZoomToLevel(); |
---|
136 | } |
---|
137 | |
---|
138 | void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) |
---|
139 | { |
---|
140 | if (kohde == oikeaReuna) |
---|
141 | { |
---|
142 | pelaaja1pisteet.Value += 1; |
---|
143 | } |
---|
144 | else if (kohde == vasenReuna) |
---|
145 | { |
---|
146 | pelaaja2pisteet.Value += 1; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | void AloitaPeli() |
---|
151 | { |
---|
152 | Vector impulssi = new Vector(-200.0, -200.0); |
---|
153 | pallo.Hit(impulssi); |
---|
154 | } |
---|
155 | |
---|
156 | PhysicsObject LuoMaila(double x, double y) |
---|
157 | { |
---|
158 | PhysicsObject maila = PhysicsObject.CreateStaticObject(25.0, 150.0); |
---|
159 | maila.Shape = Shape.Rectangle; |
---|
160 | maila.Color = Color.Black; |
---|
161 | maila.X = x; |
---|
162 | maila.Y = y; |
---|
163 | maila.Restitution = 1.0; |
---|
164 | Add(maila); |
---|
165 | return maila; |
---|
166 | } |
---|
167 | |
---|
168 | void AsetaOhjaimet() |
---|
169 | { |
---|
170 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Valikko, "Valikkoon"); |
---|
171 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
172 | Keyboard.Listen(Key.Space, ButtonState.Pressed, Release, "Liikuta palloa"); |
---|
173 | |
---|
174 | Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "1 ylös", maila1, nopeusYlos); |
---|
175 | Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
176 | Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "1 alas", maila1, nopeusAlas); |
---|
177 | Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
178 | |
---|
179 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "2 ylös", maila2, nopeusYlos); |
---|
180 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
181 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "2 alas", maila2, nopeusAlas); |
---|
182 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
183 | } |
---|
184 | |
---|
185 | void Release() |
---|
186 | { |
---|
187 | pallo.Velocity = new Vector(400, 150); |
---|
188 | } |
---|
189 | |
---|
190 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
191 | { |
---|
192 | if( (nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
193 | { |
---|
194 | maila.Velocity = Vector.Zero; |
---|
195 | return; |
---|
196 | } |
---|
197 | |
---|
198 | if( (nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) |
---|
199 | { |
---|
200 | maila.Velocity = Vector.Zero; |
---|
201 | return; |
---|
202 | } |
---|
203 | maila.Velocity = nopeus; |
---|
204 | } |
---|
205 | } |
---|