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