1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.Widgets; |
---|
4 | using Jypeli.Assets; |
---|
5 | |
---|
6 | public class Peli : PhysicsGame |
---|
7 | { |
---|
8 | Vector nopeusYlos = new Vector(0, 400); |
---|
9 | Vector nopeusAlas = new Vector(0, -400); |
---|
10 | |
---|
11 | PhysicsObject pallo; |
---|
12 | |
---|
13 | PhysicsObject maila1; |
---|
14 | PhysicsObject maila2; |
---|
15 | |
---|
16 | PhysicsObject vasenReuna; |
---|
17 | PhysicsObject oikeaReuna; |
---|
18 | |
---|
19 | |
---|
20 | IntMeter pelaajan1Pisteet; |
---|
21 | IntMeter pelaajan2Pisteet; |
---|
22 | |
---|
23 | //... |
---|
24 | protected override void Begin() |
---|
25 | { |
---|
26 | LuoKentta(); |
---|
27 | AloitaPeli(); |
---|
28 | AsetaOhjaimet(); |
---|
29 | LisaaLaskurit(); |
---|
30 | } |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | void AloitaPeli() |
---|
35 | { |
---|
36 | Vector impulssi = new Vector(500.0, 45.0); |
---|
37 | pallo.Hit( impulssi ); |
---|
38 | } |
---|
39 | //TODO: Alusta peli tässä |
---|
40 | |
---|
41 | void LuoKentta() |
---|
42 | { |
---|
43 | pallo = new PhysicsObject(40.0, 40.0); |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | pallo.Shape = Shapes.Circle; |
---|
48 | pallo.X = -200.0; |
---|
49 | pallo.Y = 0.0; |
---|
50 | pallo.X = -200.0; |
---|
51 | pallo.Y = 0.0; |
---|
52 | pallo.Restitution = 1.0; |
---|
53 | Add(pallo); |
---|
54 | pallo.Color = Color.YellowGreen; |
---|
55 | pallo.KineticFriction = 0.0; |
---|
56 | |
---|
57 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
58 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | vasenReuna = Level.CreateLeftBorder(); |
---|
63 | vasenReuna.Restitution = 1.0; |
---|
64 | vasenReuna.IsVisible = false; |
---|
65 | |
---|
66 | oikeaReuna = Level.CreateRightBorder(); |
---|
67 | oikeaReuna.Restitution = 1.0; |
---|
68 | oikeaReuna.IsVisible = false; |
---|
69 | |
---|
70 | PhysicsObject alaReuna = Level.CreateBottomBorder(); |
---|
71 | |
---|
72 | PhysicsObject ylaReuna = Level.CreateTopBorder(); |
---|
73 | |
---|
74 | Level.BackgroundColor = Color.Black; |
---|
75 | |
---|
76 | Camera.ZoomToLevel(); |
---|
77 | AddCollisionHandler(pallo, KasittelePallonTormays); |
---|
78 | |
---|
79 | } |
---|
80 | PhysicsObject LuoMaila(double x, double y) |
---|
81 | { |
---|
82 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
83 | maila.Shape = Shapes.Rectangle; |
---|
84 | maila.X = x; |
---|
85 | maila.Y = y; |
---|
86 | maila.Restitution = 1.0; |
---|
87 | Add(maila); |
---|
88 | |
---|
89 | return maila; |
---|
90 | |
---|
91 | } |
---|
92 | void AsetaOhjaimet() |
---|
93 | { |
---|
94 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
95 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
96 | |
---|
97 | Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); |
---|
98 | Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
99 | Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); |
---|
100 | Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
101 | |
---|
102 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); |
---|
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 | |
---|
107 | |
---|
108 | } |
---|
109 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
110 | { |
---|
111 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
112 | { |
---|
113 | maila.Velocity = Vector.Zero; |
---|
114 | return; |
---|
115 | } |
---|
116 | if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) |
---|
117 | { |
---|
118 | maila.Velocity = Vector.Zero; |
---|
119 | return; |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | maila.Velocity = nopeus; |
---|
124 | } |
---|
125 | |
---|
126 | protected override void Update(Time time) |
---|
127 | { |
---|
128 | base.Update(time); |
---|
129 | if (pallo.Velocity.Magnitude > 600) |
---|
130 | { |
---|
131 | while (pallo.Velocity.Magnitude > 500) |
---|
132 | { |
---|
133 | pallo.Velocity /= 1.1; |
---|
134 | } |
---|
135 | |
---|
136 | } |
---|
137 | |
---|
138 | if (pallo.Velocity.Magnitude < 400) |
---|
139 | { |
---|
140 | while (pallo.Velocity.Magnitude < 500) |
---|
141 | { |
---|
142 | pallo.Velocity *= 1.1; |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|
146 | IntMeter LuoPisteLaskuri(double x, double y) |
---|
147 | { |
---|
148 | IntMeter laskuri = new IntMeter(0); |
---|
149 | laskuri.MaxValue = 10; |
---|
150 | Label naytto = new Label(); |
---|
151 | naytto.BindTo( laskuri ); |
---|
152 | naytto.X = x; |
---|
153 | naytto.Y = y; |
---|
154 | naytto.TextColor = Color.White; |
---|
155 | Add(naytto); |
---|
156 | return laskuri; |
---|
157 | } |
---|
158 | |
---|
159 | void LisaaLaskurit() |
---|
160 | { |
---|
161 | pelaajan1Pisteet = LuoPisteLaskuri( Screen.Left + 100.0, Screen.Top - 100.0 ); |
---|
162 | pelaajan2Pisteet = LuoPisteLaskuri( Screen.Right - 100.0, Screen.Top - 100.0 ); |
---|
163 | } |
---|
164 | |
---|
165 | void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) |
---|
166 | { |
---|
167 | if (kohde == oikeaReuna) |
---|
168 | { |
---|
169 | pelaajan1Pisteet.Value += 1; |
---|
170 | } |
---|
171 | else if (kohde == vasenReuna) |
---|
172 | { |
---|
173 | pelaajan2Pisteet.Value += 1; |
---|
174 | } |
---|
175 | |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | |
---|
180 | } |
---|