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