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 | PhysicsObject pallo; |
---|
12 | |
---|
13 | Vector nopeusYlos = new Vector(0, 200); |
---|
14 | Vector nopeusAlas = new Vector(0, -200); |
---|
15 | |
---|
16 | PhysicsObject maila1; |
---|
17 | PhysicsObject maila2; |
---|
18 | |
---|
19 | IntMeter pelaajan1Pisteet; |
---|
20 | IntMeter pelaajan2Pisteet; |
---|
21 | |
---|
22 | PhysicsObject vasenReuna; |
---|
23 | PhysicsObject oikeaReuna; |
---|
24 | PhysicsObject ylaReuna; |
---|
25 | PhysicsObject alaReuna; |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | public override void Begin() |
---|
32 | { |
---|
33 | LuoKenttä(); |
---|
34 | AloitaPeli(); |
---|
35 | AsetaOhjaimet(); |
---|
36 | LisaaLaskurit(); |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | } |
---|
43 | |
---|
44 | void LuoKenttä() |
---|
45 | { |
---|
46 | pallo = new PhysicsObject(50, 50); |
---|
47 | pallo.Shape = Shape.Circle; |
---|
48 | |
---|
49 | Add(pallo); |
---|
50 | |
---|
51 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
52 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
53 | |
---|
54 | MessageDisplay.TextColor = Color.White; |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | pallo.X = 200.0; |
---|
59 | pallo.Y = 0.0; |
---|
60 | pallo.Restitution = 1.2; |
---|
61 | vasenReuna = Level.CreateLeftBorder(); |
---|
62 | vasenReuna.Restitution = 1.0; |
---|
63 | vasenReuna.IsVisible = false; |
---|
64 | |
---|
65 | oikeaReuna = Level.CreateRightBorder(); |
---|
66 | oikeaReuna.Restitution = 1.0; |
---|
67 | oikeaReuna.IsVisible = false; |
---|
68 | |
---|
69 | ylaReuna = Level.CreateTopBorder(); |
---|
70 | ylaReuna.Restitution = 1.0; |
---|
71 | ylaReuna.IsVisible = false; |
---|
72 | alaReuna = Level.CreateBottomBorder(); |
---|
73 | alaReuna.Restitution = 1.0; |
---|
74 | alaReuna.IsVisible = false; |
---|
75 | |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | Level.BackgroundColor = Color.Black; |
---|
80 | Camera.ZoomToLevel(); |
---|
81 | |
---|
82 | AddCollisionHandler(pallo, KasittelePallonTormays); |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | } |
---|
87 | |
---|
88 | void KasittelePallonTormays( PhysicsObject pallo, PhysicsObject kohde ) |
---|
89 | { |
---|
90 | if (kohde == oikeaReuna) |
---|
91 | { |
---|
92 | pelaajan1Pisteet.Value += 1; |
---|
93 | } |
---|
94 | else if (kohde == vasenReuna) |
---|
95 | { |
---|
96 | pelaajan2Pisteet.Value += 1; |
---|
97 | } |
---|
98 | |
---|
99 | } |
---|
100 | |
---|
101 | void AloitaPeli() |
---|
102 | { |
---|
103 | Vector impulssi = new Vector(500.0, 0.0); |
---|
104 | pallo.Hit(impulssi); |
---|
105 | } |
---|
106 | |
---|
107 | PhysicsObject LuoMaila( double x, double y ) |
---|
108 | { |
---|
109 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
110 | maila.Shape = Shape.Rectangle; |
---|
111 | maila.X = x; |
---|
112 | maila.Y = y; |
---|
113 | maila.Restitution = 1.0; |
---|
114 | Add(maila); |
---|
115 | |
---|
116 | return maila; |
---|
117 | } |
---|
118 | |
---|
119 | void AsetaOhjaimet() |
---|
120 | { |
---|
121 | Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); |
---|
122 | Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
123 | Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); |
---|
124 | Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
125 | Keyboard.Listen(Key.Space, ButtonState.Down, Haxaa, null); |
---|
126 | Keyboard.Listen(Key.LeftAlt, ButtonState.Pressed, Haxaa2, null); |
---|
127 | |
---|
128 | |
---|
129 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); |
---|
130 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
131 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); |
---|
132 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
133 | |
---|
134 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
135 | |
---|
136 | Keyboard.Listen(Key.R, ButtonState.Pressed, AloitaAlusta, "Aloita alusta"); |
---|
137 | |
---|
138 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
139 | |
---|
140 | |
---|
141 | ControllerOne.Listen(Button.DPadUp, ButtonState.Down, AsetaNopeus, "Liikuta mailaa ylös", maila1, nopeusYlos); |
---|
142 | ControllerOne.Listen(Button.DPadUp, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
143 | ControllerOne.Listen(Button.DPadDown, ButtonState.Down, AsetaNopeus, "Liikuta mailaa alas", maila1, nopeusAlas); |
---|
144 | ControllerOne.Listen(Button.DPadDown, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
145 | |
---|
146 | ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu"); |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | void Haxaa() |
---|
157 | { |
---|
158 | Vector impulssi = new Vector(50.0, 0.0); |
---|
159 | pallo.Hit(impulssi); |
---|
160 | } |
---|
161 | |
---|
162 | void Haxaa2() |
---|
163 | { |
---|
164 | maila2.Destroy(); |
---|
165 | |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | void AloitaAlusta() |
---|
171 | { |
---|
172 | pallo.Destroy(); |
---|
173 | pelaajan1Pisteet.Value = 0; |
---|
174 | pelaajan2Pisteet.Value = 0; |
---|
175 | maila1.Destroy(); |
---|
176 | maila2.Destroy(); |
---|
177 | LuoKenttä(); |
---|
178 | AloitaPeli(); |
---|
179 | AsetaOhjaimet(); |
---|
180 | ylaReuna.Destroy(); |
---|
181 | alaReuna.Destroy(); |
---|
182 | |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | void AsetaNopeus( PhysicsObject maila, Vector nopeus ) |
---|
187 | { |
---|
188 | |
---|
189 | if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) |
---|
190 | { |
---|
191 | maila.Velocity = Vector.Zero; |
---|
192 | return; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
197 | { |
---|
198 | maila.Velocity = Vector.Zero; |
---|
199 | return; |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | maila.Velocity = nopeus; |
---|
208 | } |
---|
209 | |
---|
210 | void LisaaLaskurit() |
---|
211 | { |
---|
212 | pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
213 | pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
214 | |
---|
215 | } |
---|
216 | |
---|
217 | IntMeter LuoPisteLaskuri( double x, double y ) |
---|
218 | { |
---|
219 | IntMeter laskuri = new IntMeter(0); |
---|
220 | laskuri.MaxValue = 10; |
---|
221 | Label naytto = new Label(); |
---|
222 | naytto.BindTo(laskuri); |
---|
223 | naytto.X = x; |
---|
224 | naytto.Y = y; |
---|
225 | naytto.TextColor = Color.White; |
---|
226 | naytto.BorderColor = Level.BackgroundColor; |
---|
227 | naytto.Color = Level.BackgroundColor; |
---|
228 | Add(naytto); |
---|
229 | return laskuri; |
---|
230 | |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | } |
---|
246 | |
---|