source: 2010/31/vinekova/Tankkipeli1/Peli.cs @ 1611

Revision 1611, 7.1 KB checked in by tekrjant, 13 years ago (diff)
Line 
1using System;
2using Jypeli;
3using Jypeli.Widgets;
4using Jypeli.Assets;
5using Jypeli.Effects;
6using System.Collections.Generic;
7
8
9public class Tankkipeli : PhysicsGame
10{
11    PhysicsObject vasenReuna;
12    PhysicsObject alaReuna;
13    IntMeter kenttaLaskuri;
14    Tank tankki;
15    Tank tankki2;
16    PlasmaCannon plazma;
17    List<Label> valikonKohdat;
18    const int ruudunLeveys = 50;
19    const int ruudunKorkeus = 50;
20    DoubleMeter voimaMittari;
21
22
23
24    protected override void Begin()
25    {
26        Camera.ZoomToLevel();
27        IsFullScreen = true;
28        Valikko();
29        voimaMittari = new DoubleMeter(10);
30        voimaMittari.MaxValue = 10;
31        BarGauge voimaPalkki = new BarGauge(20, 150);
32        voimaPalkki.BindTo(voimaMittari);
33        Add(voimaPalkki);
34
35        voimaPalkki.X = (0.8 * Screen.RightSafe);
36        voimaPalkki.Y = (0.8 * Screen.TopSafe);
37        voimaPalkki.BarColor = Color.Green;
38        voimaPalkki.BorderColor = Color.White;
39        voimaPalkki.Angle = Angle.Degrees(90);
40
41        // Kun voima loppuu, kutsutaan VoimaLoppui-aliohjelmaa
42        voimaMittari.LowerLimit += VoimaLoppui;
43
44        Keyboard.Listen(Key.Space, ButtonState.Pressed, VahennaVoimia, "Vähennä pelaajan voimia");
45    }
46
47    void Valikko()
48    {
49        ClearAll();
50        valikonKohdat = new List<Label>();
51
52        Label kohta1 = new Label("Aloita peli");
53        kohta1.Position = new Vector(0, 40);
54        valikonKohdat.Add(kohta1);
55
56        Label kohta2 = new Label("Lopeta peli");
57        kohta2.Position = new Vector(0, -40);
58        valikonKohdat.Add(kohta2);
59
60        foreach (Label valikonKohta in valikonKohdat)
61        {
62            Add(valikonKohta);
63        }
64
65        Mouse.ListenOn(kohta1, MouseButton.Left, ButtonState.Pressed, AloitaPeli, null);
66        Mouse.ListenOn(kohta2, MouseButton.Left, ButtonState.Pressed, Lopeta, null);
67
68        Mouse.IsCursorVisible = true;
69        Mouse.ListenMovement(1.0, ValikossaLiikkuminen, null);
70        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Lopeta, "");
71    }
72
73    void VahennaVoimia()
74    {
75        voimaMittari.Value--;
76    }
77
78    void VoimaLoppui(double mittarinArvo)
79    {
80        MessageDisplay.Add("Voimat loppuivat, voi voi.");
81    }
82
83
84    void ValikossaLiikkuminen(AnalogState hiirenTila)
85    {
86        foreach (Label kohta in valikonKohdat)
87        {
88            if (Mouse.IsCursorOn(kohta))
89            {
90                kohta.TextColor = Color.Red;
91            }
92            else
93            {
94                kohta.TextColor = Color.White;
95            }
96
97        }
98    }
99
100    void AloitaPeli()
101    {
102        ClearAll();
103        SeuraavaKentta(1);
104        AsetaOhjaimet();
105        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Valikko, "Palaa valikkoon");
106    }
107
108    void Lopeta()
109    {
110        Exit();
111    }
112
113
114    void LuoKentta1()
115    {
116        Shape muoto = Shapes.FromImage(LoadImage("TankkiKenttä1"));
117        PhysicsObject maasto = PhysicsObject.CreateStaticObject(Level.Width, 300, muoto);
118        GameObject maastokuva = new GameObject(Level.Width, -300);
119        maastokuva.Image = LoadImage("TankkiKenttä1");
120        maasto.Add(maastokuva);
121        Add(maasto);
122        maasto.Y = 800;
123    }
124
125    void LuoKentta2()
126    {
127        Level.CreateGround(10, 100, 20, LoadImage("TankkiKenttä2"));
128    }
129
130    void LuoKentta3()
131    {
132        Level.CreateGround(10, 100, 20, LoadImage("TankkiKenttä3"));
133    }
134
135    void TankkiTormasi(PhysicsObject kissa, PhysicsObject kohde)
136    {
137        if (kohde == vasenReuna)
138        {
139            kenttaLaskuri.Value++;
140            SeuraavaKentta(kenttaLaskuri.Value);
141        }
142        if (kohde == alaReuna)
143        {
144            SeuraavaKentta(kenttaLaskuri.Value);
145        }
146
147    }
148
149   
150
151    void LuoTankki()
152    {
153        tankki = new Tank(100, 50);
154        tankki.Y = Level.Bottom + 50;
155        Add(tankki);
156
157        tankki.Cannon.Power.Value = 1000;
158        tankki.Cannon.CannonBallCollision = KuulaOsuu;
159
160        plazma = new PlasmaCannon(25, 15);
161        tankki.Add(plazma);
162
163        // tankki.Add(gannon);
164
165        tankki2 = new Tank(100, 50);
166        tankki2.Y = Level.Bottom + 50;
167        tankki2.X += 70;
168        Add(tankki2);
169
170        tankki2.Cannon.Power.Value = 1000;
171        tankki2.Cannon.CannonBallCollision = KuulaOsuu;
172    }
173
174    void AsetaOhjaimet()
175    {
176        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu");
177        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
178        Keyboard.Listen(Key.A, ButtonState.Down, Aja, "Liiku vasemmalle", tankki, 1.0);
179        Keyboard.Listen(Key.D, ButtonState.Down, Aja, "Liiku oikealle", tankki, -1.0);
180        Keyboard.Listen(Key.W, ButtonState.Down, KaannaPutkea, "Käännä putkea vastapäivään", tankki, Angle.Degrees(1));
181        Keyboard.Listen(Key.S, ButtonState.Down, KaannaPutkea, "Käännä putkea myötäpäivään", tankki, Angle.Degrees(-1));
182        Keyboard.Listen(Key.LeftControl, ButtonState.Down, Laukaus, "Ammu");
183
184
185        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu");
186        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
187        Keyboard.Listen(Key.J, ButtonState.Down, Aja, "Liiku vasemmalle", tankki2, 1.0);
188        Keyboard.Listen(Key.L, ButtonState.Down, Aja, "Liiku oikealle", tankki2, -1.0);
189        Keyboard.Listen(Key.I, ButtonState.Down, KaannaPutkea, "Käännä putkea vastapäivään", tankki2, Angle.Degrees(1));
190        Keyboard.Listen(Key.K, ButtonState.Down, KaannaPutkea, "Käännä putkea myötäpäivään", tankki2, Angle.Degrees(-1));
191        Keyboard.Listen(Key.RightControl, ButtonState.Down, Laukaus2, "Ammu");
192    }
193       
194
195
196    void Aja(Tank t, double vaanto)
197    {
198        t.Accelerate(vaanto);
199    }
200    void KaannaPutkea(Tank t, Angle kaanto)
201    {
202        t.Cannon.Angle += kaanto;
203    }
204
205    void KuulaOsuu(PhysicsObject kuula, PhysicsObject toinen)
206    {
207        kuula.Destroy();
208        Explosion rajahdys = new Explosion(40);
209        rajahdys.Position = kuula.Position;
210        Add(rajahdys);
211    }
212
213
214    void Laukaus()
215    {
216         plazma.Shoot();
217 
218         plazma.Use();
219
220        tankki.Cannon.Power.Value = 20000;
221        tankki.Cannon.Use();
222    }
223
224    void Laukaus2()
225    {
226         plazma.Shoot();
227
228         plazma.Use();
229
230        tankki2.Cannon.Power.Value = 20000;
231        tankki2.Cannon.Use();
232    }
233
234    void SeuraavaKentta(int kentanNro)
235    {
236        ClearAll();
237
238        kenttaLaskuri = new IntMeter(kentanNro);
239
240        vasenReuna = Level.CreateLeftBorder();
241        Level.CreateRightBorder();
242        alaReuna = Level.CreateBottomBorder();
243        Level.CreateTopBorder();
244
245        if (kenttaLaskuri.Value == 1) LuoKentta1();
246        else if (kenttaLaskuri.Value == 2) LuoKentta2();
247        else if (kenttaLaskuri.Value == 3) LuoKentta3();
248        else if (kenttaLaskuri.Value > 3) Exit();
249
250        Gravity = new Vector(1.0, -400);
251        LuoTankki();
252        Camera.ZoomToLevel();
253
254    }
255
256
257}
Note: See TracBrowser for help on using the repository browser.