1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.ScreenObjects; |
---|
4 | using Jypeli.Assets; |
---|
5 | |
---|
6 | |
---|
7 | class Tasohyppely : PhysicsGame |
---|
8 | { const int ruudunLeveys = 50; |
---|
9 | const int ruudunKorkeus = 50; |
---|
10 | |
---|
11 | |
---|
12 | const double nopeus = 400; |
---|
13 | const double hyppyVoima = 20000; |
---|
14 | |
---|
15 | PlatformCharacter pelaaja2; |
---|
16 | PlatformCharacter pelaaja1; |
---|
17 | |
---|
18 | IntMeter pistelaskuri1; |
---|
19 | IntMeter pistelaskuri2; |
---|
20 | IntMeter pelaaja1hp; |
---|
21 | IntMeter pelaaja2hp; |
---|
22 | |
---|
23 | int kenttaNro; // monesko kenttä on menossa |
---|
24 | |
---|
25 | |
---|
26 | protected override void Begin() |
---|
27 | { |
---|
28 | |
---|
29 | luoKentta(0, 0); |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | kenttaNro = 1; |
---|
35 | Level.Width = 1820; |
---|
36 | Level.Height = 1125; |
---|
37 | |
---|
38 | //Luodaan pistelaskuri; |
---|
39 | |
---|
40 | |
---|
41 | //luodaan pistelaskunäyttö; |
---|
42 | |
---|
43 | |
---|
44 | // Zoomataan lähemmäksi |
---|
45 | Camera.ZoomFactor = 0.8; |
---|
46 | |
---|
47 | Camera.StayInLevel = false; |
---|
48 | |
---|
49 | |
---|
50 | seuraavaKentta(); |
---|
51 | |
---|
52 | |
---|
53 | } |
---|
54 | void pelaaja2alarajassa(int arvo) |
---|
55 | { |
---|
56 | pelaaja2.Destroy(); |
---|
57 | pistelaskuri1.Value++; |
---|
58 | if (pistelaskuri1.Value == 5) |
---|
59 | { |
---|
60 | Timer ajastin = new Timer(); |
---|
61 | ajastin.Interval = 3; |
---|
62 | ajastin.Trigger += peliloppu; |
---|
63 | Add(ajastin); |
---|
64 | ajastin.Start(1); |
---|
65 | |
---|
66 | MessageDisplay.Add("pelaaja1 voitti"); |
---|
67 | } |
---|
68 | else |
---|
69 | { |
---|
70 | seuraavaKentta(); |
---|
71 | } |
---|
72 | } |
---|
73 | void pelaaja1alarajassa(int arvo) |
---|
74 | { |
---|
75 | pelaaja1.Destroy(); |
---|
76 | pistelaskuri2.Value++; |
---|
77 | |
---|
78 | if (pistelaskuri2.Value == 5) |
---|
79 | { |
---|
80 | Timer ajastin = new Timer(); |
---|
81 | ajastin.Interval = 3; |
---|
82 | ajastin.Trigger += peliloppu; |
---|
83 | Add(ajastin); |
---|
84 | ajastin.Start(1); |
---|
85 | |
---|
86 | MessageDisplay.Add("pelaaja2 voitti"); |
---|
87 | } |
---|
88 | else |
---|
89 | { |
---|
90 | seuraavaKentta(); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | void luoKentta(int edellisenPisteet1, int edellisenPisteet2) |
---|
95 | { |
---|
96 | Level.CreateBorders(); |
---|
97 | Level.BackgroundColor = Color.Green; |
---|
98 | |
---|
99 | Gravity = new Vector(0, -2000); |
---|
100 | |
---|
101 | for (int i = 0; i < 200; i = i + 9) |
---|
102 | { |
---|
103 | |
---|
104 | lisaataso(RandomGen.NextInt(-900, 900), RandomGen.NextInt(-550, 550)); |
---|
105 | |
---|
106 | |
---|
107 | } |
---|
108 | |
---|
109 | lisaaPelaajat(); |
---|
110 | lisaaNappaimet(); |
---|
111 | |
---|
112 | pelaaja1hp = new IntMeter(10); |
---|
113 | pelaaja1hp.MinValue = 0; |
---|
114 | pelaaja1hp.LowerLimit += pelaaja1alarajassa; |
---|
115 | |
---|
116 | pelaaja2hp = new IntMeter(10); |
---|
117 | pelaaja2hp.MinValue = 0; |
---|
118 | pelaaja2hp.LowerLimit += pelaaja2alarajassa; |
---|
119 | |
---|
120 | ValueDisplay hpNaytto1 = new ValueDisplay(); |
---|
121 | hpNaytto1.Text = "Pelaaja1 HP: "; |
---|
122 | |
---|
123 | hpNaytto1.X = Screen.Left + 100; |
---|
124 | hpNaytto1.Y = Screen.Top - 100; |
---|
125 | |
---|
126 | hpNaytto1.ValueColor = Color.Red; |
---|
127 | hpNaytto1.TextColor = Color.Red; |
---|
128 | |
---|
129 | hpNaytto1.BindTo(pelaaja1hp); |
---|
130 | Add(hpNaytto1); |
---|
131 | |
---|
132 | ValueDisplay hpNaytto2 = new ValueDisplay(); |
---|
133 | hpNaytto2.Text = "Pelaaja2 HP: "; |
---|
134 | |
---|
135 | hpNaytto2.X = Screen.Right - 100; |
---|
136 | hpNaytto2.Y = Screen.Top - 100; |
---|
137 | |
---|
138 | hpNaytto2.ValueColor = Color.Red; |
---|
139 | hpNaytto2.TextColor = Color.Red; |
---|
140 | |
---|
141 | hpNaytto2.BindTo(pelaaja2hp); |
---|
142 | Add(hpNaytto2); |
---|
143 | |
---|
144 | pistelaskuri1 = new IntMeter(0); |
---|
145 | pistelaskuri1.Value = edellisenPisteet1; |
---|
146 | |
---|
147 | ValueDisplay pisteNaytto1 = new ValueDisplay(); |
---|
148 | pisteNaytto1.Text = "Pisteitä: "; |
---|
149 | pisteNaytto1.X = Screen.Left + 500; |
---|
150 | pisteNaytto1.Y = Screen.Top - 100; |
---|
151 | pisteNaytto1.ValueColor = Color.Red; |
---|
152 | pisteNaytto1.TextColor = Color.Red; |
---|
153 | pisteNaytto1.BindTo(pistelaskuri1); |
---|
154 | Add(pisteNaytto1); |
---|
155 | //pistelaskuri1.MaxValue = 1; |
---|
156 | //pistelaskuri1.UpperLimit += pelaaja1Voittaa; |
---|
157 | |
---|
158 | pistelaskuri2 = new IntMeter(0); |
---|
159 | pistelaskuri2.Value = edellisenPisteet2; |
---|
160 | ValueDisplay pisteNaytto2 = new ValueDisplay(); |
---|
161 | pisteNaytto2.Text = "Pisteitä: "; |
---|
162 | pisteNaytto2.X = Screen.Right - 500; |
---|
163 | pisteNaytto2.Y = Screen.Top - 100; |
---|
164 | pisteNaytto2.ValueColor = Color.Red; |
---|
165 | pisteNaytto2.TextColor = Color.Red; |
---|
166 | pisteNaytto2.BindTo(pistelaskuri2); |
---|
167 | Add(pisteNaytto2); |
---|
168 | //pistelaskuri2.MaxValue = 1; |
---|
169 | //pistelaskuri2.UpperLimit += pelaaja2Voittaa; |
---|
170 | |
---|
171 | } |
---|
172 | |
---|
173 | void pelaaja1Voittaa(int value) |
---|
174 | { |
---|
175 | |
---|
176 | |
---|
177 | } |
---|
178 | void pelaaja2Voittaa(int value) |
---|
179 | { |
---|
180 | |
---|
181 | } |
---|
182 | void peliloppu(Timer sender) |
---|
183 | { |
---|
184 | Exit(); |
---|
185 | } |
---|
186 | void seuraavaKentta() |
---|
187 | { |
---|
188 | int edellisenKentanPisteet1 = pistelaskuri1.Value; |
---|
189 | int edellisenKentanPisteet2 = pistelaskuri2.Value; |
---|
190 | |
---|
191 | ClearAll(); |
---|
192 | //pisteLaskuri.Reset(); |
---|
193 | |
---|
194 | kenttaNro += 1; // lisätään kenttänumeroa yhdellä |
---|
195 | MessageDisplay.Add("Kenttä " + kenttaNro); |
---|
196 | |
---|
197 | // Asetetaan painovoima |
---|
198 | |
---|
199 | |
---|
200 | luoKentta(edellisenKentanPisteet1, edellisenKentanPisteet2); |
---|
201 | |
---|
202 | |
---|
203 | } |
---|
204 | |
---|
205 | void lisaataso(double x, double y) |
---|
206 | { |
---|
207 | PhysicsObject taso = PhysicsObject.CreateStaticObject(100, 30); |
---|
208 | taso.Image = LoadImage("battle"); |
---|
209 | taso.X = x; |
---|
210 | taso.Y = y; |
---|
211 | Add(taso); |
---|
212 | } |
---|
213 | |
---|
214 | void lisaaPelaajat() |
---|
215 | { |
---|
216 | pelaaja2 = new PlatformCharacter(60, 80); |
---|
217 | pelaaja2.Mass = 10.0; |
---|
218 | pelaaja2.Image = LoadImage("untitled"); |
---|
219 | AddCollisionHandler(pelaaja2, osuiMaaliin); |
---|
220 | Add(pelaaja2); |
---|
221 | |
---|
222 | |
---|
223 | PlasmaCannon pyssy = new PlasmaCannon(100, 40); |
---|
224 | pelaaja2.Weapon = pyssy; |
---|
225 | |
---|
226 | pyssy.PlasmaParticleCollision = PalloLyo; |
---|
227 | |
---|
228 | |
---|
229 | pelaaja1 = new PlatformCharacter(60, 80); |
---|
230 | pelaaja1.Mass = 10.0; |
---|
231 | pelaaja1.Image = LoadImage("Untitled 23"); |
---|
232 | pelaaja1.X = 0; |
---|
233 | pelaaja1.Y = Level.Bottom + 120; |
---|
234 | PlasmaCannon tykki = new PlasmaCannon(100, 40); |
---|
235 | pelaaja1.Weapon = tykki; |
---|
236 | Add(pelaaja1); |
---|
237 | AddCollisionHandler(pelaaja1, osuiMaaliin); |
---|
238 | tykki.PlasmaParticleCollision = PalloOsuu; |
---|
239 | } |
---|
240 | |
---|
241 | void PalloOsuu(PhysicsObject tankki, PhysicsObject toinen) |
---|
242 | { |
---|
243 | |
---|
244 | if (toinen != pelaaja1){ |
---|
245 | |
---|
246 | tankki.Destroy(); |
---|
247 | Explosion rajahdys = new Explosion(100); |
---|
248 | rajahdys.Position = tankki.Position; |
---|
249 | |
---|
250 | Add(rajahdys); |
---|
251 | |
---|
252 | if (toinen == pelaaja2) |
---|
253 | { |
---|
254 | pelaaja2hp.Value--; |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | void PalloLyo(PhysicsObject plasmapallo, PhysicsObject toinen) |
---|
260 | { |
---|
261 | if (toinen != pelaaja2) |
---|
262 | { |
---|
263 | plasmapallo.Destroy(); |
---|
264 | Explosion rajahdys = new Explosion(100); |
---|
265 | rajahdys.Position = plasmapallo.Position; |
---|
266 | |
---|
267 | Add(rajahdys); |
---|
268 | |
---|
269 | if (toinen == pelaaja1) |
---|
270 | { |
---|
271 | pelaaja1hp.Value--; |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | } |
---|
276 | |
---|
277 | } |
---|
278 | void lisaaMaali(double x, double y) |
---|
279 | { |
---|
280 | PhysicsObject maali = PhysicsObject.CreateStaticObject(50, 50, Shapes.Circle); |
---|
281 | maali.Tag = "maali"; |
---|
282 | maali.IgnoresCollisionResponse = true; |
---|
283 | maali.Image = LoadImage("tahti"); |
---|
284 | maali.Position = new Vector(x, y); |
---|
285 | Add(maali); |
---|
286 | |
---|
287 | } |
---|
288 | |
---|
289 | void lisaaNappaimet() |
---|
290 | { |
---|
291 | |
---|
292 | |
---|
293 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
294 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu pelistä"); |
---|
295 | |
---|
296 | Keyboard.Listen(Key.Left, ButtonState.Down, liikuta, "Liikkuu vasemmalle", pelaaja1, -nopeus); |
---|
297 | Keyboard.Listen(Key.Right, ButtonState.Down, liikuta, "Liikkuu oikealle", pelaaja1, nopeus); |
---|
298 | Keyboard.Listen(Key.Up, ButtonState.Pressed, hyppaa, "Hyppää", pelaaja1, hyppyVoima); |
---|
299 | |
---|
300 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
301 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu pelistä"); |
---|
302 | |
---|
303 | |
---|
304 | Keyboard.Listen(Key.A, ButtonState.Down, liikuta, "Liikkuu vasemmalle", pelaaja2, -nopeus); |
---|
305 | Keyboard.Listen(Key.D, ButtonState.Down, liikuta, "Liikkuu oikealle", pelaaja2, nopeus); |
---|
306 | Keyboard.Listen(Key.W, ButtonState.Pressed, hyppaa, "Hyppää", pelaaja2, hyppyVoima); |
---|
307 | |
---|
308 | Keyboard.Listen(Key.Space, ButtonState.Down, pelaaja2.Weapon.Use, "Ammu"); |
---|
309 | |
---|
310 | Keyboard.Listen(Key.Enter, ButtonState.Down, pelaaja1.Weapon.Use, "Ammu"); |
---|
311 | |
---|
312 | |
---|
313 | |
---|
314 | |
---|
315 | lisaaGamePadNappaimet(ControllerOne); |
---|
316 | } |
---|
317 | |
---|
318 | void lisaaGamePadNappaimet(GamePad controller) |
---|
319 | { |
---|
320 | controller.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä"); |
---|
321 | |
---|
322 | controller.Listen(Button.DPadLeft, ButtonState.Down, liikuta, "Pelaaja liikkuu vasemmalle", pelaaja1, -nopeus); |
---|
323 | controller.Listen(Button.DPadRight, ButtonState.Down, liikuta, "Pelaaja liikkuu oikealle", pelaaja1, nopeus); |
---|
324 | controller.Listen(Button.A, ButtonState.Pressed, hyppaa, "Pelaaja hyppää", pelaaja1, hyppyVoima); |
---|
325 | } |
---|
326 | |
---|
327 | void liikuta(PlatformCharacter hahmo, double nopeus) |
---|
328 | { |
---|
329 | hahmo.Walk(nopeus); |
---|
330 | } |
---|
331 | |
---|
332 | void hyppaa(PlatformCharacter hahmo, double voima) |
---|
333 | { |
---|
334 | hahmo.Jump(voima); |
---|
335 | } |
---|
336 | |
---|
337 | void osuiMaaliin(PhysicsObject collidingObject, PhysicsObject otherObject) |
---|
338 | { |
---|
339 | if (otherObject.Tag.ToString() == "maali") |
---|
340 | { |
---|
341 | this.PlaySound("maali"); |
---|
342 | seuraavaKentta(); |
---|
343 | //MessageDisplay.Add("Pääsit läpi kentän " + kenttaNro + ". Pisteitä: " + edellisenKentanPisteet); |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | } |
---|
348 | |
---|