source: 2017/27/PyryS/GravityAssist/GravityAssist/GravityAssist/GravityAssist.cs @ 8854

Revision 8854, 3.9 KB checked in by npo17_32, 2 years ago (diff)

Finished Pong, Started GravityAssist? Game.

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class GravityAssist : PhysicsGame
10{
11    const double nopeus = 300;
12    const double hyppyNopeus = 750;
13    const int RUUDUN_KOKO = 40;
14
15    bool GravityDirection;
16
17    PhysicsObject player;
18
19    Image[] playerImages = LoadImages("Player0", "Player1");
20    Image obstacleImage = LoadImage("tahti");
21
22   
23
24    public override void Begin()
25    {
26        Gravity = new Vector(0, -1000);
27        SmoothTextures = false;
28
29        LuoKentta();
30        LisaaNappaimet();
31
32        Camera.Follow(player);
33        Camera.ZoomFactor = 1.6;
34        Camera.StayInLevel = true;
35    }
36
37    void LuoKentta()
38    {
39        TileMap kentta = TileMap.FromLevelAsset("kentta1");
40        kentta.SetTileMethod('#', LisaaTaso);
41        kentta.SetTileMethod('*', LisaaTahti);
42        kentta.SetTileMethod('N', LisaaPelaaja);
43        kentta.Execute(RUUDUN_KOKO, RUUDUN_KOKO);
44        Level.CreateBorders();
45        Level.Background.CreateGradient(Color.White, Color.SkyBlue);
46    }
47
48    void LisaaTaso(Vector paikka, double leveys, double korkeus)
49    {
50        PhysicsObject taso = PhysicsObject.CreateStaticObject(leveys, korkeus);
51        taso.Position = paikka;
52        taso.Color = Color.Gray;
53        taso.Restitution = 0.0;
54        taso.KineticFriction = 0.0;
55        taso.StaticFriction = 0.0;
56        Add(taso);
57    }
58
59    void LisaaTahti(Vector paikka, double leveys, double korkeus)
60    {
61        PhysicsObject obstacle = PhysicsObject.CreateStaticObject(leveys, korkeus);
62        obstacle.IgnoresCollisionResponse = true;
63        obstacle.Position = paikka;
64        obstacle.Image = obstacleImage;
65        obstacle.Tag = "obstacle";
66        Add(obstacle);
67    }
68
69    void LisaaPelaaja(Vector paikka, double leveys, double korkeus)
70    {
71        player = new PhysicsObject(leveys, korkeus);
72        player.Position = paikka;
73        player.Mass = 20.0;
74        player.CanRotate = false;
75        player.KineticFriction = 0.0;
76        player.StaticFriction = 0.0;
77        player.Restitution = 0.0;
78        player.Image = playerImages[0];
79        //player.AnimIdle = new Animation(playerImages);
80        //player.AnimIdle.FPS = 15;
81        player.Animation = new Animation(playerImages);
82        player.Animation.FPS = 15;
83        AddCollisionHandler(player, "obstacle", playerdeath);
84        Add(player);
85
86        Timer liikutusAjastin = new Timer();
87        liikutusAjastin.Interval = 0.02;
88        //liikutusAjastin.Timeout += delegate { player.Walk(Direction.Right); };
89        liikutusAjastin.Timeout += delegate { player.Velocity = new Vector(150, player.Velocity.Y); };
90        liikutusAjastin.Start();
91
92    }
93
94    void LisaaNappaimet()
95    {
96        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit");
97
98        Keyboard.Listen(Key.W, ButtonState.Pressed, GravitySwap, "Gravity Switched", player, 1.0);
99
100        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Exit");
101
102        ControllerOne.Listen(Button.A, ButtonState.Pressed, GravitySwap, "Gravity Switched", player, 1.0);
103    }
104
105
106    void GravitySwap(PhysicsObject Player, double Force)
107    {
108        //Player.Jump(Force);
109        if (GravityDirection == true)
110        {
111            Gravity = new Vector(0, -1000);
112            GravityDirection = false;
113            player.TextureWrapSize = new Vector(1, 1);
114        }
115        else if (GravityDirection == false)
116        {
117            Gravity = new Vector(0, 1000);
118            GravityDirection = true;
119            player.TextureWrapSize = new Vector(1, -1);
120        }
121    }
122
123    void playerdeath(PhysicsObject hahmo, PhysicsObject obstacle)
124    {
125        MessageDisplay.Add("You died.");
126        obstacle.Destroy();
127        player.Destroy();
128    }
129}
Note: See TracBrowser for help on using the repository browser.