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