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

Revision 7956, 7.4 KB checked in by tesatapa, 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 = 30000;
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    Image[] charecterimages = LoadImages("CJ1", "CJ2", "CJ3", "CJ4", "CJ5", "CJ6", "CJ7", "CJ8");
21    Image[] charecterrunimages = LoadImages("CR1", "CR2", "CR3", "CR4", "CR5", "CR6");
22    Boolean rotation = true;
23    Image startimage = LoadImage("CR4");
24    Image cacphonicimage = LoadImage("orange2");
25
26    SoundEffect collectsound = LoadSoundEffect("maali");
27
28    public override void Begin()
29    {
30        ClearAll();
31
32        Createlevel();
33        AddKeyboard();
34
35        Camera.Follow(player1);
36        Camera.ZoomFactor = 1.2;
37        Camera.StayInLevel = true;
38    }
39
40    void Createlevel()
41    {
42        TileMap level = TileMap.FromLevelAsset("kentta1");
43        level.SetTileMethod('#', Addsoil);
44        level.SetTileMethod('*', AddApple);
45        level.SetTileMethod('N', Addplayer);
46        level.SetTileMethod('!', AddRiver);
47        level.SetTileMethod('^', AddWall);
48        level.SetTileMethod('%', AddTree);
49        level.SetTileMethod('&', AddBarrier);
50        level.SetTileMethod('D', AddGrass);
51        level.SetTileMethod('O', AddOrange);
52        level.Execute(TILE_SIZE, TILE_SIZE);
53
54        Level.CreateBorders();
55        Level.Background.Image = grassimage;
56        Level.Background.FitToLevel();
57    }
58
59    void Addsoil(Vector place, double width, double hieght)
60    {
61        GameObject soil = new GameObject(width, hieght);
62        soil.Position = place;
63        soil.Color = Color.Brown;
64        Add(soil);
65    }
66   
67    void AddOrange(Vector place, double width, double hieght)
68    {
69        GameObject orange = new GameObject(width, hieght);
70        orange.Position = place;
71        orange.Image = cacphonicimage;
72        orange.Tag = orange;
73        orange.Color = Color.Orange;
74        Add(orange);
75
76    }
77
78    void AddGrass(Vector place, double width, double hieght)
79    {
80        GameObject grass = new GameObject(width, hieght);
81        grass.Position = place;
82        grass.Image = grassimage;
83        grass.Tag = grass;
84        grass.Color = Color.Green;
85        Add(grass);
86
87    }
88
89    void AddBarrier(Vector place, double width, double hieght)
90    {
91        GameObject barrier = new GameObject(width, hieght);
92        barrier.Position = place;
93        barrier.Color = Color.Black;
94        Add(barrier);
95
96    }
97
98    void AddTree(Vector place, double width, double hieght)
99    {
100        PhysicsObject tree = PhysicsObject.CreateStaticObject(width, hieght);
101        tree.Position = place;
102        tree.Image = treeimage;
103        tree.Tag = tree;
104        tree.Color = Color.Green;
105        tree.Restitution = 0.0;
106        Add(tree);
107    }
108
109
110    void AddWall(Vector place, double width, double hieght)
111    {
112        PhysicsObject wall = PhysicsObject.CreateStaticObject(width*0.3, hieght*3);
113        wall.Position = place;
114        wall.Color = Color.Black;
115        wall.Restitution = 0.0;
116        wall.CollisionIgnoreGroup = 5;
117        Add(wall);
118    }
119
120    void AddRiver(Vector place, double width, double hieght)
121    {
122        PhysicsObject river = PhysicsObject.CreateStaticObject(width, hieght*3);
123        river.Color = Color.MediumBlue;
124        river.Position = place;
125        river.Restitution = 0.0;
126        Add(river);
127    }
128
129    void AddApple(Vector place, double width, double hieght)
130    {
131        PhysicsObject apple = PhysicsObject.CreateStaticObject(width, hieght);
132        apple.Position = place;
133        apple.Image = appleimage;
134        apple.Tag = "apple";
135        Add(apple);
136    }
137
138    void Addplayer(Vector place, double width, double hieght)
139    {
140        player1 = new PhysicsObject(width*0.5, hieght);
141        player1.Position = place;
142        player1.Mass = 4.0;
143        AddCollisionHandler(player1, "apple", HitApple);
144        player1.CollisionIgnoreGroup = 5;
145        player1.Image = startimage;
146        Add(player1);
147        player1.CanRotate = false;
148    }
149
150    void AddKeyboard()
151    {
152        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "show rules");
153        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "end game");
154
155        Keyboard.Listen(Key.Left, ButtonState.Pressed, move, "move right", player1, new Vector(-speed, 0));
156       // Keyboard.Listen(Key.Left, ButtonState.Released, , "");
157        Keyboard.Listen(Key.Right, ButtonState.Pressed,moveright, "move left", player1, new Vector(speed, 0));
158        //Keyboard.Listen(Key.Right, ButtonState.Released, player1.Stop, "");
159        Keyboard.Listen(Key.Up, ButtonState.Pressed, move, "player jumps", player1, new Vector(0, speed));
160        //Keyboard.Listen(Key.Up, ButtonState.Released, player1.Stop, "");
161        Keyboard.Listen(Key.Down, ButtonState.Pressed, move, "player jumps", player1, new Vector(0, -speed));
162        //Keyboard.Listen(Key.Down, ButtonState.Released, player1.Stop, "");
163        Keyboard.Listen(Key.Left, ButtonState.Released, animation, "", player1);
164        Keyboard.Listen(Key.Right, ButtonState.Released, animation, "", player1);
165        Keyboard.Listen(Key.Up, ButtonState.Released, animation, "", player1);
166        Keyboard.Listen(Key.Down, ButtonState.Released, animation, "", player1);
167        /*ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "exit game");
168
169         ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, move, "player moves left", player1, -speed);
170         ControllerOne.Listen(Button.DPadRight, ButtonState.Down, move, "player moves right", player1, speed);
171         ControllerOne.Listen(Button.A, ButtonState.Pressed, Jump, "player jumps", player1, jumpspeed);
172         */
173
174        PhoneBackButton.Listen(ConfirmExit, "end game");
175    }
176
177    void move(PhysicsObject charecter, Vector speed)
178    {
179        player1.Animation = new Animation(charecterrunimages);
180        if (speed.X <= 0.0)
181        {
182            player1.MirrorImage();
183            rotation = false;
184        }
185        player1.Animation.Start();
186        charecter.Push(speed);
187       
188
189    }
190    void moveright(PhysicsObject charecter, Vector speed)
191    {
192        player1.Animation = new Animation(charecterrunimages);
193        if (rotation == false)
194        {
195            player1.MirrorImage();
196            rotation = true;
197        }
198            player1.Animation.Start();
199            charecter.Push(speed);
200
201
202    }
203
204    void animation(PhysicsObject charecter)
205
206    {
207        player1.Animation.Stop();
208        player1.Stop();
209    }
210
211    void HitApple(PhysicsObject charecter, PhysicsObject apple)
212    {
213        collectsound.Play();
214        MessageDisplay.Add("collected apple!");
215        apple.Destroy();
216        charecter.Destroy();
217        MessageDisplay.Add("game over!");
218        EndMenu();
219    }
220
221    void EndMenu()
222    {
223        this.StopAll();
224        MultiSelectWindow endMenu = new MultiSelectWindow("Game Over", "Restart",  "Exit");
225        Add(endMenu);
226        endMenu.AddItemHandler(0, Begin);
227        endMenu.AddItemHandler(1, Exit);
228    }
229}
Note: See TracBrowser for help on using the repository browser.