1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.Widgets; |
---|
4 | |
---|
5 | public class Peli : PhysicsGame |
---|
6 | { |
---|
7 | Vector nopeusYlos = new Vector(0, 200); |
---|
8 | Vector nopeusAlas = new Vector(0, -200); |
---|
9 | |
---|
10 | PhysicsObject pallo; |
---|
11 | PhysicsObject maila1; |
---|
12 | PhysicsObject maila2; |
---|
13 | |
---|
14 | IntMeter pelaajan1Pisteet; |
---|
15 | IntMeter pelaajan2Pisteet; |
---|
16 | |
---|
17 | PhysicsObject vasenReuna; |
---|
18 | PhysicsObject oikeaReuna; |
---|
19 | |
---|
20 | protected override void Begin() |
---|
21 | { |
---|
22 | LuoKentta(); |
---|
23 | AsetaOhjaimet(); |
---|
24 | LisaaLaskurit(); |
---|
25 | AloitaPeli(); |
---|
26 | } |
---|
27 | |
---|
28 | void LuoKentta() |
---|
29 | { |
---|
30 | pallo = new PhysicsObject( 40.0, 40.0 ); |
---|
31 | pallo.Shape = Shapes.Circle; |
---|
32 | pallo.X = -200.0; |
---|
33 | pallo.Y = 0.0; |
---|
34 | pallo.Restitution = 1.0; |
---|
35 | pallo.CanRotate = false; |
---|
36 | Add( pallo ); |
---|
37 | |
---|
38 | |
---|
39 | maila1 = LuoMaila( Level.Left + 20.0, 0.0 ); |
---|
40 | maila2 = LuoMaila( Level.Right - 20.0, 0.0 ); |
---|
41 | |
---|
42 | vasenReuna = Level.CreateLeftBorder(); |
---|
43 | vasenReuna.Restitution = 1.0; |
---|
44 | vasenReuna.IsVisible = false; |
---|
45 | |
---|
46 | oikeaReuna = Level.CreateRightBorder(); |
---|
47 | oikeaReuna.Restitution = 1.0; |
---|
48 | oikeaReuna.IsVisible = false; |
---|
49 | |
---|
50 | PhysicsObject ylaReuna = Level.CreateTopBorder(); |
---|
51 | vasenReuna.Restitution = 1.0; |
---|
52 | vasenReuna.IsVisible = false; |
---|
53 | |
---|
54 | PhysicsObject alaReuna = Level.CreateBottomBorder(); |
---|
55 | vasenReuna.Restitution = 1.0; |
---|
56 | vasenReuna.IsVisible = false; |
---|
57 | |
---|
58 | Level.BackgroundColor = Color.Black; |
---|
59 | |
---|
60 | Camera.ZoomToLevel(); |
---|
61 | AddCollisionHandler(pallo, KasittelePallonTormays) ; |
---|
62 | } |
---|
63 | |
---|
64 | PhysicsObject LuoMaila( double x, double y ) |
---|
65 | { |
---|
66 | PhysicsObject maila = PhysicsObject.CreateStaticObject( 20.0, 100.0 ); |
---|
67 | maila.Shape = Shapes.Rectangle; |
---|
68 | maila.X = x; |
---|
69 | maila.Y = y; |
---|
70 | maila.Restitution = 1.0; |
---|
71 | Add( maila ); |
---|
72 | return maila; |
---|
73 | } |
---|
74 | |
---|
75 | void AloitaPeli() |
---|
76 | { |
---|
77 | Vector impulssi = new Vector( 500.0, 0.0 ); |
---|
78 | pallo.Hit( impulssi ); |
---|
79 | } |
---|
80 | |
---|
81 | void AsetaOhjaimet() |
---|
82 | { |
---|
83 | Keyboard.Listen( Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos ); |
---|
84 | Keyboard.Listen( Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero ); |
---|
85 | Keyboard.Listen( Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas ); |
---|
86 | Keyboard.Listen( Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero ); |
---|
87 | |
---|
88 | Keyboard.Listen( Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos ); |
---|
89 | Keyboard.Listen( Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero ); |
---|
90 | Keyboard.Listen( Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas ); |
---|
91 | Keyboard.Listen( Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero ); |
---|
92 | |
---|
93 | Keyboard.Listen( Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet" ); |
---|
94 | Keyboard.Listen( Key.Escape, ButtonState.Pressed, Exit, "Poistu" ); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | void AsetaNopeus( PhysicsObject maila, Vector nopeus ) |
---|
99 | { |
---|
100 | if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) |
---|
101 | { |
---|
102 | maila.Velocity = Vector.Zero; |
---|
103 | return; |
---|
104 | } |
---|
105 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
106 | { |
---|
107 | maila.Velocity = Vector.Zero; |
---|
108 | return; |
---|
109 | } |
---|
110 | maila.Velocity = nopeus; |
---|
111 | } |
---|
112 | void LisaaLaskurit() |
---|
113 | { |
---|
114 | pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
115 | pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
116 | } |
---|
117 | IntMeter LuoPisteLaskuri(double x, double y) |
---|
118 | { |
---|
119 | IntMeter laskuri = new IntMeter(0); |
---|
120 | laskuri.MaxValue = 10; |
---|
121 | Label naytto = new Label(); |
---|
122 | naytto.BindTo(laskuri); |
---|
123 | naytto.X = x; |
---|
124 | naytto.Y = y; |
---|
125 | naytto.TextColor = Color.White; |
---|
126 | Add(naytto); |
---|
127 | return laskuri; |
---|
128 | } |
---|
129 | void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) |
---|
130 | { |
---|
131 | if (kohde == oikeaReuna) |
---|
132 | { |
---|
133 | pelaajan1Pisteet.Value += 1; |
---|
134 | } |
---|
135 | else if (kohde == vasenReuna) |
---|
136 | { |
---|
137 | pelaajan2Pisteet.Value += 1; |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|