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, 700); |
---|
12 | Vector nopeusAlas = new Vector(0, -700); |
---|
13 | PhysicsObject pallo; |
---|
14 | PhysicsObject maila; |
---|
15 | PhysicsObject maila2; |
---|
16 | PhysicsObject vasenReuna; |
---|
17 | PhysicsObject oikeaReuna; |
---|
18 | PhysicsObject ylaReuna; |
---|
19 | PhysicsObject alaReuna; |
---|
20 | IntMeter pelaajan1pisteet; |
---|
21 | IntMeter pelaajan2pisteet; |
---|
22 | public override void Begin() |
---|
23 | { |
---|
24 | LuoKentta(); |
---|
25 | AloitaPeli(); |
---|
26 | LisaaLaskurit(); |
---|
27 | AsetaOhjaimet(); |
---|
28 | |
---|
29 | |
---|
30 | } |
---|
31 | void LuoKentta () |
---|
32 | { |
---|
33 | pallo = new PhysicsObject (70, 70); |
---|
34 | pallo.Shape = Shape.Hexagon; |
---|
35 | pallo.Color = Color.Turquoise; |
---|
36 | pallo.Restitution = 1.0; |
---|
37 | pallo.CanRotate = true; |
---|
38 | pallo.KineticFriction = 0; |
---|
39 | Add(pallo); |
---|
40 | maila = LuoMaila(400, Color.YellowGreen); |
---|
41 | maila2 = LuoMaila (-400, Color.OrangeRed); |
---|
42 | |
---|
43 | vasenReuna = Level.CreateLeftBorder(); |
---|
44 | vasenReuna.Restitution = 1; |
---|
45 | vasenReuna.IsVisible = false; |
---|
46 | oikeaReuna = Level.CreateRightBorder(); |
---|
47 | oikeaReuna.Restitution = 1; |
---|
48 | oikeaReuna.IsVisible = false; |
---|
49 | alaReuna = Level.CreateBottomBorder(); |
---|
50 | alaReuna.Restitution = 1; |
---|
51 | ylaReuna = Level.CreateTopBorder(); |
---|
52 | ylaReuna.Restitution = 1; |
---|
53 | Level.BackgroundColor = Color.DarkGreen; |
---|
54 | Camera.ZoomToLevel(); |
---|
55 | AddCollisionHandler(pallo, KasittelePallonTormays); |
---|
56 | } |
---|
57 | void AloitaPeli() |
---|
58 | { |
---|
59 | Vector impulssi = new Vector(550, 0); |
---|
60 | pallo.Hit(impulssi); |
---|
61 | } |
---|
62 | void AsetaOhjaimet() |
---|
63 | { |
---|
64 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
65 | Keyboard.Listen(Key.Up, ButtonState.Down, Asetanopeus, "Pelaaja1: Liikuta mailaa ylos", maila, nopeusYlos); |
---|
66 | Keyboard.Listen(Key.Up, ButtonState.Released, Asetanopeus, null, maila, Vector.Zero); |
---|
67 | Keyboard.Listen(Key.Down, ButtonState.Down, Asetanopeus, "Pelaaja1: Liikuta mailaa alas", maila, nopeusAlas); |
---|
68 | Keyboard.Listen(Key.Down, ButtonState.Released, Asetanopeus, null, maila, Vector.Zero); |
---|
69 | Keyboard.Listen(Key.A, ButtonState.Down, Asetanopeus, "Pelaaja2: Liikuta mailaa ylos", maila2, nopeusYlos); |
---|
70 | Keyboard.Listen(Key.A, ButtonState.Released, Asetanopeus, null, maila2, Vector.Zero); |
---|
71 | Keyboard.Listen(Key.Z, ButtonState.Down, Asetanopeus, "Pelaaja2: Liikuta mailaa alas", maila2, nopeusAlas); |
---|
72 | Keyboard.Listen(Key.Z, ButtonState.Released, Asetanopeus, null, maila2, Vector.Zero); |
---|
73 | } |
---|
74 | PhysicsObject LuoMaila(double x, Color vari) |
---|
75 | { |
---|
76 | PhysicsObject maila = PhysicsObject.CreateStaticObject (20, 120); |
---|
77 | |
---|
78 | maila.Shape = Shape.Rectangle; |
---|
79 | maila.Color = vari; |
---|
80 | maila.X = x; |
---|
81 | maila.Y = 0; |
---|
82 | maila.Restitution = 1; |
---|
83 | Add(maila); |
---|
84 | return maila; |
---|
85 | } |
---|
86 | void Asetanopeus(PhysicsObject maila, Vector nopeus) |
---|
87 | { |
---|
88 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
89 | { |
---|
90 | maila.Velocity = Vector.Zero; |
---|
91 | return; |
---|
92 | } |
---|
93 | if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) |
---|
94 | { |
---|
95 | maila.Velocity = Vector.Zero; |
---|
96 | return; |
---|
97 | } |
---|
98 | maila.Velocity = nopeus; |
---|
99 | } |
---|
100 | void LisaaLaskurit() |
---|
101 | { |
---|
102 | |
---|
103 | pelaajan1pisteet = LuoPisteLaskuri (-400,375); |
---|
104 | pelaajan2pisteet = LuoPisteLaskuri(400, 375); |
---|
105 | } |
---|
106 | IntMeter LuoPisteLaskuri(double x, double y) |
---|
107 | { |
---|
108 | IntMeter Laskuri = new IntMeter(0); |
---|
109 | Laskuri.MaxValue = 1000000; |
---|
110 | Label naytto = new Label(); |
---|
111 | naytto.BindTo(Laskuri); |
---|
112 | naytto.X = x; |
---|
113 | naytto.Y = y; |
---|
114 | naytto.TextColor = Color.DarkTurquoise; |
---|
115 | naytto.BorderColor = Color.SkyBlue; |
---|
116 | naytto.Color = Color.DarkGreen; |
---|
117 | Add(naytto); |
---|
118 | return Laskuri; |
---|
119 | } |
---|
120 | void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) |
---|
121 | { |
---|
122 | if (kohde == vasenReuna) |
---|
123 | { |
---|
124 | pelaajan2pisteet.Value += 1; |
---|
125 | } |
---|
126 | if (kohde == oikeaReuna) |
---|
127 | { |
---|
128 | pelaajan1pisteet.Value +=1; |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | |
---|