source: 2011/26/JouniM/MatoPeli/MatoPeli/MatoPeli/Peli.cs @ 2407

Revision 2407, 8.1 KB checked in by jojumuho, 12 years ago (diff)

Talletus.

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Peli : PhysicsGame
10{
11    PhysicsObject Mato;
12    PhysicsObject Saalis;
13
14    DoubleMeter alaspainLaskuri;
15
16    Timer ajastin;
17    Timer sadeajastin;
18    Timer aikaLaskuri;
19    IntMeter pisteLaskuri;
20    int SaaliidenMaara = 0;
21    Image MadonKuva = LoadImage("Mato");
22    Image SaaliinKuva = LoadImage("Saalis");
23    Image GameOver = LoadImage("game over");
24    Image taustaKuva = LoadImage("taustakuva");
25
26    ScoreList topLista = new ScoreList(10, false, 0);
27
28
29
30    bool kuolematon = false;
31
32    public override void Begin()
33    {
34        // TODO: Kirjoita ohjelmakoodisi tähän
35
36        ajastin = new Timer();
37        ajastin.Interval = 0.1;
38        ajastin.Trigger += LiikutaMatoa;
39
40
41        sadeajastin = new Timer();
42        sadeajastin.Trigger += LuoVihollinen;
43
44        aikaLaskuri = new Timer();
45        aikaLaskuri.Interval = 0.1;
46        aikaLaskuri.Trigger += LaskeAlaspain;
47
48        MediaPlayer.Play("duck hunt");
49
50        if (DataStorage.Exists("pisteet.xml"))
51            topLista = DataStorage.Load<ScoreList>(topLista, "pisteet.xml");
52
53
54        Aloitapeli();
55
56    }
57
58    private void Aloitapeli()
59    {
60        sadeajastin.Interval = 1.1;
61        LuoKentta();
62        LuoMato();
63        AsetaOhjaimet();
64        SaaliidenMaara = 0;
65        LuoSaalis(20, 30);
66        LuoLaskuri();
67        LuoAikaLaskuri();
68        ajastin.Start();
69        sadeajastin.Start();
70        aikaLaskuri.Start();
71
72
73
74    }
75
76    void LuoKentta()
77    {
78        Level.CreateBorders();
79        Camera.ZoomToLevel();
80        Level.Background.Image = taustaKuva;
81        Level.Background.FitToLevel();
82        Level.Background.FitToLevel();
83    }
84
85    void LuoMato()
86    {
87        Mato = new PhysicsObject(30.0, 40.0);
88        Mato.Tag = "Mato";
89        Add(Mato);
90        Mato.Shape = Shape.Heart;
91        Mato.Image = MadonKuva;
92
93        AddCollisionHandler(Mato, KasitteleMadonTormays);
94
95    }
96
97    void KasitteleMadonTormays(PhysicsObject Mato, PhysicsObject kohde)
98    {
99
100        if (kohde.Tag.ToString() == "saalis")
101        {
102            kohde.Destroy();
103            SaalisDestroyed();
104           
105        }
106
107    }
108
109
110    void AsetaOhjaimet()
111    {
112        Mouse.IsCursorVisible = true;
113        Keyboard.Listen(Key.G, ButtonState.Down, TuleeKuolemattomaksi, null);
114        Keyboard.Listen(Key.G, ButtonState.Up, Kuolemattomuusloppuu, null);
115        Keyboard.Listen(Key.R, ButtonState.Released, uudestaan, "aloitus");
116
117
118    }
119
120
121    void TuleeKuolemattomaksi()
122    {
123        kuolematon = true;
124    }
125
126    void Kuolemattomuusloppuu()
127    {
128        kuolematon = false;
129    }
130
131    void KuunteleLiiketta(AnalogState hiirenTila)
132    {
133        Mato.X = Mouse.PositionOnWorld.X;
134        Mato.Y = Mouse.PositionOnWorld.Y;
135
136        Vector hiirenLiike = hiirenTila.MouseMovement;
137
138    }
139
140    void LiikutaMatoa(Timer sender)
141    {
142        Vector etaisyys = Mouse.PositionOnWorld - Mato.Position;
143        Mato.MoveTo(Mouse.PositionOnWorld, etaisyys.Magnitude * 1.5);
144        Mato.Angle = etaisyys.Angle + Angle.FromDegrees(270);
145
146    }
147
148    void LuoSaalis(double X, double Y)
149    {
150        Saalis = new PhysicsObject(30, 30);
151        Saalis.Tag = "saalis";
152        Saalis.Image = SaaliinKuva;
153        SaaliidenMaara++;
154        Add(Saalis);
155        Saalis.Shape = Shape.Star;
156        Saalis.X = RandomGen.NextDouble(Level.Left, Level.Right);
157        Saalis.Y = RandomGen.NextDouble(Level.Bottom, Level.Top);
158        //FollowerBrain karkuunSaalis = new FollowerBrain(Mato, -50);
159        //karkuunSaalis.Target = Mato;
160        //Saalis.Brain = karkuunSaalis;
161        Saalis.Hit(new Vector(RandomGen.NextDouble(-600, 600), RandomGen.NextDouble(-600, 600)));
162        AddCollisionHandler(Saalis, KasitteleSaaliinTormays);
163        //Saalis.Destroyed += new Action(Saalis_Destroyed);
164    }
165
166    void KasitteleSaaliinTormays(PhysicsObject saalis, PhysicsObject kohde)
167    {
168        if(kohde.Tag.ToString() != "saalis") saalis.Hit(-1/2*saalis.Position);
169    }
170
171    void SaalisDestroyed()
172    {
173        SaaliidenMaara--;
174        if (SaaliidenMaara < 10)
175        {
176            LuoSaalis(RandomGen.NextDouble(Level.Left, Level.Right), RandomGen.NextDouble(Level.Bottom, Level.Top));
177            LuoSaalis(RandomGen.NextDouble(Level.Left, Level.Right), RandomGen.NextDouble(Level.Bottom, Level.Top));
178        }
179        pisteLaskuri.Value = pisteLaskuri.Value + 1 ;
180        // KasvataMatoa();
181    }
182
183    void LuoLaskuri()
184    {
185        pisteLaskuri = new IntMeter(0);
186
187        Label pisteTeksti = new Label("Pisteitä: ");
188        pisteTeksti.X = Screen.Right - 160;
189        pisteTeksti.Y = Screen.Top - 100;
190        pisteTeksti.TextColor = Color.Red;
191        Add(pisteTeksti);
192
193        Label pisteNaytto = new Label();
194        pisteNaytto.X = Screen.Right - 110;
195        pisteNaytto.Y = Screen.Top - 100;
196        pisteNaytto.TextColor = Color.Red;
197
198        pisteNaytto.BindTo(pisteLaskuri);
199        Add(pisteNaytto);
200
201
202    }
203
204    void LuoVihollinen(Timer sender)
205    {
206        sender.Interval = sender.Interval - 0.019;
207
208        Vector suunta = new Vector(0, 1);
209
210        Vector position;
211        if (RandomGen.NextBool())
212        {
213            double x = RandomGen.NextDouble(Level.Left, Level.Right);
214            while (Math.Abs(Mato.X - x) <100 )
215            {
216                x = RandomGen.NextDouble(Level.Left, Level.Right);
217            }
218            position = new Vector(x, Level.Top);
219            suunta = new Vector(0.0, -1);
220        }
221
222        else
223        {
224        double y = RandomGen.NextDouble(Level.Bottom, Level.Top);
225            while (Math.Abs(Mato.Y - y) <100 )
226            {
227                y = RandomGen.NextDouble(Level.Bottom, Level.Top);
228            }
229            position = new Vector(Level.Left, y);
230            suunta = new Vector(1,0);
231        }
232
233       
234        PhysicsObject Vihollinen = new PhysicsObject(new RaySegment(position, suunta, 2000));
235        Vihollinen.MaximumLifetime = TimeSpan.FromSeconds(2.0);
236        Vihollinen.Color = Color.Yellow;
237        AddCollisionHandler(Vihollinen, KasitteleVihollisenTormays);
238        Add(Vihollinen);
239
240    }
241
242    void KasitteleVihollisenTormays(PhysicsObject Vihollinen, PhysicsObject kohde)
243    {
244        if (kohde.Tag.ToString() == "Mato" && kuolematon == false)
245        {
246            kohde.Destroy();
247            PeliLoppui();
248        }
249    }
250
251    void LuoAikaLaskuri()
252    {
253        alaspainLaskuri = new DoubleMeter(35);
254
255        Label aikaNaytto = new Label();
256        aikaNaytto.TextColor = Color.White;
257        aikaNaytto.DecimalPlaces = 1;
258        aikaNaytto.BindTo(alaspainLaskuri);
259        aikaNaytto.X = 0;
260        aikaNaytto.Y = Screen.Top - 100;
261        Add(aikaNaytto);
262    }
263
264    void LaskeAlaspain(Timer timer)
265    {
266        alaspainLaskuri.Value -= 0.1;
267
268        if (alaspainLaskuri.Value <= 0)
269        {
270            timer.Stop();
271            PeliLoppui();
272
273            // täydennä mitä tapahtuu, kun aika loppuu
274        }
275    }
276
277
278    void uudestaan()
279    {
280        ClearAll();
281
282        Aloitapeli();
283    }
284
285
286    void PeliLoppui()
287    {
288          MessageDisplay.Add("Peli Loppui");
289          ajastin.Stop();
290            ClearAll();
291            Keyboard.Listen(Key.R, ButtonState.Released, uudestaan, "aloitus");
292            Widget lopputeksti = new Widget(Screen.Width, Screen.Height);
293            lopputeksti.Image = GameOver;
294            Add(lopputeksti);
295
296            HighScoreWindow topIkkuna = new HighScoreWindow("Pelin nimi tai jotain",
297                                 "Parhaat pisteet",
298                                 "Onneksi olkoon, pääsit listalle pisteillä %p! Syötä nimesi:",
299                                 topLista, pisteLaskuri.Value);
300            topIkkuna.Closed += TallennaPisteet;
301            Add(topIkkuna);
302
303    }
304
305    void TallennaPisteet(Window sender)
306    {
307        DataStorage.Save<ScoreList>(topLista, "pisteet.xml");
308    }
309
310}
Note: See TracBrowser for help on using the repository browser.