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 PoNG : PhysicsGame |
---|
10 | { |
---|
11 | Vector nopeusylos = new Vector(0, 200); |
---|
12 | Vector nopeusalas = new Vector(0, -200); |
---|
13 | PhysicsObject pallo; |
---|
14 | PhysicsObject maila1; |
---|
15 | PhysicsObject maila2; |
---|
16 | PhysicsObject vasenreuna; |
---|
17 | PhysicsObject oikeareuna; |
---|
18 | IntMeter pelaajan1pisteet; |
---|
19 | IntMeter pelaajan2pisteet; |
---|
20 | |
---|
21 | public override void Begin() |
---|
22 | { |
---|
23 | Luokentta(); |
---|
24 | aloitapeli(); |
---|
25 | asetenappaimet(); |
---|
26 | lisaaLaskurit(); |
---|
27 | |
---|
28 | Vector impulssi = new Vector(1000, 0); |
---|
29 | pallo.Hit(impulssi); |
---|
30 | |
---|
31 | PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); |
---|
32 | } |
---|
33 | |
---|
34 | void Luokentta() |
---|
35 | { |
---|
36 | pallo = new PhysicsObject(30.0, 30.0); |
---|
37 | Add(pallo); |
---|
38 | pallo.Shape = Shape.Circle; |
---|
39 | pallo.Color = Color.Orange; |
---|
40 | pallo.X = 000; |
---|
41 | pallo.Y = 000; |
---|
42 | maila1 = luomaila(Level.Left + 20.0, 0.0); |
---|
43 | maila2 = luomaila(Level.Right - 20.0, 0.0); |
---|
44 | pallo.Restitution = 20.0; |
---|
45 | Level.BackgroundColor = Color.DarkJungleGreen; |
---|
46 | vasenreuna = Level.CreateLeftBorder(); |
---|
47 | vasenreuna.Restitution = 1.0; |
---|
48 | vasenreuna.IsVisible = false; |
---|
49 | oikeareuna = Level.CreateRightBorder(); |
---|
50 | oikeareuna.Restitution = 10.0; |
---|
51 | oikeareuna.IsVisible = false; |
---|
52 | PhysicsObject alareuna = Level.CreateBottomBorder(); |
---|
53 | alareuna.Restitution = 1.0; |
---|
54 | alareuna.IsVisible = false; |
---|
55 | alareuna.KineticFriction = 0.0; |
---|
56 | PhysicsObject ylareuna = Level.CreateTopBorder(); |
---|
57 | ylareuna.Restitution = 1.0; |
---|
58 | ylareuna.IsVisible = false; |
---|
59 | |
---|
60 | AddCollisionHandler(pallo, kasittelepallontormays); |
---|
61 | Camera.ZoomToLevel(); |
---|
62 | } |
---|
63 | |
---|
64 | void aloitapeli() |
---|
65 | { |
---|
66 | Vector impulssi = new Vector(1000, 0); |
---|
67 | pallo.Hit(impulssi); |
---|
68 | } |
---|
69 | PhysicsObject luomaila(double x, double y) |
---|
70 | { |
---|
71 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
72 | maila.Shape = Shape.Rectangle; |
---|
73 | maila.X = x; |
---|
74 | maila.Y = y; |
---|
75 | maila.Restitution = 1.0; |
---|
76 | Add(maila); |
---|
77 | return maila; |
---|
78 | } |
---|
79 | void asetenappaimet() |
---|
80 | { |
---|
81 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
82 | Keyboard.Listen(Key.A, ButtonState.Down, Asetanopeus, "pelaaja 1. liikuttaa mailaa ylös", maila1, nopeusylos); |
---|
83 | Keyboard.Listen(Key.A, ButtonState.Released, Asetanopeus, null, maila1, Vector.Zero); |
---|
84 | Keyboard.Listen(Key.Z, ButtonState.Down, Asetanopeus, "pelaja 2 liikuttaa mailaa alas", maila1, nopeusalas); |
---|
85 | Keyboard.Listen(Key.Z, ButtonState.Released, Asetanopeus, null, maila1, Vector.Zero); |
---|
86 | Keyboard.Listen(Key.Up, ButtonState.Down, Asetanopeus, "pelaaja 2 liikuttaa mailaa ylös", maila2, nopeusylos); |
---|
87 | Keyboard.Listen(Key.Up, ButtonState.Released, Asetanopeus, null, maila2, Vector.Zero); |
---|
88 | Keyboard.Listen(Key.Down, ButtonState.Down, Asetanopeus, "pelaaja 2 liikuttaa mailaa alas", maila2, nopeusalas); |
---|
89 | Keyboard.Listen(Key.Down, ButtonState.Released, Asetanopeus, null, maila2, Vector.Zero); |
---|
90 | } |
---|
91 | void Asetanopeus(PhysicsObject maila, Vector nopeus) |
---|
92 | { |
---|
93 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
94 | { |
---|
95 | maila.Velocity = Vector.Zero; |
---|
96 | return; |
---|
97 | } |
---|
98 | |
---|
99 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
100 | { |
---|
101 | maila.Velocity = Vector.Zero; |
---|
102 | return; |
---|
103 | } |
---|
104 | maila.Velocity = nopeus; |
---|
105 | } |
---|
106 | void lisaaLaskurit() |
---|
107 | { |
---|
108 | pelaajan1pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
109 | pelaajan2pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
110 | } |
---|
111 | IntMeter LuoPisteLaskuri(double x, double y) |
---|
112 | { |
---|
113 | IntMeter laskuri = new IntMeter(0); |
---|
114 | laskuri.MaxValue = (10); |
---|
115 | |
---|
116 | Label naytto = new Label(); |
---|
117 | naytto.BindTo(laskuri); |
---|
118 | naytto.X = x; |
---|
119 | naytto.Y = y; |
---|
120 | naytto.TextColor = Color.White; |
---|
121 | naytto.BorderColor = Level.BackgroundColor; |
---|
122 | naytto.Color = Level.BackgroundColor; |
---|
123 | Add(naytto); |
---|
124 | |
---|
125 | return laskuri; |
---|
126 | { |
---|
127 | } |
---|
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 | } |
---|
141 | |
---|
142 | |
---|