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