source: 2010/23/lasakauh/Testi/Peli.cs @ 627

Revision 627, 6.6 KB checked in by lasakauh, 13 years ago (diff)
Line 
1using System;
2using Jypeli;
3using Jypeli.ScreenObjects;
4using Jypeli.Assets;
5
6
7namespace Testi
8{
9    class Tasohyppely : PhysicsGame
10    {
11        const double nopeusEteen = 100;
12        const double nopeusTaakse = 100;
13        const double hyppyVoima = 4000;
14        const double isku = 10;
15
16        PlatformCharacter p1;
17        PlatformCharacter p2;
18        int hp1 = 100;
19        int hp2 = 100;
20       
21
22        protected override void Begin()
23        {
24            Level.Width = 300;
25            Level.Height = 300;
26            Camera.ZoomFactor = 2.0;
27            Camera.StayInLevel = true;
28            Gravity = new Vector(0, -1000);
29
30            luoKentta();
31            lisaaNappaimet();
32           
33
34        }
35
36        void luoKentta()
37        {
38            Level.CreateBorders();
39            Level.Background.CreateGradient(Color.Black,Color.DarkBlue);
40
41            lisaaMaaTaso(0, -87);
42            lisaaPelaajat();
43            lisaap2();
44            hpbar1();
45            hpbar2();
46            hpp1(145,100);
47            hpp2(-145, 100);
48        }
49
50        void lisaaMaaTaso(double x, double y)
51        {
52            PhysicsObject taso = PhysicsObject.CreateStaticObject(302, 5);
53            taso.Image = LoadImage("taso_maa");
54            taso.X = x;
55            taso.Y = y;
56            Add(taso);
57        }
58
59        void lisaaSeina(double x,double y)
60        {
61            PhysicsObject taso = PhysicsObject.CreateStaticObject(10, 30);
62            taso.Image = LoadImage("taso_pystyseina");
63            taso.X = x;
64            taso.Y = y;
65            Add(taso);
66        }
67
68        void lisaaTaso(double x, double y)
69        {
70            PhysicsObject taso = PhysicsObject.CreateStaticObject(30, 10);
71            taso.Image = LoadImage("taso_vaakaseina");
72            taso.X = x;
73            taso.Y = y;
74            Add(taso);
75        }
76
77        void lisaaPelaajat()
78        {
79            p1 = new PlatformCharacter(10, 20);
80            p1.Mass = 6.0;
81            //Animation("p1_ani",65,false);
82            p1.Image = LoadImage("ninja");
83            //p1.Animation = LoadImage("p1_ani");
84            p1.X = -140;
85            p1.Y = Level.Bottom + 100;
86            Add(p1);
87           
88
89        }
90
91        void lisaap2()
92        {
93            p2 = new PlatformCharacter(10, 20);
94            p2.Mass = 6.0;
95            p2.Image = LoadImage("ninja2");
96            p2.X = 140;
97            p2.Y = Level.Bottom + 100;
98            Add(p2);
99           
100        }
101
102        void lisaaNappaimet()
103        {
104            Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
105            Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu pelistä");
106
107            Keyboard.Listen(Key.A, ButtonState.Down, liikutaTaakse, "Liikkuu vasemmalle", p1,nopeusTaakse);
108            Keyboard.Listen(Key.D, ButtonState.Down, liikutaEteen, "Liikkuu oikealle", p1,nopeusEteen);
109            Keyboard.Listen(Key.W, ButtonState.Pressed, hyppaa, "Hyppää", p1, hyppyVoima);
110
111            Keyboard.Listen(Key.Left, ButtonState.Down, liikutaTaakse, "Liikkuu vasemmalle", p2, nopeusTaakse);
112            Keyboard.Listen(Key.Right, ButtonState.Down, liikutaEteen, "Liikkuu oikealle", p2, nopeusEteen);
113            Keyboard.Listen(Key.Up, ButtonState.Pressed, hyppaa, "Hyppää", p2, hyppyVoima);
114            Keyboard.Listen(Key.NumPad0, ButtonState.Pressed, iske,"iske"); 
115
116            lisaaGamePadNappaimet(ControllerOne, p1);
117            lisaaGamePadNappaimet(ControllerTwo, p2);
118        }
119
120        void lisaaGamePadNappaimet(GamePad controller, PlatformCharacter hahmo)
121        {
122            controller.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä");
123            controller.Listen(Button.DPadLeft, ButtonState.Down, liikutaTaakse, "Pelaaja liikkuu vasemmalle", hahmo, nopeusTaakse);
124            controller.Listen(Button.DPadRight, ButtonState.Down, liikutaEteen, "Pelaaja liikkuu oikealle", hahmo, nopeusEteen);
125            controller.Listen(Button.A, ButtonState.Pressed, hyppaa, "Pelaaja hyppää", hahmo, hyppyVoima);
126            Vector tatinAsento = ControllerOne.LeftThumbDirection;
127        }
128
129        void liikutaEteen(PlatformCharacter hahmo, double nopeus)
130        {
131            hahmo.Walk(nopeus);
132        }
133
134        void liikutaTaakse(PlatformCharacter hahmo, double nopeus)
135        {
136            hahmo.Walk(-nopeus);
137        }
138
139        void hyppaa(PlatformCharacter hahmo, double voima)
140        {
141            hahmo.Jump(voima);
142        }
143        void hpbar1()
144        {
145            PhysicsObject hpbar1 = PhysicsObject.CreateStaticObject(102, 20);
146            hpbar1.Image = LoadImage("hpbar1");
147            hpbar1.X=-95;
148            hpbar1.Y=140;
149            Add(hpbar1);
150           
151        }
152        void hpbar2()
153        {
154            PhysicsObject hpbar2 = PhysicsObject.CreateStaticObject(102, 20);
155            hpbar2.Image = LoadImage("hpbar2");
156            hpbar2.X = 95;
157            hpbar2.Y = 140;
158            Add(hpbar2);
159        }
160        void hpp1(double x, double hp1)
161        {
162            for (int i = 1; i < 102; i++)
163            {
164                PhysicsObject hpp1 = PhysicsObject.CreateStaticObject(1, 8.5);
165                hpp1.Shape = Shapes.Rectangle;
166                hpp1.Color = Color.Green;
167                hpp1.X = -146+i;
168                hpp1.Y = 144.8;
169                Add(hpp1);
170            }
171        }
172        void hpp2(double x, double hp2)
173        {
174            for (int i = 1; i < 102; i++)
175            {
176                PhysicsObject hpp2 = PhysicsObject.CreateStaticObject(1, 8.5);
177                hpp2.Shape = Shapes.Rectangle;
178                hpp2.Color = Color.Green;
179                hpp2.X = 146-i;
180                hpp2.Y = 144.8;
181                Add(hpp2);
182            }
183        }
184        void iske()
185        {
186            Vector isku = new Vector(kohde.Position.X + 5, kohde.Position.Y + 5);
187            kohde.Hit(isku);
188            Image[] iskuKuvat = LoadImages("a1", "a2", "a3", "a4");
189            p1.Animation = new Animation(iskuKuvat);
190            p1.Animation.Start;
191                hp2 =-10;
192                for (int i = 1; i < 102 - hp1; i++)
193                {
194                    PhysicsObject hpp1 = PhysicsObject.CreateStaticObject(1, 8.5);
195                    hpp1.Shape = Shapes.Rectangle;
196                    hpp1.Color = Color.Green;
197                    hpp1.X = -146 + i;
198                    hpp1.Y = 144.8;
199                    Add(hpp1);
200                }
201            }
202        }
203
204    }
205}
Note: See TracBrowser for help on using the repository browser.