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, 200); |
---|
12 | Vector nopeusAlas = new Vector(0, -200); |
---|
13 | |
---|
14 | PhysicsObject maila1; |
---|
15 | PhysicsObject maila2; |
---|
16 | PhysicsObject pallo; |
---|
17 | IntMeter pelaajan1Pisteet; |
---|
18 | IntMeter pelaajan2Pisteet; |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | public override void Begin() |
---|
25 | { |
---|
26 | |
---|
27 | luokentta(); |
---|
28 | AloitaPeli(); |
---|
29 | AsetaOhjaimet(); |
---|
30 | LisaaLaskurit(); |
---|
31 | |
---|
32 | // TODO: Kirjoita ohjelmakoodisi tähän |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | } |
---|
42 | void luokentta() |
---|
43 | { |
---|
44 | Level.CreateBorders(1.0, false); |
---|
45 | Level.BackgroundColor = Color.Transparent; |
---|
46 | Camera.ZoomToLevel(); |
---|
47 | pallo = new PhysicsObject(50.0, 50.0); |
---|
48 | pallo.Shape = Shape.Circle; |
---|
49 | pallo.Color = Color.GreenYellow; |
---|
50 | pallo.Restitution = 1.0; |
---|
51 | pallo.Shape = Shape.Circle; |
---|
52 | pallo.Color = Color.GreenYellow; |
---|
53 | pallo.Restitution = 1.0; |
---|
54 | |
---|
55 | pallo.X = 0; |
---|
56 | pallo.Y = 0; |
---|
57 | Add(pallo); |
---|
58 | |
---|
59 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
60 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | void AloitaPeli() |
---|
68 | { |
---|
69 | Vector impulssi = new Vector(1000.0, 0.0); |
---|
70 | pallo.Hit(impulssi); |
---|
71 | |
---|
72 | } |
---|
73 | PhysicsObject LuoMaila(double x, double y) |
---|
74 | { |
---|
75 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
76 | maila.Shape = Shape.Rectangle; |
---|
77 | maila.X = x; |
---|
78 | maila.Y = y; |
---|
79 | maila.Restitution = 1.0; |
---|
80 | maila.Color = Color.DarkRed; |
---|
81 | Add(maila); |
---|
82 | return maila; |
---|
83 | } |
---|
84 | void AsetaOhjaimet() |
---|
85 | { |
---|
86 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
87 | |
---|
88 | Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); |
---|
89 | Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
90 | Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); |
---|
91 | Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
92 | |
---|
93 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); |
---|
94 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
95 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); |
---|
96 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
97 | |
---|
98 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
99 | |
---|
100 | |
---|
101 | } |
---|
102 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
103 | { |
---|
104 | if ((nopeus.Y > 0) && maila.Top > Level.Top) |
---|
105 | { |
---|
106 | maila.Velocity = Vector.Zero; |
---|
107 | return;} |
---|
108 | |
---|
109 | if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) |
---|
110 | { |
---|
111 | maila.Velocity = Vector.Zero; |
---|
112 | return; |
---|
113 | |
---|
114 | } |
---|
115 | |
---|
116 | maila.Velocity = nopeus; |
---|
117 | } |
---|
118 | void LisaaLaskurit(); |
---|
119 | { |
---|
120 | pelaajan1Pisteet = LuoPisteLaskuri ( Screen.Left + 100.0, Screen.Top - 100.0 ); |
---|
121 | pelaajan2Pisteet = LuoPisteLaskuri ( Screen.Right - 100.0, Screen.Top - 100.0 ); } |
---|
122 | |
---|
123 | |
---|
124 | IntMeter LuoPistelaskuri ( double x, double y ); |
---|
125 | |
---|
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.White; |
---|
134 | naytto.BorderColor = Level.BackgroundColor; |
---|
135 | naytto.Color = Level.BackgroundColor; |
---|
136 | Add( naytto ); |
---|
137 | |
---|
138 | return laskuri; |
---|
139 | |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | } |
---|
144 | |
---|