source: 2011/26/JoelH/THPeli/THPeli/Peli.cs @ 2310

Revision 2310, 3.0 KB checked in by jokrhiet, 12 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 Peli : PhysicsGame
10{
11    const double nopeus = 300;
12    const double hyppyNopeus = 900;
13    const int RUUDUN_KOKO = 40;
14    PlatformCharacter pelaaja1;
15
16    Image pelaajanKuva = LoadImage("norsu");
17    Image tahtiKuva = LoadImage("tahti");
18    Image piikkikuva = LoadImage("piikit");
19    Image splashscreen = LoadImage("Knife's Edge");
20
21    public override void Begin()
22    {
23        PhysicsObject splash = new PhysicsObject( 400.0, 400.0 );
24        splash.Shape = Shape.Rectangle;
25        splash.Image = splashscreen;
26       
27        MediaPlayer.Play("Game Theme");
28        MediaPlayer.IsRepeating = true;
29        Gravity = new Vector(0, -800);
30
31        luoKentta();
32        lisaaNappaimet();
33
34        Camera.Follow(pelaaja1);
35        Camera.ZoomFactor = 3.0;
36        Camera.StayInLevel = true;
37    }
38
39    void luoKentta()
40    {
41
42        TileMap kentta = TileMap.FromFile("kentta1.txt");
43        kentta['#'] = lisaaTaso;
44        kentta['*'] = lisaaTahti;
45        kentta['N'] = lisaaPelaaja;
46        kentta.Insert(RUUDUN_KOKO, RUUDUN_KOKO);
47        Level.CreateBorders();
48        Level.Background.CreateGradient(Color.Black, Color.Pink);
49       
50    }
51
52    PhysicsObject lisaaTaso()
53    {
54        PhysicsObject taso = PhysicsObject.CreateStaticObject(RUUDUN_KOKO, RUUDUN_KOKO);
55        taso.Color = Color.Black;
56        return taso;
57    }
58
59    PhysicsObject lisaaTahti()
60    {
61        PhysicsObject tahti = PhysicsObject.CreateStaticObject(RUUDUN_KOKO, RUUDUN_KOKO);
62        tahti.Image = tahtiKuva;
63        tahti.Tag = "tahti";
64        return tahti;
65    }
66
67    PlatformCharacter lisaaPelaaja()
68    {
69        pelaaja1 = new PlatformCharacter(30, 40);
70        pelaaja1.Mass = 10.0;
71        pelaaja1.Image = pelaajanKuva;
72        AddCollisionHandler(pelaaja1, osuTahteen);
73        return pelaaja1;
74    }
75
76
77
78
79    void lisaaNappaimet()
80    {
81        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu pelistä");
82
83        Keyboard.Listen(Key.Left, ButtonState.Down, liikuta, "Liikkuu vasemmalle", pelaaja1, -nopeus);
84        Keyboard.Listen(Key.Right, ButtonState.Down, liikuta, "Liikkuu oikealle", pelaaja1, nopeus);
85        Keyboard.Listen(Key.Up, ButtonState.Pressed, hyppaa, "Pelaaja hyppää", pelaaja1, hyppyNopeus);
86
87        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä");
88
89    }
90
91    void liikuta(PlatformCharacter hahmo, double nopeus)
92    {
93        hahmo.Walk(nopeus);
94    }
95
96    void hyppaa(PlatformCharacter hahmo, double nopeus)
97    {
98        hahmo.Jump((nopeus)-400);
99    }
100
101    void osuTahteen(PhysicsObject hahmo, PhysicsObject kohde)
102    {
103        if (kohde.Tag.ToString() == "tahti")
104        {
105            MessageDisplay.Add("Kristalli kerätty!");
106            kohde.Destroy();
107
108        }
109    }
110   
111
112}
Note: See TracBrowser for help on using the repository browser.