source: 2012/23/AnttiR/Pong/Pong/Pong/Peli.cs @ 2791

Revision 2791, 7.2 KB checked in by anjuroys, 11 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 Peli : PhysicsGame
10{
11
12    PhysicsObject vasenReuna;
13    PhysicsObject oikeaReuna;
14    PhysicsObject www;
15    PhysicsObject qqq;
16    IntMeter pelaajan1Pisteet;
17    IntMeter pelaajan2Pisteet;
18    Vector nopeusYlos = new Vector(0, 500);
19    Vector nopeusAlas = new Vector(0, -500);
20    PhysicsObject pallo;
21    PhysicsObject maila2;
22    PhysicsObject maila1;
23    PhysicsObject este;
24    PhysicsObject este2;
25    PhysicsObject este3;
26    PhysicsObject este4;
27    PhysicsObject este5;
28    public override void Begin()
29    {
30        // TODO: Kirjoita ohjelmakoodisi tähän
31
32        LuoKentta();
33        aloitapeli();
34        ohjaimet();
35        LisaaLaskurit();
36
37    }
38
39    IntMeter LuoPisteLaskuri(double x, double y, PhysicsObject maila)
40    {
41        IntMeter laskuri = new IntMeter(0);
42        laskuri.MaxValue = 10;
43        laskuri.UpperLimit += delegate { maila.Destroy(); };
44        laskuri.UpperLimit += delegate
45        {
46            Explosion a = new Explosion(200);
47            a.Position = maila.Position;
48            Add(a);
49        };
50
51        Label naytto = new Label();
52        naytto.BindTo(laskuri);
53        naytto.X = x;
54        naytto.Y = y;
55        naytto.TextColor = Color.White;
56        naytto.BorderColor = Color.Black;
57        naytto.Color = Color.Black;
58        Add(naytto);
59
60        return laskuri;
61    }
62
63    void LuoKentta()
64    {
65
66        pallo = new PhysicsObject(15, 15);
67        pallo.Shape = Shape.Circle;
68        pallo.Color = Color.Black;
69        Level.BackgroundColor = Color.White;
70        pallo.X = -150;
71        pallo.Y = 0;
72        pallo.Restitution = 1.0;
73        pallo.KineticFriction = 0.1;
74        Add(pallo);
75        AddCollisionHandler(pallo, KasittelePallonTormays);
76
77        maila2 = PhysicsObject.CreateStaticObject(10.0, 150.0);
78        maila2.Shape = Shape.Rectangle;
79        maila2.X = Level.Left + 10.0;
80        maila2.Y = 0.0;
81        maila2.Restitution = 1.0;
82        maila2.Color = Color.LightBlue;
83        Add(maila2);
84
85        maila1 = PhysicsObject.CreateStaticObject(10.0, 150.0);
86        maila1.Shape = Shape.Rectangle;
87        maila1.X = Level.Right - 10.0;
88        maila1.Y = 0;
89        maila1.Restitution = 1.0;
90        maila1.Color = Color.DarkRed;
91        Add(maila1);
92
93        este = PhysicsObject.CreateStaticObject(200.0, 200.0);
94        este.Restitution = 1.0;
95        este.AngularVelocity = 10.0;
96        este.Color = Color.Black;
97        este.Shape = Shape.Hexagon;
98        este.X = -50;
99        este.Y = 300;
100        Add(este);
101
102
103        este2 = PhysicsObject.CreateStaticObject(100.0, 100.0);
104        este2.Restitution = 1.0;
105        este2.AngularVelocity = -10.0;
106        este2.Color = Color.Black;
107        este2.Shape = Shape.Octagon;
108        este2.X = 200;
109        este2.Y = -150;
110        Add(este2);
111
112        este3 = PhysicsObject.CreateStaticObject(150.0, 200.0);
113        este3.Restitution = 1.0;
114        este3.AngularVelocity = -5.0;
115        este3.Color = Color.Black;
116        este3.Shape = Shape.Pentagon;
117        este3.X = 150;
118        este3.Y = 200;
119        Add(este3);
120
121        este4 = PhysicsObject.CreateStaticObject(150.0, 150.0);
122        este4.Restitution = 1.0;
123        este4.AngularVelocity = 5.0;
124        este4.Color = Color.Black;
125        este4.Shape = Shape.Triangle;
126        este4.X = -300;
127        este4.Y = 10;
128        Add(este4);
129
130        este5 = PhysicsObject.CreateStaticObject(50.0, 50.0);
131        este5.Restitution = 1.0;
132        este5.AngularVelocity = 5.0;
133        este5.Color = Color.Black;
134        este5.Shape = Shape.Pentagon;
135        este5.X = -150;
136        este5.Y = -100;
137        Add(este5);
138
139        PhysicsObject www = Level.CreateBottomBorder();
140        vasenReuna = Level.CreateLeftBorder();
141        oikeaReuna = Level.CreateRightBorder();
142        PhysicsObject qqq = Level.CreateTopBorder();
143        vasenReuna.Restitution = 1.0;
144        vasenReuna.IsVisible = false;
145
146        oikeaReuna.Restitution = 1.0;
147        oikeaReuna.IsVisible = false;
148
149        qqq.Restitution = 1.0;
150        qqq.IsVisible = false;
151
152        www.Restitution = 1.0;
153        www.IsVisible = false;
154        Camera.ZoomToLevel();
155
156
157    }
158    void aloitapeli()
159    {
160        Vector impulssi = new Vector(510.0, 140.0);
161        pallo.Hit(impulssi);
162    }
163    void ohjaimet()
164    {
165        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
166        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila2, nopeusYlos);
167        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
168
169
170        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila2, nopeusAlas);
171        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
172
173        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila1, nopeusYlos);
174        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
175
176        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila1, nopeusAlas);
177        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
178
179        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
180
181        Keyboard.Listen(Key.Space, ButtonState.Pressed, delegate { pallo.Velocity = new Vector(pallo.Velocity.Y, pallo.Velocity.X); }, "Käännä pallon pystynopeus vaakanopeudeksi");
182        Keyboard.Listen(Key.Enter, ButtonState.Pressed, delegate { pallo.Velocity = new Vector(pallo.Velocity.X*2,pallo.Velocity.Y*2);},"Kiihdytä palloa");
183         // Keyboard.Listen(Key.Delete,ButtonState.Pressed, delegate { pallo.Velocity = new Vector(pallo.Velocity.X:2,pallo.Velocity.Y:2);},"Hidasta palloa palloa");
184
185    }
186
187
188    void AsetaNopeus(PhysicsObject maila, Vector nopeus)
189    {
190        if ((nopeus.Y > 0) && (maila.Top > Level.Top))
191        {
192            maila.Velocity = Vector.Zero;
193            return;
194        }
195
196        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom))
197        {
198            maila.Velocity = Vector.Zero;
199            return;
200        }
201
202        maila.Velocity = nopeus;
203    }
204
205    void LisaaLaskurit()
206    {
207        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0, maila1);
208        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0, maila2);
209    }
210
211    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde)
212    {   
213        if (maila1.IsDestroyed || maila2.IsDestroyed) return;
214        if (kohde == oikeaReuna) pelaajan1Pisteet.Value += 1;
215        if (kohde == vasenReuna) pelaajan2Pisteet.Value += 1;
216        if (pelaajan1Pisteet > pelaajan2Pisteet)
217        Level.BackgroundColor = Color.Blue;
218        if (pelaajan2Pisteet > pelaajan1Pisteet)
219        Level.BackgroundColor = Color.Red;
220        if (pelaajan1Pisteet == pelaajan2Pisteet)
221            Level.BackgroundColor = Color.White;
222    }
223}
Note: See TracBrowser for help on using the repository browser.