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 | PhysicsObject vasenReuna; |
---|
20 | PhysicsObject oikeaReuna; |
---|
21 | |
---|
22 | IntMeter Pelaajan1Pisteet; |
---|
23 | IntMeter Pelaajan2Pisteet; |
---|
24 | |
---|
25 | public override void Begin() |
---|
26 | { |
---|
27 | LuoKentta (); |
---|
28 | AsetaOhjaimet(); |
---|
29 | LisaaLaskurit(); |
---|
30 | Aloitapeli (); |
---|
31 | } |
---|
32 | |
---|
33 | // TODO: Kirjoita ohjelmakoodisi tähän |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | void LuoKentta() |
---|
41 | { |
---|
42 | pallo = new PhysicsObject(20, 20); |
---|
43 | Add(pallo); |
---|
44 | pallo.Shape = Shape.Circle; |
---|
45 | pallo.X = -200; |
---|
46 | pallo.Y = 0; |
---|
47 | pallo.Restitution = 1.0; |
---|
48 | pallo.KineticFriction = 0.0; |
---|
49 | AddCollisionHandler(pallo, KasittelePallonTormays); |
---|
50 | |
---|
51 | maila1 = LuoMaila (Level.Left + 20, 0); |
---|
52 | maila2 = LuoMaila (Level.Right - 20, 0); |
---|
53 | |
---|
54 | Camera.ZoomToLevel(); |
---|
55 | |
---|
56 | vasenReuna = Level.CreateLeftBorder(); |
---|
57 | vasenReuna.Restitution = 1; |
---|
58 | vasenReuna.IsVisible = false; |
---|
59 | |
---|
60 | oikeaReuna = Level.CreateRightBorder(); |
---|
61 | vasenReuna.Restitution = 1; |
---|
62 | vasenReuna.IsVisible = false; |
---|
63 | |
---|
64 | PhysicsObject yläReuna = Level.CreateTopBorder(); |
---|
65 | yläReuna.Restitution = 1; |
---|
66 | yläReuna.IsVisible = false; |
---|
67 | |
---|
68 | PhysicsObject alaReuna = Level.CreateBottomBorder(); |
---|
69 | alaReuna.Restitution = 1; |
---|
70 | alaReuna.IsVisible = false; |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | Level.BackgroundColor = Color.Black; |
---|
75 | MessageDisplay.TextColor = Color.White; |
---|
76 | |
---|
77 | AddCollisionHandler (pallo, KasittelePallonTormays); |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | } |
---|
83 | void Aloitapeli() |
---|
84 | { |
---|
85 | Vector impulssi = new Vector (500, 200); |
---|
86 | pallo.Hit(impulssi); |
---|
87 | } |
---|
88 | |
---|
89 | PhysicsObject LuoMaila (double x, double y) |
---|
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; |
---|
95 | Add(maila); |
---|
96 | |
---|
97 | return maila; |
---|
98 | } |
---|
99 | void AsetaOhjaimet() |
---|
100 | { |
---|
101 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
102 | |
---|
103 | Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja liikuttaa mailaa ylös", maila1, nopeusYlos); |
---|
104 | Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
105 | |
---|
106 | Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); |
---|
107 | Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
108 | |
---|
109 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); |
---|
110 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
111 | |
---|
112 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); |
---|
113 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
114 | |
---|
115 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
116 | |
---|
117 | |
---|
118 | } |
---|
119 | |
---|
120 | void AsetaNopeus ( PhysicsObject Maila, Vector Nopeus) |
---|
121 | { |
---|
122 | if ( (Nopeus.Y < 0 ) && (Maila.Bottom < Level.Bottom) ) |
---|
123 | { |
---|
124 | Maila.Velocity = Vector.Zero; |
---|
125 | return; |
---|
126 | } |
---|
127 | |
---|
128 | if ( (Nopeus.Y > 0 ) && (Maila.Top > Level.Top) ) |
---|
129 | { |
---|
130 | Maila.Velocity = Vector.Zero; |
---|
131 | return; |
---|
132 | } |
---|
133 | |
---|
134 | Maila.Velocity = Nopeus; |
---|
135 | } |
---|
136 | void LisaaLaskurit() |
---|
137 | { |
---|
138 | Pelaajan1Pisteet = LuoPisteLaskuri ( Screen.Left + 100.0 , Screen.Top - 100.0 ); |
---|
139 | Pelaajan2Pisteet = LuoPisteLaskuri ( Screen.Right - 100.0 , Screen.Top - 100.0 ); |
---|
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 | |
---|
154 | return laskuri; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | void KasittelePallonTormays ( PhysicsObject pallo, PhysicsObject kohde ) |
---|
161 | { |
---|
162 | if (kohde == oikeaReuna) |
---|
163 | { |
---|
164 | Pelaajan1Pisteet.Value += 1; |
---|
165 | } |
---|
166 | else if (kohde == vasenReuna) |
---|
167 | { |
---|
168 | Pelaajan2Pisteet.Value += 1; |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | } |
---|
173 | |
---|
174 | } |
---|