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, 540); |
---|
12 | Vector nopeusAlas = new Vector(0, -540); |
---|
13 | Vector nopeusVasen = new Vector(-800, 0); |
---|
14 | Vector nopeusOikea = new Vector(800, 0); |
---|
15 | |
---|
16 | PhysicsObject ball; |
---|
17 | |
---|
18 | PhysicsObject maila1; |
---|
19 | PhysicsObject maila2; |
---|
20 | PhysicsObject maila3; |
---|
21 | PhysicsObject maila4; |
---|
22 | |
---|
23 | IntMeter pelaajan1pisteet; |
---|
24 | IntMeter pelaajan2pisteet; |
---|
25 | |
---|
26 | PhysicsObject vasenReuna; |
---|
27 | PhysicsObject oikeaReuna; |
---|
28 | PhysicsObject ylaReuna; |
---|
29 | PhysicsObject alaReuna; |
---|
30 | |
---|
31 | public override void Begin() |
---|
32 | { |
---|
33 | LuoKentta(); |
---|
34 | AloitaPeli(); |
---|
35 | AsetaOhjaimet(); |
---|
36 | LisaaLaskurit(); |
---|
37 | } |
---|
38 | |
---|
39 | void LuoKentta() |
---|
40 | { |
---|
41 | Level.BackgroundColor = Color.Black; |
---|
42 | |
---|
43 | ball = new PhysicsObject(20.0, 20.0); |
---|
44 | ball.Shape = Shape.Circle; |
---|
45 | ball.Color = Color.White; |
---|
46 | ball.X = -200; |
---|
47 | ball.Y = -200; |
---|
48 | ball.Restitution = 1.0; |
---|
49 | ball.KineticFriction = 0.0; |
---|
50 | ball.CanRotate = true; |
---|
51 | Add(ball); |
---|
52 | |
---|
53 | maila1 = LuoMaila ( Level.Left + 20.0, 0.0, 20.0, 100.0); |
---|
54 | maila2 = LuoMaila ( Level.Right - 20.0, 0.0, 20.0, 100.0 ); |
---|
55 | maila3 = LuoMaila ( 0.0, Level.Bottom + 20.0, 100, 20); |
---|
56 | maila4 = LuoMaila(0.0, Level.Top - 20.0, 100.0, 20.0); |
---|
57 | |
---|
58 | maila1.Color = Color.DarkViolet; |
---|
59 | maila2.Color = Color.Yellow; |
---|
60 | maila3.Color = Color.Violet; |
---|
61 | maila4.Color = Color.YellowGreen; |
---|
62 | |
---|
63 | vasenReuna = Level.CreateLeftBorder(); |
---|
64 | vasenReuna.Restitution = 1.0; |
---|
65 | vasenReuna.IsVisible = false; |
---|
66 | |
---|
67 | oikeaReuna = Level.CreateRightBorder(); |
---|
68 | oikeaReuna.Restitution = 1.0; |
---|
69 | oikeaReuna.IsVisible = false; |
---|
70 | |
---|
71 | alaReuna = Level.CreateBottomBorder(); |
---|
72 | alaReuna.Restitution = 1.0; |
---|
73 | alaReuna.IsVisible = false; |
---|
74 | |
---|
75 | ylaReuna = Level.CreateTopBorder(); |
---|
76 | ylaReuna.Restitution = 1.0; |
---|
77 | ylaReuna.IsVisible = false; |
---|
78 | |
---|
79 | Camera.ZoomToLevel(); |
---|
80 | |
---|
81 | AddCollisionHandler (ball, KasittelePallonTormays); |
---|
82 | } |
---|
83 | void AloitaPeli () |
---|
84 | { |
---|
85 | Vector impulssi = new Vector(250.0, 200.0); |
---|
86 | ball.Hit(impulssi); |
---|
87 | } |
---|
88 | |
---|
89 | PhysicsObject LuoMaila( double x, double y, double z, double xyzo) |
---|
90 | { |
---|
91 | PhysicsObject maila = PhysicsObject.CreateStaticObject(z, xyzo); |
---|
92 | maila.Shape = Shape.Rectangle; |
---|
93 | maila.X = x; |
---|
94 | maila.Y = y; |
---|
95 | maila.Restitution = 1.0; |
---|
96 | Add(maila); |
---|
97 | |
---|
98 | return maila; |
---|
99 | } |
---|
100 | void AsetaOhjaimet() |
---|
101 | { |
---|
102 | Keyboard.Listen(Key.F1, ButtonState.Pressed, Ohjeet, "Näytä ohjeet"); |
---|
103 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
104 | |
---|
105 | Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "P1:n maila ylös", maila1, nopeusYlos); |
---|
106 | Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
107 | Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "P1:n maila alas", maila1, nopeusAlas); |
---|
108 | Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
109 | Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "P1:n maila vasen", maila3, nopeusVasen); |
---|
110 | Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila3, Vector.Zero); |
---|
111 | Keyboard.Listen(Key.D, ButtonState.Down, AsetaNopeus, "P1:n maila oikea", maila3, nopeusOikea); |
---|
112 | Keyboard.Listen(Key.D, ButtonState.Released, AsetaNopeus, null, maila3, Vector.Zero); |
---|
113 | |
---|
114 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "P2:n maila ylös", maila2, nopeusYlos); |
---|
115 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
116 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "P2:n maila alas", maila2, nopeusAlas); |
---|
117 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
118 | Keyboard.Listen(Key.Left, ButtonState.Down, AsetaNopeus, "P2:n maila vasen", maila4, nopeusVasen); |
---|
119 | Keyboard.Listen(Key.Left, ButtonState.Released, AsetaNopeus, null, maila4, Vector.Zero); |
---|
120 | Keyboard.Listen(Key.Right, ButtonState.Down, AsetaNopeus, "P2:n maila oikea", maila4, nopeusOikea); |
---|
121 | Keyboard.Listen(Key.Right, ButtonState.Released, AsetaNopeus, null, maila4, Vector.Zero); |
---|
122 | Keyboard.Listen(Key.Enter, ButtonState.Pressed, Reset, null); |
---|
123 | } |
---|
124 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
125 | { |
---|
126 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
127 | { |
---|
128 | maila.Velocity = Vector.Zero; |
---|
129 | return; |
---|
130 | } |
---|
131 | if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) |
---|
132 | { |
---|
133 | maila.Velocity = Vector.Zero; |
---|
134 | return; |
---|
135 | } |
---|
136 | if ((nopeus.X > 0) && (maila.Right > Level.Right)) |
---|
137 | { |
---|
138 | maila.Velocity = Vector.Zero; |
---|
139 | return; |
---|
140 | } |
---|
141 | if ((nopeus.X < 0) && (maila.Left < Level.Left)) |
---|
142 | { |
---|
143 | maila.Velocity = Vector.Zero; |
---|
144 | return; |
---|
145 | } |
---|
146 | |
---|
147 | maila.Velocity = nopeus; |
---|
148 | |
---|
149 | } |
---|
150 | void Ohjeet() |
---|
151 | { |
---|
152 | MessageDisplay.Add ( "P1: Ylös=Ylänuoli, Alas=Alanuoli P2: Ylös=A Alas=B" ) ; |
---|
153 | } |
---|
154 | void LisaaLaskurit() |
---|
155 | { |
---|
156 | pelaajan1pisteet = LuoLaskuri (Screen.Left +100, Screen.Top -100); |
---|
157 | pelaajan2pisteet = LuoLaskuri(Screen.Right -100, Screen.Top -100); |
---|
158 | } |
---|
159 | IntMeter LuoLaskuri(double x, double y) |
---|
160 | { |
---|
161 | IntMeter laskuri = new IntMeter(0); |
---|
162 | laskuri.MaxValue = 10; |
---|
163 | Label naytto = new Label(); |
---|
164 | naytto.BindTo(laskuri); |
---|
165 | naytto.X = x; |
---|
166 | naytto.Y = y; |
---|
167 | naytto.TextColor = Color.White; |
---|
168 | naytto.BorderColor = Level.BackgroundColor; |
---|
169 | naytto.Color = Level.BackgroundColor; |
---|
170 | Add(naytto); |
---|
171 | return laskuri; |
---|
172 | } |
---|
173 | void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) |
---|
174 | { |
---|
175 | if (kohde == vasenReuna) |
---|
176 | { |
---|
177 | pelaajan2pisteet.Value += 1; |
---|
178 | } |
---|
179 | else if (kohde == oikeaReuna) |
---|
180 | { |
---|
181 | pelaajan1pisteet.Value += 1; |
---|
182 | } |
---|
183 | else if (kohde == ylaReuna) |
---|
184 | { |
---|
185 | pelaajan1pisteet.Value += 1; |
---|
186 | } |
---|
187 | else if (kohde == alaReuna) |
---|
188 | { |
---|
189 | pelaajan2pisteet.Value += 1; |
---|
190 | } |
---|
191 | |
---|
192 | if (pelaajan1pisteet.Value > 9) |
---|
193 | { |
---|
194 | PhysicsEnabled = false; |
---|
195 | MessageDisplay.Add("Pelaaja 1 voitti!"); |
---|
196 | |
---|
197 | } |
---|
198 | else if (pelaajan2pisteet.Value > 9) |
---|
199 | { |
---|
200 | PhysicsEnabled = false; |
---|
201 | MessageDisplay.Add("Pelaaja 2 voitti!"); |
---|
202 | } |
---|
203 | |
---|
204 | } |
---|
205 | void Reset() |
---|
206 | { |
---|
207 | pelaajan1pisteet.Value = 0; |
---|
208 | pelaajan2pisteet.Value = 0; |
---|
209 | ball.Destroy(); |
---|
210 | ball = new PhysicsObject(20.0, 20.0); |
---|
211 | ball.Shape = Shape.Circle; |
---|
212 | ball.Color = Color.White; |
---|
213 | ball.X = -200; |
---|
214 | ball.Y = -200; |
---|
215 | ball.Restitution = 1.0; |
---|
216 | ball.KineticFriction = 0.0; |
---|
217 | ball.CanRotate = true; |
---|
218 | Add(ball); |
---|
219 | AddCollisionHandler(ball, KasittelePallonTormays); |
---|
220 | PhysicsEnabled = true; |
---|
221 | AloitaPeli(); |
---|
222 | } |
---|
223 | } |
---|