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