source: 2012/27/MarkusK/projekti/Kouura/Kouura/Kouura/Kouura.cs @ 3671

Revision 3671, 6.5 KB checked in by maolkuus, 11 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 Kouura : PhysicsGame
10{
11    const int ruudunleveys = 1;
12    const int ruudunkorkeus = 1;
13
14   
15    IntMeter Paukut;
16
17    PhysicsObject Pelaaja;
18
19    Vector NopeusYlos = new Vector(0, 30);
20    Vector NopeusAlas = new Vector(0, -30);
21    Vector NopeusVasen = new Vector(-30, 0);
22    Vector NopeusOikea = new Vector(30, 0);
23
24    Image taustaKuva = LoadImage("Hypnotoad");
25
26    public override void Begin()
27    {
28
29       
30        LuoKentta();
31        Lisaalaskuri();
32        LataaOhjaimet();
33
34        // TODO: Kirjoita ohjelmakoodisi tähän
35
36    }
37    void LuoKentta()
38    {
39        ColorTileMap ruudut = ColorTileMap.FromLevelAsset("Kentta");
40        ruudut.SetTileMethod(Color.Black, LuoPelaaja);
41        ruudut.SetTileMethod(Color.DarkBlue, LuoSeina);
42        ruudut.SetTileMethod(Color.White, LuoItem);
43        ruudut.SetTileMethod(Color.Yellow, LuoTrigger);
44        ruudut.SetTileMethod(Color.Green, LuoMaali);
45        ruudut.Execute(ruudunkorkeus, ruudunleveys);
46
47       
48
49        PhysicsObject vasenReuna = Level.CreateLeftBorder();
50        vasenReuna.Restitution = 1.0;
51        vasenReuna.IsVisible = false;
52
53        PhysicsObject oikeaReuna = Level.CreateRightBorder();
54        oikeaReuna.Restitution = 1.0;
55        oikeaReuna.IsVisible = false;
56
57        PhysicsObject ylaReuna = Level.CreateTopBorder();
58        ylaReuna.Restitution = 2.0;
59        ylaReuna.IsVisible = false;
60
61        PhysicsObject alaReuna = Level.CreateBottomBorder();
62        alaReuna.Restitution = 2.0;
63        alaReuna.IsVisible = false;
64
65
66        Level.Background.Image = taustaKuva;
67        Camera.ZoomToLevel();
68    }
69
70    void LuoPelaaja(Vector paikka, double leveys, double korkeus)
71    {
72        Pelaaja = new PhysicsObject(1, 1);
73        Pelaaja.Color = Color.Blue;
74        Pelaaja.Shape = Shape.Hexagon;
75        Pelaaja.Position = paikka;
76        Pelaaja.Mass = 10;
77        AddCollisionHandler(Pelaaja, "Sydän", Pistelisa);
78        AddCollisionHandler(Pelaaja, "Miinus", PisteMiinus);
79        AddCollisionHandler(Pelaaja, "Maali", Maali);
80
81
82        Add(Pelaaja);
83
84    }
85    void LuoSeina(Vector paikka, double leveys, double korkeus)
86    {
87        PhysicsObject Seina = PhysicsObject.CreateStaticObject(1, 1);
88        Seina.Position = paikka;
89        Seina.Shape = Shape.Rectangle;
90        Seina.Color = Color.MediumVioletRed;
91       
92        Add(Seina);
93    }
94    void LuoMaali(Vector paikka, double leveys, double korkeus)
95    {
96        PhysicsObject Maali = PhysicsObject.CreateStaticObject(1, 1);
97        Maali.Position = paikka;
98        Maali.Shape = Shape.Rectangle;
99        Maali.Color = Color.Red;
100        Maali.Tag = "Maali";
101       
102        Add(Maali);
103    }
104
105    void LuoItem(Vector paikka, double leveys, double korkeys)
106    {
107        PhysicsObject Item = new PhysicsObject(1, 1);
108        Item.Position = paikka;
109        Item.Mass = 0.005;
110        Item.Shape = Shape.Heart;
111        Item.Restitution = 1.0;
112        Item.Image = null;
113
114        Item.Tag = "Sydän";
115        Add(Item, 1);
116
117    }
118
119    void LuoTrigger(Vector paikka, double leveys, double korkeys)
120    {
121        PhysicsObject Trigger = new PhysicsObject(1, 1);
122        Trigger.Position = paikka;
123        Trigger.Mass = 5;
124        Trigger.Color = Color.Crimson;
125        Trigger.Shape = Shape.Rectangle;
126        Trigger.Restitution = 0;
127        Trigger.Image = null;
128        Trigger.Tag = "Miinus";
129
130        Add(Trigger, 1);
131    }
132
133
134       
135       
136    void LataaOhjaimet()
137    {
138        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
139        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
140
141        Keyboard.Listen(Key.A, ButtonState.Down, asetaNopeus, "pelaaja liikuttaa hahmoa vasemmalle", Pelaaja, NopeusVasen);
142        Keyboard.Listen(Key.A, ButtonState.Released, asetaNopeus, null, Pelaaja, Vector.Zero);
143
144        Keyboard.Listen(Key.D, ButtonState.Down, asetaNopeus, "pelaaja liikuttaa hahmoa Oikealle", Pelaaja, NopeusOikea);
145        Keyboard.Listen(Key.D, ButtonState.Released, asetaNopeus, null, Pelaaja, Vector.Zero);
146
147        Keyboard.Listen(Key.S, ButtonState.Down, asetaNopeus, "pelaaja liikuttaa hahmoa Alas", Pelaaja, NopeusAlas);
148        Keyboard.Listen(Key.S, ButtonState.Released, asetaNopeus, null, Pelaaja, Vector.Zero);
149
150        Keyboard.Listen(Key.W, ButtonState.Down, asetaNopeus, "pelaaja liikuttaa hahmoa Ylös", Pelaaja, NopeusYlos);
151        Keyboard.Listen(Key.W, ButtonState.Released, asetaNopeus, null, Pelaaja, Vector.Zero);
152    }
153
154    void asetaNopeus(PhysicsObject Pelaaja, Vector Nopeus)
155    {
156        if ((Nopeus.Y > 0) && (Pelaaja.Top > Level.Top))
157        {
158            Pelaaja.Velocity = Vector.Zero;
159            return;
160        }
161        if ((Nopeus.Y < 0) && (Pelaaja.Bottom < Level.Bottom))
162        {
163            Pelaaja.Velocity = Vector.Zero;
164            return;
165        }
166
167        if ((Nopeus.X < 0) && (Pelaaja.Left < Level.Left))
168        {
169            Pelaaja.Velocity = Vector.Zero;
170            return;
171        }
172
173        if ((Nopeus.X > 0) && (Pelaaja.Right > Level.Right))
174        {
175            Pelaaja.Velocity = Vector.Zero;
176            return;
177        }
178        Pelaaja.Velocity = Nopeus;
179    }
180
181
182    void Lisaalaskuri()
183    {
184
185       Paukut = Luopistelaskuri (Screen.Left + 100.0, Screen.Top - 100.0); 
186    }
187
188    IntMeter Luopistelaskuri(double x, double y)
189    { //Jos laskuri laskee 0:an peli loppuu
190       IntMeter laskuri = new IntMeter (1);
191        laskuri.MaxValue = 20;
192
193        Label naytto = new Label ();
194        naytto.BindTo(laskuri);
195        naytto.X = x;
196        naytto.Y = y;
197        naytto.TextColor = Color.Black;
198        naytto.BorderColor = Level.BackgroundColor;
199        Add(naytto);
200        return laskuri;
201    }
202
203    void Pistelisa(PhysicsObject Pelaaja, PhysicsObject Kohde)
204    {
205       
206        Paukut.AddValue(Paukut+1);
207        Kohde.Destroy(); 
208 
209           
210
211        }
212    void PisteMiinus(PhysicsObject Pelaaja, PhysicsObject Kohde)
213    {
214
215        Paukut.AddValue (+-1);
216        Kohde.Destroy();
217
218
219
220    }
221
222    void GameOver()
223    {
224        if (Paukut <= 0)
225        {
226            Pelaaja.Destroy();
227        EndRun();
228        }
229    }
230    void Maali(PhysicsObject Pelaaja, PhysicsObject Kohde)
231    {
232        //::://
233        EasyHighScore Lista = new EasyHighScore();
234
235    }
236       
237}
Note: See TracBrowser for help on using the repository browser.