source: 2014/30/OonaH/GoldenMaknae2/GoldenMaknae2/GoldenMaknae2/GoldenMaknae2.cs @ 5631

Revision 5631, 6.7 KB checked in by oomahutt, 9 years ago (diff)

Peliin lisätty alkuvalikko ja hiottu pelinäkymää.

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class GoldenMaknae2 : PhysicsGame
10{
11
12    #region Olioiden nimeaminen
13    PlatformCharacter Kookie;
14    PhysicsObject reuna;
15    PhysicsObject tavarat;
16    Label pisteNaytto;
17    IntMeter pisteLaskuri;
18    Label aloitusNappi;
19    List<Label> valikot;
20
21
22    #endregion
23    #region kuvien lataaminen
24    Image KookieKuva = LoadImage("Kookie");
25    Image LetterKuva = LoadImage("Letter");
26    Image RapmonKuva = LoadImage("Rapmon");
27    #endregion
28    #region nopeudet
29    Vector nopeusOikealle = new Vector(300, 0);
30    Vector nopeusVasemmalle = new Vector(-300, 0);
31    #endregion
32
33    public override void Begin()
34    {
35        Valikko();
36        Ajastin();
37        LuoPisteLaskuri();
38        LuoKentta();
39        AsetaOhjaimet();
40       
41    }
42
43    void Valikko()
44    {
45        ClearAll();
46
47        valikot = new List<Label>();
48        aloitusNappi = new Label("Start");
49        aloitusNappi.Position = new Vector(0, 100);
50        valikot.Add(aloitusNappi);
51
52        foreach (Label valikko in valikot)
53        {
54            Add(valikko);
55        }
56
57        Mouse.ListenOn(aloitusNappi, MouseButton.Left, ButtonState.Pressed, AloitaPeli, null);
58    }
59
60    void ValikossaLiikkuminen(AnalogState hiiri)
61    {
62        Mouse.ListenMovement(1.0, ValikossaLiikkuminen, null);
63        //foreach (Label kohta in valikonKohdat)
64
65        foreach (Label kohta in valikot)
66        {
67            if (Mouse.IsCursorOn(kohta))
68            {
69                kohta.TextColor = Color.HotPink;
70            }
71            else
72            {
73                kohta.TextColor = Color.Black;
74            }
75        }
76    }
77    void AloitaPeli()
78    {
79
80    }
81    void Ajastin()
82    {
83        Timer Ajastin = new Timer();
84        Ajastin.Interval = 3;
85        Ajastin.Timeout += LuoHahmot;
86        Ajastin.Start();
87    }
88    void LuoHahmot()
89    {
90        bool tuleekoKirje = RandomGen.NextBool();
91        if (tuleekoKirje == true)
92        {
93            LuoHahmoja(1, LetterKuva, "Letter");
94        }
95        else
96        {
97            LuoHahmoja(1, RapmonKuva, "Hyung");
98        }
99
100    }
101
102    void LuoHahmoja(int maara, Image Kuva, string tag)
103    {
104        int i = 0;
105        while (i < maara)
106        {
107            double x = RandomGen.NextDouble(Level.Left + 10, Level.Right - 10);
108            tavarat = LuoTavarat(x, Level.Top - 20, 20.0);
109            tavarat.Tag = tag;
110            tavarat.Image = Kuva;
111            tavarat.Velocity = new Vector(0, -200);
112            tavarat.CollisionIgnoreGroup = 1;
113            tavarat.IgnoresGravity = true;
114            Add(tavarat);
115
116            i++;
117        }
118    }
119
120    PhysicsObject LuoTavarat(double x, double y, double sade)
121    {
122        PhysicsObject tavara = new PhysicsObject(sade * 3.0, sade * 3.0, Shape.Circle);
123        tavara.Position = new Vector(x, y);
124        return tavara;
125    }
126
127    void LuoKentta()
128    {
129        SetWindowSize(600,800);
130        Level.Size = new Vector(600, 800);
131
132        TileMap alaReuna = TileMap.FromLevelAsset("Kentta");
133        alaReuna.SetTileMethod('#', LuoReunaa);
134        alaReuna.SetTileMethod('-', LuoKookie);
135        alaReuna.Execute(20.0,20.0);
136        Camera.ZoomToLevel();
137
138        IsMouseVisible = true;
139
140        PhysicsObject oikeaReuna = Level.CreateRightBorder();
141        oikeaReuna.Restitution = 1.0;
142
143        PhysicsObject vasenReuna = Level.CreateLeftBorder();
144        vasenReuna.Restitution = 1.0;
145    }
146
147    void LuoReunaa(Vector paikka, double leveys, double korkeus)
148    {
149        reuna = PhysicsObject.CreateStaticObject(leveys, korkeus);
150        reuna.Position = paikka;
151        reuna.Color = Color.Salmon;
152        reuna.Tag = "reuna";
153        Add(reuna);
154
155        AddCollisionHandler(reuna, "Letter", Osuma);
156        AddCollisionHandler(reuna, "Hyung", ToinenOsuma);
157    }
158
159    void ToinenOsuma(PhysicsObject reuna, PhysicsObject Hyung)
160    {
161        Hyung.Destroy();
162    }
163
164    void LuoKookie(Vector paikka, double leveys, double korkeus)
165    {
166       
167        Kookie = new PlatformCharacter(70.0, 70.0);
168        Kookie.Position = paikka;
169        Kookie.Image = KookieKuva;
170        Kookie.Restitution = 1.0;
171        Gravity = new Vector(0,-5000);
172
173        Add(Kookie);
174
175        AddCollisionHandler(Kookie, "Letter", PisteidenSaanti);
176        AddCollisionHandler(Kookie, "Hyung", Kuolema);
177
178    }
179
180    void Osuma(PhysicsObject reuna, PhysicsObject osuneetKirjeet)
181    {
182        osuneetKirjeet.Velocity = Vector.Zero;
183
184    }
185
186    void LuoPisteLaskuri()
187    {
188        pisteLaskuri = new IntMeter(0, -1000, 1000);
189
190        pisteNaytto = new Label();
191        pisteNaytto.X = 0;
192        pisteNaytto.Y = 200;
193        pisteNaytto.TextColor = Color.Black;
194        pisteNaytto.Color = Color.Transparent;
195        pisteNaytto.BindTo(pisteLaskuri);
196        pisteNaytto.IntFormatString = "Points: {0:D1}";
197
198        Add(pisteNaytto);
199    }
200
201    void AsetaOhjaimet()
202    {
203        Keyboard.Listen(Key.Right, ButtonState.Down, AsetaNopeus, "Oikealle.", Kookie, nopeusOikealle);
204        Keyboard.Listen(Key.Right, ButtonState.Released, AsetaNopeus, null, Kookie, Vector.Zero);
205
206        Keyboard.Listen(Key.Left, ButtonState.Down, AsetaNopeus, "Vasemmalle.", Kookie, nopeusVasemmalle);
207        Keyboard.Listen(Key.Left, ButtonState.Released, AsetaNopeus, null, Kookie, Vector.Zero);
208
209        Keyboard.Listen(Key.Space, ButtonState.Pressed, Hyppaa, "Hyppy");
210
211
212        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
213    }
214
215    void Hyppaa()
216    {
217        Kookie.Jump(2500);
218    }
219
220    void AsetaNopeus(PhysicsObject hahmo, Vector nopeus)
221    {
222        if ((nopeus.X > 0) && (hahmo.Right > Level.Right))
223        {
224            hahmo.Velocity = Vector.Zero;
225            return;
226        }
227
228        if ((nopeus.X < 0) && (hahmo.Left < Level.Left))
229        {
230            hahmo.Velocity = Vector.Zero;
231            return;
232        }
233
234        hahmo.Velocity = nopeus;
235    }
236
237    #region Kuolema
238    void Kuolema(PhysicsObject Kookie, PhysicsObject kohde)
239    {
240        MessageDisplay.Add("Hävisit");
241        Explosion rajahdys = new Explosion(100);
242        rajahdys.Position = kohde.Position;
243        Add(rajahdys);
244        kohde.Destroy();
245        Timer.SingleShot(1, Tuhoa);
246    }
247
248    void Tuhoa()
249    {
250        Kookie.Destroy();
251 
252    }
253    #endregion
254
255    void PisteidenSaanti(PhysicsObject Kookie, PhysicsObject kohde)
256    {
257        kohde.Destroy();
258        pisteLaskuri.Value = pisteLaskuri.Value + 10;
259    }
260   
261}
Note: See TracBrowser for help on using the repository browser.