1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.ScreenObjects; |
---|
4 | using Jypeli.Assets; |
---|
5 | |
---|
6 | namespace Pong |
---|
7 | { |
---|
8 | public class Peli : PhysicsGame |
---|
9 | { |
---|
10 | PhysicsObject pallo; |
---|
11 | PhysicsObject maila1; |
---|
12 | PhysicsObject maila2; |
---|
13 | protected override void Begin() |
---|
14 | { |
---|
15 | LuoKentta(); |
---|
16 | AloitaPeli(); |
---|
17 | AsetaOhjaimet(); |
---|
18 | } |
---|
19 | void LuoKentta() |
---|
20 | { |
---|
21 | pallo = new PhysicsObject(40.0, 40.0); |
---|
22 | pallo.Shape = Shapes.Circle; |
---|
23 | pallo.Restitution = 1; |
---|
24 | pallo.X = -200; |
---|
25 | pallo.Y = 0; |
---|
26 | Add(pallo); |
---|
27 | |
---|
28 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
29 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
30 | |
---|
31 | |
---|
32 | Level.CreateBorders(1,false); |
---|
33 | Level.BackgroundColor = Color.Black; |
---|
34 | Camera.ZoomToLevel(); |
---|
35 | } |
---|
36 | PhysicsObject LuoMaila(double x, double y) |
---|
37 | { |
---|
38 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
39 | maila.Shape = Shapes.Rectangle; |
---|
40 | maila.X = x; |
---|
41 | maila.Y = y; |
---|
42 | maila.Restitution = 1.0; |
---|
43 | Add(maila); |
---|
44 | return maila; |
---|
45 | } |
---|
46 | void AsetaOhjaimet() |
---|
47 | { |
---|
48 | Keyboard.Listen(Key.W, ButtonState.Down, LiikutaMailaaYlos, "Pelaaja 1: Liikuta mailaa ylös", maila1); |
---|
49 | Keyboard.Listen(Key.W, ButtonState.Released, PysaytaMaila, null, maila1); |
---|
50 | Keyboard.Listen(Key.S, ButtonState.Down, LiikutaMailaaAlas, "Pelaaja 1: Liikuta mailaa alas", maila1); |
---|
51 | Keyboard.Listen(Key.S, ButtonState.Released, PysaytaMaila, null, maila1); |
---|
52 | |
---|
53 | Keyboard.Listen(Key.Up, ButtonState.Down, LiikutaMailaaYlos, "Pelaaja 2: Liikuta mailaa ylös", maila2); |
---|
54 | Keyboard.Listen(Key.Up, ButtonState.Released, PysaytaMaila, null, maila2); |
---|
55 | Keyboard.Listen(Key.Down, ButtonState.Down, LiikutaMailaaAlas, "Pelaaja 2: Liikuta mailaa alas", maila2); |
---|
56 | Keyboard.Listen(Key.Down, ButtonState.Released, PysaytaMaila, null, maila2); |
---|
57 | |
---|
58 | Keyboard.Listen(Key.F1, ButtonState.Down, ShowControlHelp, "Näytä ohjeet"); |
---|
59 | Keyboard.Listen(Key.F2, ButtonState.Down, AloitaPeliUudelleen, "Aloita peli uudelleen"); |
---|
60 | Keyboard.Listen(Key.Escape, ButtonState.Down, Exit, "Poistu"); |
---|
61 | } |
---|
62 | void LiikutaMailaaYlos(PhysicsObject maila) |
---|
63 | { |
---|
64 | if (maila.Y >= Level.Top - 50) |
---|
65 | { |
---|
66 | maila.Velocity = Vector.Zero; |
---|
67 | return; |
---|
68 | } |
---|
69 | Vector nopeus = new Vector(0, 200); |
---|
70 | maila.Velocity = nopeus; |
---|
71 | } |
---|
72 | void LiikutaMailaaAlas(PhysicsObject maila) |
---|
73 | { |
---|
74 | if (maila.Y <= Level.Bottom + 50) |
---|
75 | { |
---|
76 | maila.Velocity = Vector.Zero; |
---|
77 | return; |
---|
78 | } |
---|
79 | Vector nopeus = new Vector(0, -200); |
---|
80 | maila.Velocity = nopeus; |
---|
81 | } |
---|
82 | |
---|
83 | void PysaytaMaila(PhysicsObject maila) |
---|
84 | { |
---|
85 | maila.Velocity = Vector.Zero; |
---|
86 | } |
---|
87 | |
---|
88 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
89 | { |
---|
90 | if ((nopeus.Y < 0) && (maila.Y < Level.Bottom)) |
---|
91 | { |
---|
92 | maila.Velocity = Vector.Zero; |
---|
93 | return; |
---|
94 | } |
---|
95 | if ((nopeus.Y > 0) && (maila.Y > Level.Top)) |
---|
96 | { |
---|
97 | maila.Velocity = Vector.Zero; |
---|
98 | return; |
---|
99 | } |
---|
100 | |
---|
101 | maila.Velocity = nopeus; |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | void AloitaPeli() |
---|
106 | { |
---|
107 | Vector impulssi = new Vector(500.0, 0.0); |
---|
108 | pallo.Hit(impulssi); |
---|
109 | } |
---|
110 | void AloitaPeliUudelleen() |
---|
111 | { |
---|
112 | pallo.Velocity = Vector.Zero; |
---|
113 | pallo.X = 0; |
---|
114 | pallo.Y = 0; |
---|
115 | Vector impulssi = new Vector(500.0, 0.0); |
---|
116 | pallo.Hit(impulssi); |
---|
117 | } |
---|
118 | |
---|
119 | } |
---|
120 | } |
---|