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 | |
---|
12 | Vector impls = new Vector(30.0, 30.0); |
---|
13 | |
---|
14 | Vector nYlos = new Vector(0, 200); |
---|
15 | Vector aYlos = new Vector(0, -200); |
---|
16 | Vector vasen = new Vector(-200, 0); |
---|
17 | Vector oikea = new Vector(200, 0); |
---|
18 | |
---|
19 | PhysicsObject pallo = new PhysicsObject(25, 25); |
---|
20 | PhysicsObject oMaila; |
---|
21 | PhysicsObject vMaila; |
---|
22 | |
---|
23 | public override void Begin() |
---|
24 | { |
---|
25 | Start(); |
---|
26 | asetaOhj(); |
---|
27 | } |
---|
28 | |
---|
29 | void lKentta() |
---|
30 | { |
---|
31 | pallo.Shape = Shape.Circle; |
---|
32 | pallo.Color = Color.Black; |
---|
33 | pallo.X = -200; |
---|
34 | pallo.Y = 0; |
---|
35 | pallo.Restitution = 1.0; |
---|
36 | Add(pallo); |
---|
37 | |
---|
38 | vMaila = luoMaila(Level.Left + 20.0, 0.0); |
---|
39 | oMaila = luoMaila(Level.Right - 20.0, 0.0); |
---|
40 | |
---|
41 | //Turhat kamat |
---|
42 | Level.BackgroundColor = Color.White; |
---|
43 | Level.CreateBorders(1.0, false); |
---|
44 | Camera.ZoomToLevel(); |
---|
45 | } |
---|
46 | |
---|
47 | PhysicsObject luoMaila(double x, double y) |
---|
48 | { |
---|
49 | PhysicsObject maila = new PhysicsObject(20, 50); |
---|
50 | maila.Shape = Shape.Rectangle; |
---|
51 | maila.X = x; |
---|
52 | maila.Y = y; |
---|
53 | maila.Restitution = 1.0; |
---|
54 | maila.Color = Color.Green; |
---|
55 | Add(maila); |
---|
56 | return maila; |
---|
57 | } |
---|
58 | |
---|
59 | void Start() |
---|
60 | { |
---|
61 | lKentta(); |
---|
62 | pallo.Hit(impls); |
---|
63 | } |
---|
64 | |
---|
65 | void asetaOhj() |
---|
66 | { |
---|
67 | Keyboard.Listen(Key.W, ButtonState.Down, mailanNopeus, "", vMaila, nYlos); |
---|
68 | Keyboard.Listen(Key.W, ButtonState.Released, mailanNopeus, "", vMaila, Vector.Zero); |
---|
69 | |
---|
70 | Keyboard.Listen(Key.S, ButtonState.Down, mailanNopeus, "", vMaila, aYlos); |
---|
71 | Keyboard.Listen(Key.S, ButtonState.Released, mailanNopeus, "", vMaila, Vector.Zero); |
---|
72 | |
---|
73 | Keyboard.Listen(Key.A, ButtonState.Down, mailanNopeus, "", vMaila, vasen); |
---|
74 | Keyboard.Listen(Key.A, ButtonState.Released, mailanNopeus, "", vMaila, Vector.Zero); |
---|
75 | |
---|
76 | Keyboard.Listen(Key.D, ButtonState.Down, mailanNopeus, "", vMaila, oikea); |
---|
77 | Keyboard.Listen(Key.D, ButtonState.Released, mailanNopeus, "", vMaila, Vector.Zero); |
---|
78 | |
---|
79 | Keyboard.Listen(Key.Up, ButtonState.Down, mailanNopeus, "", oMaila, nYlos); |
---|
80 | Keyboard.Listen(Key.Up, ButtonState.Released, mailanNopeus, "", oMaila, Vector.Zero); |
---|
81 | |
---|
82 | Keyboard.Listen(Key.Down, ButtonState.Down, mailanNopeus, "", oMaila, aYlos); |
---|
83 | Keyboard.Listen(Key.Down, ButtonState.Released, mailanNopeus, "", vMaila, Vector.Zero); |
---|
84 | |
---|
85 | Keyboard.Listen(Key.Left, ButtonState.Down, mailanNopeus, "", oMaila, vasen); |
---|
86 | Keyboard.Listen(Key.Left, ButtonState.Released, mailanNopeus, "", oMaila, Vector.Zero); |
---|
87 | |
---|
88 | Keyboard.Listen(Key.Right, ButtonState.Down, mailanNopeus, "", oMaila, oikea); |
---|
89 | Keyboard.Listen(Key.Right, ButtonState.Released, mailanNopeus, "", oMaila, Vector.Zero); |
---|
90 | |
---|
91 | } |
---|
92 | |
---|
93 | void mailanNopeus(PhysicsObject maila, Vector nopeus) |
---|
94 | { |
---|
95 | maila.Velocity = nopeus; |
---|
96 | } |
---|
97 | |
---|
98 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
99 | { |
---|
100 | if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) |
---|
101 | { |
---|
102 | maila.Velocity = Vector.Zero; |
---|
103 | return; |
---|
104 | } |
---|
105 | if ((nopeus.Y > 0) && (maila.Top > Level.Top)) |
---|
106 | { |
---|
107 | maila.Velocity = Vector.Zero; |
---|
108 | return; |
---|
109 | } |
---|
110 | |
---|
111 | if((nopeus.X > 0) && (maila.Left > Level.Left)) |
---|
112 | { |
---|
113 | maila.Velocity = Vector.Zero; |
---|
114 | return; |
---|
115 | } |
---|
116 | |
---|
117 | if ((nopeus.X < 0) && (maila.Right > Level.Right)) |
---|
118 | { |
---|
119 | maila.Velocity = Vector.Zero; |
---|
120 | return; |
---|
121 | } |
---|
122 | |
---|
123 | maila.Velocity = nopeus; |
---|
124 | } |
---|
125 | |
---|
126 | } |
---|
127 | |
---|