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 FysiikkaPeli1 : PhysicsGame |
---|
10 | { |
---|
11 | PhysicsObject pallo; |
---|
12 | const double PALLON_MIN_NOPEUS = 500; |
---|
13 | PhysicsObject maila1; |
---|
14 | PhysicsObject maila2; |
---|
15 | |
---|
16 | PhysicsObject vasenReuna; |
---|
17 | PhysicsObject oikeaReuna; |
---|
18 | |
---|
19 | IntMeter pelaajan1Pisteet; |
---|
20 | IntMeter pelaajan2Pisteet; |
---|
21 | |
---|
22 | Vector nopeusYlos = new Vector(0, 400); |
---|
23 | Vector nopeusAlas = new Vector(0, -400); |
---|
24 | protected override void Update(Time time) |
---|
25 | { |
---|
26 | if (pallo != null && Math.Abs(pallo.Velocity.X) < PALLON_MIN_NOPEUS) |
---|
27 | { |
---|
28 | pallo.Velocity = new Vector(pallo.Velocity.X * 1.1, pallo.Velocity.Y); |
---|
29 | } |
---|
30 | base.Update(time); |
---|
31 | } |
---|
32 | |
---|
33 | public override void Begin() |
---|
34 | { |
---|
35 | // TODO: Kirjoita ohjelmakoodisi tähän |
---|
36 | |
---|
37 | LuoKentta(); |
---|
38 | AsetaOhjaimet(); |
---|
39 | LisaaLaskurit(); |
---|
40 | AloitaPeli(); |
---|
41 | |
---|
42 | } |
---|
43 | void LuoKentta() |
---|
44 | { |
---|
45 | pallo = new PhysicsObject(60.0, 60.0); |
---|
46 | |
---|
47 | Add(pallo); |
---|
48 | pallo.Shape = Shape.Circle; |
---|
49 | pallo.X = -0.0; |
---|
50 | pallo.Y = 0.0; |
---|
51 | |
---|
52 | AddCollisionHandler(pallo, KasittelePallonTormays); |
---|
53 | |
---|
54 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
55 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
56 | |
---|
57 | pallo.Restitution = 1.0; |
---|
58 | Level.Background.Color = Color.Yellow; |
---|
59 | Camera.ZoomToLevel(); |
---|
60 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
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 | alaReuna.Restitution = 1.0; |
---|
72 | alaReuna.IsVisible = false; |
---|
73 | |
---|
74 | PhysicsObject yläReuna = Level.CreateTopBorder(); |
---|
75 | yläReuna.Restitution = 1.0; |
---|
76 | yläReuna.IsVisible = false; |
---|
77 | } |
---|
78 | void AloitaPeli() |
---|
79 | { |
---|
80 | Vector impulssi = new Vector(500.0, 0.0); |
---|
81 | pallo.Hit(impulssi); |
---|
82 | } |
---|
83 | |
---|
84 | PhysicsObject LuoMaila(double x, double y) |
---|
85 | { |
---|
86 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
87 | maila.Shape = Shape.Rectangle; |
---|
88 | maila.X = x; |
---|
89 | maila.Y = y; |
---|
90 | maila.Restitution = 1.0; |
---|
91 | Add(maila); |
---|
92 | return maila; |
---|
93 | } |
---|
94 | void AsetaOhjaimet() |
---|
95 | { |
---|
96 | Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); |
---|
97 | Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
98 | |
---|
99 | Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); |
---|
100 | Keyboard.Listen(Key.Z, 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 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
108 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
109 | |
---|
110 | |
---|
111 | } |
---|
112 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
113 | { |
---|
114 | |
---|
115 | if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) |
---|
116 | { |
---|
117 | maila.Velocity = Vector.Zero; |
---|
118 | return; |
---|
119 | } |
---|
120 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
121 | { |
---|
122 | maila.Velocity = Vector.Zero; |
---|
123 | return; |
---|
124 | } |
---|
125 | |
---|
126 | maila.Velocity = nopeus; |
---|
127 | |
---|
128 | |
---|
129 | |
---|
130 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
131 | { |
---|
132 | maila.Velocity = Vector.Zero; |
---|
133 | return; |
---|
134 | } |
---|
135 | maila.Velocity = nopeus; |
---|
136 | } |
---|
137 | |
---|
138 | void LisaaLaskurit() |
---|
139 | { |
---|
140 | pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
141 | pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
142 | } |
---|
143 | IntMeter LuoPisteLaskuri(double x, double y) |
---|
144 | { |
---|
145 | IntMeter laskuri = new IntMeter(0); |
---|
146 | laskuri.MaxValue = 10; |
---|
147 | |
---|
148 | Label naytto = new Label(); |
---|
149 | naytto.BindTo(laskuri); |
---|
150 | naytto.X = x; |
---|
151 | naytto.Y = y; |
---|
152 | naytto.TextColor = Color.Black; |
---|
153 | naytto.BorderColor = Level.Background.Color; |
---|
154 | naytto.Color = Level.Background.Color; |
---|
155 | Add(naytto); |
---|
156 | |
---|
157 | return laskuri; |
---|
158 | } |
---|
159 | void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) |
---|
160 | { |
---|
161 | if (kohde == oikeaReuna) |
---|
162 | { |
---|
163 | pelaajan1Pisteet.Value += 1; |
---|
164 | } |
---|
165 | else if (kohde == vasenReuna) |
---|
166 | { |
---|
167 | pelaajan2Pisteet.Value += 1; |
---|
168 | } |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | |
---|