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