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