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