source: 2014/23/maikki/Game/Game/Game/Game.cs @ 4870

Revision 4870, 4.5 KB checked in by koannak, 8 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 Game : PhysicsGame
10{
11    const double speed = 750;
12    const double jumpspeed = 750;
13    const int TILE_SIZE = 40;
14
15    PlatformCharacter player;
16
17    Image playerImage = LoadImage("OK");
18    Image enemyImage = LoadImage("black");
19    Image starImage = LoadImage("OK");
20    IntMeter pointcounter;
21    SoundEffect goalSound = LoadSoundEffect("maali");
22
23    public override void Begin()
24    {
25        Gravity = new Vector(0, -1000);
26        CreateLevel();
27        AddControls();
28        AddPointcounter(); 
29        Camera.Follow(player);
30        Camera.ZoomFactor = 1.2;
31        Camera.StayInLevel = true;
32    }
33
34    void CreateLevel()
35    {
36        TileMap level = TileMap.FromLevelAsset("kentta1");
37        level.SetTileMethod('#', AddPlatform);
38        level.SetTileMethod('*', AddStar);
39        level.SetTileMethod('E', AddPlayer);
40        level.SetTileMethod('N', AddEnemy);
41        level.Execute(TILE_SIZE, TILE_SIZE);
42        Level.CreateBorders();
43        Level.Background.CreateGradient(Color.Gold, Color.Blue);
44    }
45
46    void AddPlatform(Vector position, double width, double height)
47    {
48        PhysicsObject platform = PhysicsObject.CreateStaticObject(width, height);
49        platform.Position = position;
50        platform.Color = Color.Green;
51        Add(platform);
52    }
53
54    void AddStar(Vector position, double width, double height)
55    {
56        PhysicsObject star = PhysicsObject.CreateStaticObject(width, height);
57        star.IgnoresCollisionResponse = true;
58        star.Position = position;
59        star.Image = starImage;
60        star.Tag = "star";
61       
62        Add(star);
63    }
64
65    void AddPlayer(Vector position, double width, double height)
66    {
67        player = new PlatformCharacter(width, height);
68        player.Position = position;
69        player.Mass = 4.0;
70        player.Image = playerImage;
71        AddCollisionHandler(player, "star", CollideWithStar);
72        Add(player);
73    }
74
75    void AddEnemy(Vector position, double width, double height)
76    {
77        PlatformCharacter enemy = new PlatformCharacter(width, height);
78         enemy.Position = position;
79       enemy.Mass = 4.0;
80        enemy.Image = enemyImage;
81        PlatformWandererBrain brain = new PlatformWandererBrain();
82        brain.Speed = 100;
83        enemy.Brain = brain;
84        Add(enemy);
85        brain.TriesToJump = true;
86        brain.JumpSpeed = 700;
87        brain.FallsOffPlatforms = false;
88    }
89    void AddControls()
90    {
91        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Show help");
92        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "End game");
93
94        Keyboard.Listen(Key.Left, ButtonState.Down, Move, "Move left", player, -speed);
95        Keyboard.Listen(Key.Right, ButtonState.Down, Move, "Move right", player, speed);
96        Keyboard.Listen(Key.Up, ButtonState.Pressed, Jump, "Jump", player, jumpspeed);
97
98        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Get out of the game");
99
100        ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, Move, "Move left", player, -speed);
101        ControllerOne.Listen(Button.DPadRight, ButtonState.Down, Move, "Move Right", player, speed);
102        ControllerOne.Listen(Button.A, ButtonState.Pressed, Jump, "Jump", player, jumpspeed);
103
104        PhoneBackButton.Listen(ConfirmExit, "End game");
105    }
106
107    void Move(PlatformCharacter character, double speed)
108    {
109        character.Walk(speed);
110    }
111
112    void Jump(PlatformCharacter character, double speed)
113    {
114       
115
116    }
117
118    void CollideWithStar(PhysicsObject character, PhysicsObject star)
119    {
120        goalSound.Play();
121        MessageDisplay.Add("WOW you got a OK!");
122        star.Destroy();
123        pointcounter.Value += 20;
124    }
125   
126
127    void AddPointcounter()
128    {
129        pointcounter = new IntMeter(0);
130
131        Label pointscreen = new Label();
132        pointscreen.X = Screen.Left + 100;
133        pointscreen.Y = Screen.Top - 100;
134        pointscreen.TextColor = Color.Black;
135        pointscreen.Color = Color.White;
136
137        pointscreen.BindTo(pointcounter);
138        Add(pointscreen);
139        pointcounter.UpperLimit +=Evrithingcollected;
140    }
141    void Evrithingcollected()
142        {
143    MessageDisplay.Add("Player 1 won the game");
144}
145
146   
147}
Note: See TracBrowser for help on using the repository browser.