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 Pongis_Khan : PhysicsGame |
---|
10 | { |
---|
11 | Vector nopeusYlös = new Vector(0, 800); |
---|
12 | Vector nopeusAlas = new Vector(0, -800); |
---|
13 | PhysicsObject pallo; |
---|
14 | PhysicsObject maila2; |
---|
15 | PhysicsObject maila; |
---|
16 | |
---|
17 | PhysicsObject vasenReuna; |
---|
18 | PhysicsObject oikeaReuna; |
---|
19 | |
---|
20 | |
---|
21 | IntMeter pelaajan1Pisteet; |
---|
22 | IntMeter pelaajan2Pisteet; |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | public override void Begin() |
---|
27 | { |
---|
28 | |
---|
29 | LuoKentta(); |
---|
30 | AloitaPeli(); |
---|
31 | AsetaOhjaimet(); |
---|
32 | LisaaLaskurit(); |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | Level.Background.CreateGradient(Color.Black, Color.Black); |
---|
37 | PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); |
---|
38 | |
---|
39 | } |
---|
40 | void LuoKentta() |
---|
41 | { |
---|
42 | pallo = new PhysicsObject(50, 50); |
---|
43 | Add(pallo); |
---|
44 | pallo.Shape = Shape.Octagon; |
---|
45 | pallo.Color = Color.White; |
---|
46 | pallo.X = -200.0; |
---|
47 | pallo.Y = 0.0; |
---|
48 | |
---|
49 | maila2 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
50 | maila = LuoMaila(Level.Right - 20.0, 0.0); |
---|
51 | |
---|
52 | vasenReuna = Level.CreateLeftBorder(); |
---|
53 | vasenReuna.Restitution = 1.0; |
---|
54 | vasenReuna.IsVisible = false; |
---|
55 | |
---|
56 | PhysicsObject Bottom = Level.CreateBottomBorder(); |
---|
57 | Bottom.Restitution = 1.0; |
---|
58 | Bottom.IsVisible = false; |
---|
59 | |
---|
60 | PhysicsObject Top = Level.CreateTopBorder(); |
---|
61 | Top.Restitution = 1.0; |
---|
62 | Top.IsVisible = false; |
---|
63 | |
---|
64 | oikeaReuna = Level.CreateRightBorder(); |
---|
65 | oikeaReuna.Restitution = 1.0; |
---|
66 | oikeaReuna.IsVisible = false; |
---|
67 | Camera.ZoomToLevel(); |
---|
68 | |
---|
69 | AddCollisionHandler(pallo, KasittelePallonTormays); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | protected override void Update(Time time) |
---|
74 | { |
---|
75 | if (pallo != null && Math.Abs(pallo.Velocity.X) < 1000) |
---|
76 | { |
---|
77 | pallo.Velocity = new Vector(pallo.Velocity.X * 1.1, pallo.Velocity.Y); |
---|
78 | } |
---|
79 | base.Update(time); |
---|
80 | |
---|
81 | } |
---|
82 | void AloitaPeli() |
---|
83 | { |
---|
84 | |
---|
85 | Vector impulssi = new Vector(1000.0, 0.0); |
---|
86 | pallo.Hit(impulssi); |
---|
87 | } |
---|
88 | PhysicsObject LuoMaila(double x, double y) |
---|
89 | { |
---|
90 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
91 | maila.Shape = Shape.Rectangle; |
---|
92 | maila.X = x; |
---|
93 | maila.Y = y; |
---|
94 | maila.Restitution = 1.0; |
---|
95 | Add(maila); |
---|
96 | return maila; |
---|
97 | } |
---|
98 | void AsetaOhjaimet() |
---|
99 | { |
---|
100 | Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "", maila2, nopeusYlös); |
---|
101 | Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
102 | Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "", maila2, nopeusAlas); |
---|
103 | Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
104 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "", maila, nopeusYlös); |
---|
105 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila, Vector.Zero); |
---|
106 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "", maila, nopeusAlas); |
---|
107 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila, Vector.Zero); |
---|
108 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, ""); |
---|
109 | } |
---|
110 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
111 | { |
---|
112 | |
---|
113 | if ((nopeus.Y >= 0) && (maila.Top >= Level.Top)) |
---|
114 | { |
---|
115 | maila.Velocity = Vector.Zero; |
---|
116 | return; |
---|
117 | } |
---|
118 | if ((nopeus.Y <= 0) && (maila.Bottom <= Level.Bottom)) |
---|
119 | { |
---|
120 | maila.Velocity = Vector.Zero; |
---|
121 | return; |
---|
122 | } |
---|
123 | maila.Velocity = nopeus; |
---|
124 | } |
---|
125 | void LisaaLaskurit() |
---|
126 | { |
---|
127 | |
---|
128 | |
---|
129 | pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
130 | pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | } IntMeter LuoPisteLaskuri(double x, double y) |
---|
136 | { |
---|
137 | |
---|
138 | IntMeter laskuri = new IntMeter(0); |
---|
139 | laskuri.MaxValue = 10; |
---|
140 | Label naytto = new Label(); |
---|
141 | naytto.BindTo(laskuri); |
---|
142 | |
---|
143 | naytto.X = x; |
---|
144 | naytto.Y = y; |
---|
145 | |
---|
146 | naytto.TextColor = Color.White; |
---|
147 | naytto.BorderColor = Level.Background.Color; |
---|
148 | naytto.Color = Level.Background.Color; |
---|
149 | Add(naytto); |
---|
150 | |
---|
151 | return laskuri; |
---|
152 | } |
---|
153 | void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) |
---|
154 | { |
---|
155 | if (kohde == oikeaReuna) |
---|
156 | { |
---|
157 | pelaajan1Pisteet.Value += 1; |
---|
158 | } |
---|
159 | else if (kohde == vasenReuna) |
---|
160 | { |
---|
161 | pelaajan2Pisteet.Value += 1; |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | } |
---|