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 | PhysicsObject pallo; |
---|
12 | const double PALLON_MIN_NOPEUS = 500; |
---|
13 | |
---|
14 | protected override void Update(Time time) |
---|
15 | { |
---|
16 | if (pallo != null && Math.Abs(pallo.Velocity.X) < PALLON_MIN_NOPEUS) |
---|
17 | { |
---|
18 | pallo.Velocity = new Vector(pallo.Velocity.X * 1.1, pallo.Velocity.Y); |
---|
19 | } |
---|
20 | base.Update(time); |
---|
21 | } |
---|
22 | Vector nopeusYlos = new Vector(0, 200); |
---|
23 | Vector nopeusAlas = new Vector(0, -200); |
---|
24 | PhysicsObject maila1; |
---|
25 | PhysicsObject maila2; |
---|
26 | public override void Begin() |
---|
27 | { |
---|
28 | Luokentta(); |
---|
29 | AloitaPeli(); |
---|
30 | AsetaOhjaimet(); |
---|
31 | |
---|
32 | |
---|
33 | } |
---|
34 | void Luokentta() |
---|
35 | { |
---|
36 | pallo = new PhysicsObject(50.0, 50.0); |
---|
37 | Add(pallo); |
---|
38 | pallo.Shape = Shape.Circle; |
---|
39 | pallo.X = 0.0; |
---|
40 | pallo.Y = 0.0; |
---|
41 | Level.CreateBorders(1.0, false); |
---|
42 | pallo.Restitution = (1.0); |
---|
43 | Level.Background.Color = Color.Black; |
---|
44 | Camera.ZoomToLevel(); |
---|
45 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
46 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
47 | |
---|
48 | } |
---|
49 | void AloitaPeli() |
---|
50 | { |
---|
51 | Vector impulssi = new Vector(450.0, 30.0); |
---|
52 | pallo.Hit(impulssi); |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | } |
---|
58 | |
---|
59 | PhysicsObject LuoMaila(double x, double y) |
---|
60 | { |
---|
61 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
62 | maila.Shape = Shape.Rectangle; |
---|
63 | maila.X = x; |
---|
64 | maila.Y = y; |
---|
65 | maila.Restitution = 1.0; |
---|
66 | Add(maila); |
---|
67 | return maila; |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | } |
---|
72 | void AsetaOhjaimet() |
---|
73 | { |
---|
74 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
75 | Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); |
---|
76 | Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
77 | Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); |
---|
78 | Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
79 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); |
---|
80 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
81 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); |
---|
82 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
90 | { |
---|
91 | maila.Velocity = nopeus; |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|