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