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