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 Bong : PhysicsGame |
---|
10 | { |
---|
11 | |
---|
12 | Vector nopeusYlos = new Vector (0, 400 ); |
---|
13 | Vector nopeusAlas = new Vector(0, -400); |
---|
14 | PhysicsObject pelaaja; |
---|
15 | PhysicsObject pelaaja2; |
---|
16 | |
---|
17 | PhysicsObject pallo = new PhysicsObject(40, 40); |
---|
18 | |
---|
19 | IntMeter pelaajan1Pisteet; |
---|
20 | IntMeter pelaajan2Pisteet; |
---|
21 | |
---|
22 | |
---|
23 | public override void Begin() |
---|
24 | { |
---|
25 | |
---|
26 | |
---|
27 | |
---|
28 | LuoKentta(); |
---|
29 | AloitaPeli(); |
---|
30 | AsetaOhjaimet(); |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | } |
---|
41 | void LuoKentta() |
---|
42 | { |
---|
43 | pelaaja = LuoMaila(-500, 0, Color.Violet); |
---|
44 | pelaaja2 = LuoMaila(500, 0, Color.Lime); |
---|
45 | |
---|
46 | Add(pallo); |
---|
47 | pallo.Shape = Shape.Circle; |
---|
48 | pallo.Color = Color.OrangeRed; |
---|
49 | pallo.Restitution = 1; |
---|
50 | // pallo.Gravity (200) ; |
---|
51 | |
---|
52 | Level.CreateBorders(1, false); |
---|
53 | |
---|
54 | Level.BackgroundColor = Color.Cyan; |
---|
55 | Camera.ZoomToLevel(); |
---|
56 | |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | void AloitaPeli() |
---|
61 | { |
---|
62 | Vector impulssi = new Vector(500, 300); |
---|
63 | pallo.Hit(impulssi); |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | } |
---|
69 | |
---|
70 | PhysicsObject LuoMaila(double x, double y, Color vari) |
---|
71 | { |
---|
72 | PhysicsObject pelaaja = PhysicsObject.CreateStaticObject (15, 70); |
---|
73 | Add(pelaaja); |
---|
74 | pelaaja.Shape = Shape.Ellipse; |
---|
75 | pelaaja.Color = vari; |
---|
76 | pelaaja.X = x; |
---|
77 | pelaaja.Y = y; |
---|
78 | pelaaja.Restitution = 1; |
---|
79 | return pelaaja; |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | void AsetaOhjaimet() |
---|
86 | { |
---|
87 | Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, null, pelaaja, nopeusAlas); |
---|
88 | Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, pelaaja, Vector.Zero); |
---|
89 | Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, null, pelaaja, nopeusYlos); |
---|
90 | Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, pelaaja, Vector.Zero); |
---|
91 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, null, pelaaja2, nopeusYlos); |
---|
92 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, pelaaja2, Vector.Zero); |
---|
93 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, null, pelaaja2, nopeusAlas); |
---|
94 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, pelaaja2, Vector.Zero); |
---|
95 | |
---|
96 | |
---|
97 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | void AsetaNopeus(PhysicsObject pelaaja,Vector nopeus ) |
---|
102 | { |
---|
103 | pelaaja.Velocity = nopeus; |
---|
104 | |
---|
105 | if ( (nopeus.Y > 0 ) && (pelaaja.Top > Level.Top) ) |
---|
106 | { |
---|
107 | pelaaja.Velocity = Vector.Zero; |
---|
108 | return; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | if ((nopeus.Y < 0) && (pelaaja.Bottom < Level.Bottom)) |
---|
113 | { |
---|
114 | pelaaja.Velocity = Vector.Zero; |
---|
115 | return; |
---|
116 | } |
---|
117 | |
---|
118 | } |
---|
119 | void LisaaLaskurit() |
---|
120 | { |
---|
121 | pelaajan1Pisteet = LuoPisteLaskuri ( Screen.Left + 100.0, Screen.Top - 100.0 ); |
---|
122 | pelaajan2Pisteet = LuoPisteLaskuri ( Screen.Right - 100.0, Screen.Top - 100.0 ); |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | } |
---|
128 | IntMeter LuoPistelaskuri (double x, double y) |
---|
129 | { |
---|
130 | IntMeter laskuri = new IntMeter(0); |
---|
131 | |
---|
132 | laskuri.MaxValue = 10; |
---|
133 | Label naytto = new Label (0); |
---|
134 | naytto.BindTo(laskuri); |
---|
135 | naytto.X = x; |
---|
136 | naytto.Y = y; |
---|
137 | naytto.TextColor = Color.White; |
---|
138 | naytto.BorderColor = Level.BackgroundColor; |
---|
139 | naytto.Color = Level.BackgroundColor; |
---|
140 | Add(naytto); |
---|
141 | return laskuri; |
---|
142 | |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | } |
---|
152 | |
---|