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 | Vector oikeah = new Vector(500, 0); |
---|
14 | Vector vasenh = new Vector(-500, 0); |
---|
15 | |
---|
16 | PhysicsObject pallo; |
---|
17 | PhysicsObject maila1; |
---|
18 | PhysicsObject maila2; |
---|
19 | PhysicsObject oikeaReuna; |
---|
20 | PhysicsObject vasenReuna; |
---|
21 | PhysicsObject yläReuna; |
---|
22 | PhysicsObject alaReuna; |
---|
23 | |
---|
24 | |
---|
25 | IntMeter pelaajan1pisteet; |
---|
26 | IntMeter pelaajan2pisteet; |
---|
27 | |
---|
28 | public override void Begin() |
---|
29 | { |
---|
30 | |
---|
31 | |
---|
32 | Luokentta(); |
---|
33 | Asetaohjaimet (); |
---|
34 | LisaaLaskuri(); |
---|
35 | Aloitapeli(); |
---|
36 | } |
---|
37 | |
---|
38 | void Luokentta() |
---|
39 | { |
---|
40 | pallo = new PhysicsObject(40.0, 40.0); |
---|
41 | pallo.Shape = Shape.Circle; |
---|
42 | pallo.X = 0.0; |
---|
43 | pallo.Y = 0.0; |
---|
44 | pallo.Restitution = 1.0; |
---|
45 | Add(pallo); |
---|
46 | |
---|
47 | maila1 = Luomaila(Level.Left + 20.0, 0.0); |
---|
48 | maila2 = Luomaila(Level.Right - 20.0, 0.0); |
---|
49 | |
---|
50 | vasenReuna = Level.CreateLeftBorder(); |
---|
51 | vasenReuna.Restitution = 1.0; |
---|
52 | vasenReuna.IsVisible = false; |
---|
53 | oikeaReuna = Level.CreateRightBorder(); |
---|
54 | oikeaReuna.Restitution = 1.0; |
---|
55 | oikeaReuna.IsVisible = false; |
---|
56 | alaReuna = Level.CreateBottomBorder(); |
---|
57 | alaReuna.Restitution = 1.0; |
---|
58 | alaReuna.IsVisible = false; |
---|
59 | yläReuna = Level.CreateTopBorder(); |
---|
60 | yläReuna.Restitution = 1.0; |
---|
61 | yläReuna.IsVisible = false; |
---|
62 | |
---|
63 | Level.BackgroundColor = Color.Green; |
---|
64 | |
---|
65 | AddCollisionHandler(pallo, KasittelePallonTormays); |
---|
66 | |
---|
67 | Camera.ZoomToLevel(); |
---|
68 | } |
---|
69 | |
---|
70 | PhysicsObject Luomaila(double x, double y) |
---|
71 | { |
---|
72 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
73 | maila.Shape = Shape.Rectangle; |
---|
74 | maila.X = x; |
---|
75 | maila.Y = y; |
---|
76 | maila.Restitution = 1.0; |
---|
77 | Add(maila); |
---|
78 | return maila; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | void Aloitapeli() |
---|
83 | { |
---|
84 | if (pelaajan2pisteet > pelaajan1pisteet) |
---|
85 | { |
---|
86 | Vector impulssi = new Vector(500.0, 0.0); |
---|
87 | pallo.Hit(impulssi); |
---|
88 | } |
---|
89 | if (pelaajan1pisteet > pelaajan2pisteet) |
---|
90 | { |
---|
91 | Vector impulssi = new Vector(-500.0, 0.0); |
---|
92 | pallo.Hit(impulssi); |
---|
93 | } |
---|
94 | if (pelaajan1pisteet > pelaajan2pisteet) |
---|
95 | { |
---|
96 | Vector impulssi = new Vector(500.0, 0.0); |
---|
97 | pallo.Hit(impulssi); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | void Asetaohjaimet() |
---|
102 | { |
---|
103 | Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); |
---|
104 | Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
105 | Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); |
---|
106 | Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
107 | |
---|
108 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); |
---|
109 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
110 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); |
---|
111 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
112 | |
---|
113 | |
---|
114 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
115 | Keyboard.Listen(Key.Space, ButtonState.Pressed, alkuun, null); |
---|
116 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
117 | |
---|
118 | } |
---|
119 | |
---|
120 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
121 | { |
---|
122 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
123 | { |
---|
124 | maila.Velocity = Vector.Zero; |
---|
125 | return; |
---|
126 | } |
---|
127 | if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) |
---|
128 | { |
---|
129 | maila.Velocity = Vector.Zero; |
---|
130 | return; |
---|
131 | } |
---|
132 | |
---|
133 | maila.Velocity = nopeus; |
---|
134 | } |
---|
135 | |
---|
136 | void LisaaLaskuri() |
---|
137 | { |
---|
138 | pelaajan1pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
139 | pelaajan2pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | IntMeter LuoPisteLaskuri(double x, double y) |
---|
144 | { |
---|
145 | IntMeter laskuri = new IntMeter(0); |
---|
146 | laskuri.MaxValue = 9; |
---|
147 | Label naytto = new Label(); |
---|
148 | naytto.BindTo(laskuri); |
---|
149 | naytto.X = x; |
---|
150 | naytto.Y = y; |
---|
151 | naytto.TextColor = Color.White; |
---|
152 | naytto.BorderColor = Level.BackgroundColor; |
---|
153 | naytto.Color = Level.BackgroundColor; |
---|
154 | Add(naytto); |
---|
155 | return laskuri; |
---|
156 | } |
---|
157 | |
---|
158 | void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) |
---|
159 | { |
---|
160 | if (kohde == oikeaReuna) |
---|
161 | { |
---|
162 | pelaajan1pisteet.Value += 1; |
---|
163 | pallo.X = 0.0; |
---|
164 | pallo.Y = 0.0; |
---|
165 | pallo.Velocity = Vector.Zero; |
---|
166 | pallo.Destroy(); |
---|
167 | maila1.Destroy(); |
---|
168 | maila2.Destroy(); |
---|
169 | Luokentta(); |
---|
170 | Asetaohjaimet(); |
---|
171 | Aloitapeli(); |
---|
172 | |
---|
173 | } |
---|
174 | else if(kohde == vasenReuna) |
---|
175 | { |
---|
176 | pelaajan2pisteet.Value += 1; |
---|
177 | pallo.X = 0.0; |
---|
178 | pallo.Y = 0.0; |
---|
179 | pallo.Velocity = Vector.Zero; |
---|
180 | pallo.Destroy(); |
---|
181 | maila1.Destroy(); |
---|
182 | maila2.Destroy(); |
---|
183 | Luokentta(); |
---|
184 | Asetaohjaimet(); |
---|
185 | Aloitapeli(); |
---|
186 | |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | void alkuun() |
---|
191 | { |
---|
192 | pallo.X = 0.0; |
---|
193 | pallo.Y = 0.0; |
---|
194 | pallo.Velocity = Vector.Zero; |
---|
195 | pallo.Destroy(); |
---|
196 | maila1.Destroy(); |
---|
197 | maila2.Destroy(); |
---|
198 | Luokentta(); |
---|
199 | Asetaohjaimet(); |
---|
200 | Aloitapeli(); |
---|
201 | } |
---|
202 | |
---|
203 | } |
---|