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 Pong : PhysicsGame |
---|
10 | { |
---|
11 | Vector nopeusYlos = new Vector(0, 200); |
---|
12 | Vector nopeusAlas = new Vector(0, -200); |
---|
13 | PhysicsObject pallo; |
---|
14 | PhysicsObject maila1; |
---|
15 | PhysicsObject maila2; |
---|
16 | public override void Begin() |
---|
17 | { |
---|
18 | LuoKentta(); |
---|
19 | AsetaOhjaimet(); |
---|
20 | LisaaLaskurit(); |
---|
21 | AloitaPeli(); |
---|
22 | |
---|
23 | } |
---|
24 | void LuoKentta() |
---|
25 | { |
---|
26 | pallo = new PhysicsObject(40.0, 40.0); |
---|
27 | pallo.X = -300.0; |
---|
28 | pallo.Y = 50.0; |
---|
29 | pallo.Restitution = 1.0; |
---|
30 | pallo.Shape = Shape.Circle; |
---|
31 | Add(pallo); |
---|
32 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
33 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
34 | Level.CreateBorders(1.0, false); |
---|
35 | Level.Background.Color = Color.Red; |
---|
36 | Camera.ZoomToLevel(); |
---|
37 | } |
---|
38 | void AloitaPeli() |
---|
39 | { |
---|
40 | Vector impulssi = new Vector(600.0, 0.0); |
---|
41 | pallo.Hit(impulssi); |
---|
42 | } |
---|
43 | PhysicsObject LuoMaila(double x, double y) |
---|
44 | { |
---|
45 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
46 | maila.Shape = Shape.Rectangle; |
---|
47 | maila.X = x; |
---|
48 | maila.Y = y; |
---|
49 | maila.Restitution = 1.0; |
---|
50 | Add(maila); |
---|
51 | return maila; |
---|
52 | } |
---|
53 | void AsetaOhjaimet() |
---|
54 | { |
---|
55 | Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); |
---|
56 | Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
57 | Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); |
---|
58 | Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
59 | |
---|
60 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); |
---|
61 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
62 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); |
---|
63 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
64 | |
---|
65 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
66 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
67 | } |
---|
68 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
69 | { |
---|
70 | if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom )) |
---|
71 | { |
---|
72 | maila.Velocity = Vector.Zero; |
---|
73 | return; |
---|
74 | |
---|
75 | } |
---|
76 | if (( nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
77 | maila.Velocity = nopeus; |
---|
78 | } |
---|
79 | void LisaaLaskurit() |
---|
80 | { |
---|
81 | // ... |
---|
82 | } |
---|
83 | } |
---|