source: 2016/27/RuhanAnjanK/Noname/Noname/Noname/Noname.cs @ 7945

Revision 7945, 5.7 KB checked in by otosjahn, 3 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 Noname : PhysicsGame
10{
11    const double speed = 2000;
12    const double jumpspeed = 750;
13    const int TILE_SIZE = 40;
14
15    PhysicsObject player1;
16    Image playerimage = LoadImage("boy3");
17    Image appleimage = LoadImage("appleimage4");
18    Image treeimage = LoadImage("tree2");
19    Image grassimage = LoadImage("grass2");
20
21    SoundEffect collectsound = LoadSoundEffect("maali");
22
23    public override void Begin()
24    {
25        ClearAll();
26
27        Createlevel();
28        AddKeyboard();
29
30        Camera.Follow(player1);
31        Camera.ZoomFactor = 1.2;
32        Camera.StayInLevel = true;
33    }
34
35    void Createlevel()
36    {
37        TileMap level = TileMap.FromLevelAsset("kentta1");
38        level.SetTileMethod('#', Addsoil);
39        level.SetTileMethod('*', AddApple);
40        level.SetTileMethod('N', Addplayer);
41        level.SetTileMethod('!', AddRiver);
42        level.SetTileMethod('^', AddWall);
43        level.SetTileMethod('%', AddTree);
44        level.SetTileMethod('&', AddBarrier);
45        level.SetTileMethod('D', AddGrass);
46        level.Execute(TILE_SIZE, TILE_SIZE);
47
48        Level.CreateBorders();
49        Level.Background.CreateGradient(Color.Brown, Color.Brown);
50    }
51
52    void Addsoil(Vector place, double width, double hieght)
53    {
54        GameObject soil = new GameObject(width, hieght);
55        soil.Position = place;
56        soil.Color = Color.Brown;
57        Add(soil);
58    }
59
60    void AddGrass(Vector place, double width, double hieght)
61    {
62        GameObject grass = new GameObject(width, hieght);
63        grass.Position = place;
64        grass.Image = grassimage;
65        grass.Tag = grass;
66        grass.Color = Color.Green;
67        Add(grass);
68
69    }
70
71    void AddBarrier(Vector place, double width, double hieght)
72    {
73        GameObject barrier = new GameObject(width, hieght);
74        barrier.Position = place;
75        barrier.Color = Color.Black;
76        Add(barrier);
77
78    }
79
80    void AddTree(Vector place, double width, double hieght)
81    {
82        PhysicsObject tree = PhysicsObject.CreateStaticObject(width, hieght);
83        tree.Position = place;
84        tree.Image = treeimage;
85        tree.Tag = tree;
86        tree.Color = Color.Green;
87        tree.Restitution = 0.0;
88        Add(tree);
89    }
90
91
92    void AddWall(Vector place, double width, double hieght)
93    {
94        PhysicsObject wall = PhysicsObject.CreateStaticObject(width*0.3, hieght*3);
95        wall.Position = place;
96        wall.Color = Color.Black;
97        wall.Restitution = 0.0;
98        wall.CollisionIgnoreGroup = 5;
99        Add(wall);
100    }
101
102    void AddRiver(Vector place, double width, double hieght)
103    {
104        PhysicsObject river = PhysicsObject.CreateStaticObject(width, hieght*3);
105        river.Color = Color.MediumBlue;
106        river.Position = place;
107        river.Restitution = 0.0;
108        Add(river);
109    }
110
111    void AddApple(Vector place, double width, double hieght)
112    {
113        PhysicsObject apple = PhysicsObject.CreateStaticObject(width, hieght);
114        apple.Position = place;
115        apple.Image = appleimage;
116        apple.Tag = "apple";
117        Add(apple);
118    }
119
120    void Addplayer(Vector place, double width, double hieght)
121    {
122        player1 = new PhysicsObject(width*0.5, hieght);
123        player1.Position = place;
124        player1.Mass = 4.0;
125        player1.Image = playerimage;
126        AddCollisionHandler(player1, "apple", HitApple);
127        player1.CollisionIgnoreGroup = 5;
128        Add(player1);
129        player1.CanRotate = false;
130    }
131
132    void AddKeyboard()
133    {
134        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "show rules");
135        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "end game");
136
137        Keyboard.Listen(Key.Left, ButtonState.Down, move, "move right", player1, new Vector(-speed, 0));
138        Keyboard.Listen(Key.Left, ButtonState.Released, player1.Stop, "");
139        Keyboard.Listen(Key.Right, ButtonState.Down,move, "move left", player1, new Vector(speed, 0));
140        Keyboard.Listen(Key.Right, ButtonState.Released, player1.Stop, "");
141        Keyboard.Listen(Key.Up, ButtonState.Down, move, "player jumps", player1, new Vector(0, speed));
142        Keyboard.Listen(Key.Up, ButtonState.Released, player1.Stop, "");
143        Keyboard.Listen(Key.Down, ButtonState.Down, move, "player jumps", player1, new Vector(0, -speed));
144        Keyboard.Listen(Key.Down, ButtonState.Released, player1.Stop, "");
145        /*ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "exit game");
146
147         ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, move, "player moves left", player1, -speed);
148         ControllerOne.Listen(Button.DPadRight, ButtonState.Down, move, "player moves right", player1, speed);
149         ControllerOne.Listen(Button.A, ButtonState.Pressed, Jump, "player jumps", player1, jumpspeed);
150         */
151
152        PhoneBackButton.Listen(ConfirmExit, "end game");
153    }
154
155    void move(PhysicsObject charecter, Vector speed)
156    {
157        charecter.Push(speed);
158    }
159
160    void HitApple(PhysicsObject charecter, PhysicsObject apple)
161    {
162        collectsound.Play();
163        MessageDisplay.Add("collected apple!");
164        apple.Destroy();
165        charecter.Destroy();
166        MessageDisplay.Add("game over!");
167        EndMenu();
168    }
169
170    void EndMenu()
171    {
172        this.StopAll();
173        MultiSelectWindow endMenu = new MultiSelectWindow("Game Over", "Restart",  "Exit");
174        Add(endMenu);
175        endMenu.AddItemHandler(0, Begin);
176        endMenu.AddItemHandler(1, Exit);
177    }
178}
Note: See TracBrowser for help on using the repository browser.