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