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