1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.ScreenObjects; |
---|
4 | using Jypeli.Assets; |
---|
5 | |
---|
6 | namespace Pong |
---|
7 | { |
---|
8 | public class Peli : PhysicsGame |
---|
9 | { |
---|
10 | PhysicsObject pallo; |
---|
11 | PhysicsObject maila1; |
---|
12 | PhysicsObject maila2; |
---|
13 | |
---|
14 | DoubleMeter P1P; |
---|
15 | DoubleMeter P2P; |
---|
16 | |
---|
17 | protected override void Begin() |
---|
18 | { |
---|
19 | LuoKentta(); |
---|
20 | AsetaOhjaimet(); |
---|
21 | LisaaLaskurit(); |
---|
22 | AloitaPeli(); |
---|
23 | } |
---|
24 | |
---|
25 | void LuoKentta() |
---|
26 | { |
---|
27 | pallo = new PhysicsObject(40.0, 40.0); |
---|
28 | pallo.Shape = Shapes.Circle; |
---|
29 | Add(pallo); |
---|
30 | pallo.X = -200.0; |
---|
31 | pallo.Y = 0.0; |
---|
32 | pallo.Restitution = 1.0; |
---|
33 | |
---|
34 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
35 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
36 | |
---|
37 | Level.CreateBorders(1.0, false); |
---|
38 | Level.BackgroundColor = Color.Green; |
---|
39 | |
---|
40 | Camera.ZoomToLevel(); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | void AloitaPeli() |
---|
45 | { |
---|
46 | Vector impulssi = new Vector(500.0, 250.0); |
---|
47 | pallo.Hit(impulssi); |
---|
48 | } |
---|
49 | |
---|
50 | PhysicsObject LuoMaila(double x, double y) |
---|
51 | { |
---|
52 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
53 | maila.Shape = Shapes.Rectangle; |
---|
54 | maila.X = x; |
---|
55 | maila.Y = y; |
---|
56 | maila.Restitution = 1.0; |
---|
57 | Add(maila); |
---|
58 | |
---|
59 | return maila; |
---|
60 | } |
---|
61 | |
---|
62 | void AsetaOhjaimet() |
---|
63 | { |
---|
64 | Keyboard.Listen(Key.A, ButtonState.Down, LiikutaMailaaYlos, "Pelaaja 1: Liikuta mailaa ylös", maila1); |
---|
65 | Keyboard.Listen(Key.A, ButtonState.Released, PysaytaMaila, null, maila1); |
---|
66 | Keyboard.Listen(Key.D, ButtonState.Down, LiikutaMailaaAlas, "Pelaaja 1: Liikuta mailaa alas", maila1); |
---|
67 | Keyboard.Listen(Key.D, ButtonState.Released, PysaytaMaila, null, maila1); |
---|
68 | |
---|
69 | Keyboard.Listen(Key.Left, ButtonState.Down, LiikutaMailaaYlos, "Pelaaja 2: Liikuta mailaa ylös", maila2); |
---|
70 | Keyboard.Listen(Key.Left, ButtonState.Released, PysaytaMaila, null, maila2); |
---|
71 | Keyboard.Listen(Key.Right, ButtonState.Down, LiikutaMailaaAlas, "Pelaaja 2: Liikuta mailaa alas", maila2); |
---|
72 | Keyboard.Listen(Key.Right, ButtonState.Released, PysaytaMaila, null, maila2); |
---|
73 | |
---|
74 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
75 | } |
---|
76 | |
---|
77 | void LiikutaMailaaYlos(PhysicsObject maila) |
---|
78 | { |
---|
79 | if (maila.Top >= Level.Top) |
---|
80 | { |
---|
81 | maila.Velocity = Vector.Zero; |
---|
82 | return; |
---|
83 | } |
---|
84 | |
---|
85 | Vector nopeusy = new Vector(0.0, 200.0); |
---|
86 | maila.Velocity = nopeusy; |
---|
87 | |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | void PysaytaMaila(PhysicsObject maila) |
---|
92 | { |
---|
93 | maila.Velocity = Vector.Zero; |
---|
94 | } |
---|
95 | |
---|
96 | void LiikutaMailaaAlas(PhysicsObject maila) |
---|
97 | { |
---|
98 | |
---|
99 | if (maila.Bottom <= Level.Bottom) |
---|
100 | { |
---|
101 | maila.Velocity = Vector.Zero; |
---|
102 | return; |
---|
103 | } |
---|
104 | |
---|
105 | Vector nopeusa = new Vector(0.0, -200.0); |
---|
106 | maila.Velocity = nopeusa; |
---|
107 | } |
---|
108 | |
---|
109 | DoubleMeter LuoPisteLaskuri(double x, double y) |
---|
110 | { |
---|
111 | DoubleMeter laskuri = new DoubleMeter(0.0); |
---|
112 | laskuri.MaxValue = 10.0; |
---|
113 | ValueDisplay naytto = new ValueDisplay(); |
---|
114 | naytto.BindTo(laskuri); |
---|
115 | naytto.X = x; |
---|
116 | naytto.Y = y; |
---|
117 | naytto.ValueColor = Color.White; |
---|
118 | Add(naytto); |
---|
119 | return laskuri; |
---|
120 | } |
---|
121 | |
---|
122 | void LisaaLaskurit() |
---|
123 | { |
---|
124 | P1P = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
125 | P2P = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|