source: 2017/27/PinjaV/RunAwayFromYourFear/RunAwayFromYourFear/RunAwayFromYourFear/RunAwayFromYourFear.cs @ 8937

Revision 8937, 5.7 KB checked in by npo17_49, 6 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class RunAwayFromYourFear : PhysicsGame
10{
11    const double nopeus = 200;
12    const double hyppyNopeus = 750;
13    const int RUUDUN_KOKO = 40;
14
15    PlatformCharacter Ben;
16    PlatformCharacter Link;
17    Image pelaajanKuva = LoadImage("link");
18    Image pelaajan2Kuva = LoadImage("link2");
19    Image tahtiKuva = LoadImage("tahti");
20    int kenttaNro = 1;
21    int indeksi = 0;
22
23    Image[] rubiiniKuvat = LoadImages("timanttipunainen", "timanttilime", "timanttioranssi", "timanttisininen", "timanttivaalee");
24    Image[] eyeless_jackKuvat = LoadImages("eyeless_jack1", "eyeless_jack2", "eyeless_jack3", "eyeless_jack4");
25    SoundEffect maaliAani = LoadSoundEffect("maali");
26
27    IntMeter rubiiniLaskuri;
28
29    public override void Begin()
30    {
31        Gravity = new Vector(0, -1000);
32
33        LuoKentta("Labyrint");
34        LisaaNappaimet();
35        LuoPisteLaskuri();
36
37        Camera.Follow(Ben, Link);
38        Camera.ZoomFactor = 1.2;
39        // Camera.StayInLevel = true;
40    }
41
42    void LuoKentta(string pv)
43    {
44        ColorTileMap kentta = ColorTileMap.FromLevelAsset(pv);
45        kentta.SetTileMethod(Color.Black, LisaaTaso);
46        kentta.SetTileMethod(Color.FromHexCode("A518EC"), LisaaTahti);
47        kentta.SetTileMethod(Color.Red, LisaaPelaaja);
48        kentta.SetTileMethod(Color.FromHexCode("FF0026FF"), LuoLink2);
49        kentta.Execute(RUUDUN_KOKO, RUUDUN_KOKO);
50        Level.CreateBorders();
51        Level.Background.CreateGradient(Color.PaintDotNetBlue, Color.MidnightBlue);
52    }
53
54    void LisaaTaso(Vector paikka, double leveys, double korkeus)
55    {
56        PhysicsObject taso = PhysicsObject.CreateStaticObject(leveys, korkeus);
57        taso.Position = paikka;
58        taso.Color = Color.ForestGreen;
59        Add(taso);
60    }
61
62    void LisaaTahti(Vector paikka, double leveys, double korkeus)
63    {
64        PhysicsObject tahti = PhysicsObject.CreateStaticObject(leveys, korkeus);
65        tahti.IgnoresCollisionResponse = true;
66        tahti.Position = paikka;
67       tahti.Image = rubiiniKuvat[indeksi++];
68        tahti.Tag = "tahti";
69        Add(tahti);
70    }
71
72    void LisaaPelaaja(Vector paikka, double leveys, double korkeus)
73    {
74        Ben = new PlatformCharacter(leveys, korkeus * 1.5);
75        Ben.Position = paikka;
76        Ben.Mass = 4.0;
77        Ben.Image = pelaajanKuva;
78        if (kenttaNro == 2)
79        {
80            Ben.AnimWalk = new Animation(eyeless_jackKuvat);
81            Ben.AnimWalk.FPS = 10;
82            Ben.AnimIdle = new Animation(eyeless_jackKuvat[0]);
83        }
84        AddCollisionHandler(Ben, "Link", Ded);
85        Ben.Tag = "Ben";
86        Add(Ben);
87    }
88    void Ded(PhysicsObject Ben, PhysicsObject Link)
89    {
90        MessageDisplay.Add("Ded! Ben won!");
91        Link.Destroy();
92        Timer.SingleShot(3, Exit);
93    }
94
95    void LinkVoittaa()
96    {
97        MessageDisplay.Add("Ben u lost and Link won");
98        Ben.Destroy();
99        Timer.SingleShot(3, Exit);
100    }
101    void LuoLink2(Vector paikka, double leveys, double korkeus)
102    {
103        Link = new PlatformCharacter(leveys, korkeus * 1.5);
104        Link.Position = paikka;
105        Link.Mass = 4.0;
106        Link.Image = pelaajan2Kuva;
107        Link.Tag = "Link";
108        AddCollisionHandler(Link, "tahti", TormaaTahteen);
109        Add(Link);
110    }
111
112
113
114    void LuoPisteLaskuri()
115    {
116        rubiiniLaskuri = new IntMeter(0, 0, 5);
117        rubiiniLaskuri.UpperLimit += LinkVoittaa;
118
119        Label pisteNaytto = new Label();
120        pisteNaytto.X = Screen.Left + 100;
121        pisteNaytto.Y = Screen.Top - 100;
122        pisteNaytto.TextColor = Color.Black;
123        pisteNaytto.Color = Color.White;
124        pisteNaytto.Title = "Linkin keräämät rubiinit";
125
126
127        pisteNaytto.BindTo(rubiiniLaskuri);
128        Add(pisteNaytto);
129
130    }
131    void LisaaNappaimet()
132    {
133        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
134        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
135
136        Keyboard.Listen(Key.Left, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", Ben, -nopeus);
137        Keyboard.Listen(Key.Right, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", Ben, nopeus);
138        Keyboard.Listen(Key.Up, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", Ben, hyppyNopeus);
139
140        Keyboard.Listen(Key.A, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", Link, -nopeus);
141        Keyboard.Listen(Key.D, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", Link, nopeus);
142        Keyboard.Listen(Key.W, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", Link, hyppyNopeus);
143
144    }
145
146    void Liikuta(PlatformCharacter hahmo, double nopeus)
147    {
148        hahmo.Walk(nopeus);
149    }
150
151    void Hyppaa(PlatformCharacter hahmo, double nopeus)
152    {
153        hahmo.Jump(nopeus);
154    }
155
156    void TormaaTahteen(PhysicsObject hahmo, PhysicsObject tahti)
157    {
158        maaliAani.Play();
159        MessageDisplay.Add("Keräsit tähden!");
160        tahti.Destroy();
161        rubiiniLaskuri.Value++;
162        if (rubiiniLaskuri.Value == 5)
163        {
164            indeksi = 0;
165            kenttaNro++;
166            SeuraavaKentta();
167           
168        }
169    }
170    void SeuraavaKentta()
171    {
172        ClearAll();
173        if (kenttaNro == 1) LuoKentta("Labyrint1");
174        else if (kenttaNro == 2) LuoKentta("Labyrint2");
175        LisaaNappaimet();
176        LuoPisteLaskuri();
177        Gravity = new Vector(0, -1000);
178        Camera.Follow(Ben, Link);
179        Camera.ZoomFactor = 1.2;
180        // Camera.StayInLevel = true;
181    }
182}
Note: See TracBrowser for help on using the repository browser.