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 | |
---|
14 | protected override void Begin() |
---|
15 | { |
---|
16 | LuoKentta(); |
---|
17 | AloitaPeli(); |
---|
18 | AsetaOhjaimet(); |
---|
19 | |
---|
20 | } |
---|
21 | |
---|
22 | void AloitaPeli() |
---|
23 | { |
---|
24 | Vector impulssi = new Vector(1500.0, 1000.0); |
---|
25 | pallo.Hit(impulssi); |
---|
26 | } |
---|
27 | |
---|
28 | void AsetaOhjaimet() |
---|
29 | { |
---|
30 | Keyboard.Listen(Key.A, ButtonState.Pressed, LiikutaMailaaYlos, "Pelaaja 1 liikuttaa mailaa ylös", maila1); |
---|
31 | Keyboard.Listen(Key.A, ButtonState.Released, PysaytaMaila, null, maila1); |
---|
32 | Keyboard.Listen(Key.Z, ButtonState.Pressed, LiikutaMailaaAlas, "Pelaaja 1 liikuttaa mailaa alas", maila1); |
---|
33 | Keyboard.Listen(Key.Z, ButtonState.Released, PysaytaMaila, null, maila1); |
---|
34 | Keyboard.Listen(Key.Up, ButtonState.Pressed, LiikutaMailaaYlos, "Pelaaja 2 liikuttaa mailaa ylös", maila2); |
---|
35 | Keyboard.Listen(Key.Up, ButtonState.Released, PysaytaMaila, null, maila2); |
---|
36 | Keyboard.Listen(Key.Down, ButtonState.Pressed, LiikutaMailaaAlas, "Pelaaja 2 liikuttaa mailaa alas", maila2); |
---|
37 | Keyboard.Listen(Key.Down, ButtonState.Released, PysaytaMaila, null, maila2); |
---|
38 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
39 | |
---|
40 | ControllerOne.Listen(Button.RightTrigger, ButtonState.Pressed, LiikutaMailaaYlos, "Pelaaja 1 liikuttaa mailaa ylös", maila1); |
---|
41 | ControllerOne.Listen(Button.RightTrigger, ButtonState.Released, PysaytaMaila, null, maila1); |
---|
42 | ControllerOne.Listen(Button.LeftTrigger, ButtonState.Pressed, LiikutaMailaaAlas, "Pelaaja 1 liikuttaa mailaa alas", maila1); |
---|
43 | ControllerOne.Listen(Button.LeftTrigger, ButtonState.Released, PysaytaMaila, null, maila1); |
---|
44 | ControllerTwo.Listen(Button.DPadUp, ButtonState.Pressed, LiikutaMailaaYlos, "Pelaaja 2 liikuttaa mailaa ylös", maila2); |
---|
45 | ControllerTwo.Listen(Button.DPadUp, ButtonState.Released, PysaytaMaila, null, maila2); |
---|
46 | ControllerTwo.Listen(Button.DPadDown, ButtonState.Pressed, LiikutaMailaaAlas, "Pelaaja 2 liikuttaa mailaa alas", maila2); |
---|
47 | ControllerTwo.Listen(Button.DPadDown, ButtonState.Released, PysaytaMaila, null, maila2); |
---|
48 | |
---|
49 | |
---|
50 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
51 | |
---|
52 | } |
---|
53 | |
---|
54 | void LiikutaMailaaYlos(PhysicsObject maila) |
---|
55 | { |
---|
56 | Vector nopeus = new Vector(0, 1500); |
---|
57 | maila.Velocity = nopeus; |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | } |
---|
63 | void PysaytaMaila(PhysicsObject maila) |
---|
64 | { |
---|
65 | Vector nopeus = new Vector(0, 0); |
---|
66 | maila.Velocity = nopeus; |
---|
67 | |
---|
68 | } |
---|
69 | void LiikutaMailaaAlas(PhysicsObject maila) |
---|
70 | { |
---|
71 | Vector nopeus = new Vector(0, -1500); |
---|
72 | maila.Velocity = nopeus; |
---|
73 | |
---|
74 | |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | void LuoKentta() |
---|
80 | { |
---|
81 | pallo = new PhysicsObject(40.0, 40.0); |
---|
82 | Add(pallo); |
---|
83 | pallo.Shape = Shapes.Circle; |
---|
84 | pallo.X = -200.0; |
---|
85 | pallo.Y = 1.0; |
---|
86 | |
---|
87 | Level.CreateBorders(1.0, false); |
---|
88 | |
---|
89 | pallo.Restitution = 1.05; |
---|
90 | pallo.AngularDamping = 0.8; |
---|
91 | pallo.KineticFriction = 0.0; |
---|
92 | pallo.Color = Color.Black; |
---|
93 | |
---|
94 | Level.BackgroundColor = Color.Pink; |
---|
95 | |
---|
96 | Camera.ZoomToLevel(); |
---|
97 | |
---|
98 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
99 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
100 | } |
---|
101 | PhysicsObject LuoMaila(double x, double y) |
---|
102 | { |
---|
103 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
104 | maila.Shape = Shapes.Rectangle; |
---|
105 | maila.X = x; |
---|
106 | maila.Y = y; |
---|
107 | maila.Color = Color.Beige; |
---|
108 | maila.Restitution = 1.0; |
---|
109 | Add(maila); |
---|
110 | return maila; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | } |
---|
117 | |
---|
118 | } |
---|
119 | |
---|