source: 2011/23/ookantol/Pong/Pong/Pong/Peli.cs @ 1718

Revision 1718, 6.6 KB checked in by ookantol, 12 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    Vector nopeusYlos = new Vector(0, 500);
12    Vector nopeusAlas = new Vector(0, -500);
13    Vector nopeusOikealle = new Vector(500, 0);
14    Vector nopeusVasemmalle = new Vector(-500, 0);
15
16    PhysicsObject pallo;
17    PhysicsObject maila1;
18    PhysicsObject maila2;
19    PhysicsObject maila3;
20    PhysicsObject maila4;
21
22    PhysicsObject vasenReuna;
23    PhysicsObject oikeaReuna;
24
25    IntMeter pelaajan1Pisteet;
26    IntMeter pelaajan2Pisteet;
27
28    public override void Begin()
29    {
30        LuoKentta();
31        AsetaOhjaimet();
32        LisaaLaskurit();
33        AloitaPeli();
34    }
35
36    void LuoKentta()
37    {
38        pallo = new PhysicsObject(40.0, 40.0);
39        pallo.Shape = Shape.Circle;
40        pallo.Color = Color.Black;
41        Add(pallo);
42        pallo.X = -200.0;
43        pallo.Y = 0.0;
44        pallo.Restitution = 1.0;
45        AddCollisionHandler(pallo, KasittelePallonTormays);
46
47        maila1 = LuoMaila(Level.Left + 20.0, 0.0);
48        maila2 = LuoMaila(Level.Right - 20.0, 0.0);
49        maila3 = LuoMaila2(0.0, Level.Top -20.0);
50        maila4 = LuoMaila2(0.0,Level.Bottom +20.0);
51
52        vasenReuna = Level.CreateLeftBorder();
53        vasenReuna.Restitution = 1.0;
54        vasenReuna.IsVisible = false;
55        oikeaReuna = Level.CreateRightBorder();
56        oikeaReuna.Restitution = 1.0;
57        oikeaReuna.IsVisible = false;
58        PhysicsObject alaReuna = Level.CreateBottomBorder();
59        alaReuna.Restitution = 1.0;
60        alaReuna.IsVisible = false;
61        PhysicsObject ylaReuna = Level.CreateTopBorder();
62        ylaReuna.Restitution = 1.0;
63        ylaReuna.IsVisible = false;
64
65        Level.BackgroundColor = Color.White;
66
67        Camera.ZoomToLevel();
68    }
69
70
71    void AloitaPeli()
72    {
73        Vector impulssi = new Vector(500.0, 0.0);
74        pallo.Hit(impulssi);
75    }
76
77
78    PhysicsObject LuoMaila(double x, double y)
79    {
80        PhysicsObject maila = PhysicsObject.CreateStaticObject(10.0, 120.0);
81        maila.Shape = Shape.Rectangle;
82        maila.X = x;
83        maila.Y = y;
84        maila.Color = Color.Black;
85        maila.Restitution = 1.0;
86        Add(maila);
87        return maila;
88    }
89
90    PhysicsObject LuoMaila2 (double x, double y)
91    {
92        PhysicsObject maila2 = PhysicsObject.CreateStaticObject(120.0, 10.0);
93        maila2.Shape = Shape.Rectangle;
94        maila2.X = x;
95        maila2.Y = y;
96        maila2.Color = Color.Black;
97        maila2.Restitution = 1.0;
98        Add(maila2);
99        return maila2;
100    }
101
102    void AsetaOhjaimet()
103    {
104        Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos);
105        Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
106        Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas);
107        Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
108
109        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos);
110        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
111        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas);
112        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
113
114        Keyboard.Listen(Key.Right, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa oikealle", maila3, nopeusOikealle);
115        Keyboard.Listen(Key.Right, ButtonState.Released, AsetaNopeus, null, maila3, Vector.Zero);
116        Keyboard.Listen(Key.Left, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa vasemmalle", maila3, nopeusVasemmalle);
117        Keyboard.Listen(Key.Left, ButtonState.Released, AsetaNopeus, null, maila3, Vector.Zero);
118
119        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa vasemmalle", maila4, nopeusVasemmalle);
120        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila4, Vector.Zero);
121        Keyboard.Listen(Key.D, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa oikealle", maila4, nopeusOikealle);
122        Keyboard.Listen(Key.D, ButtonState.Released, AsetaNopeus, null, maila4, Vector.Zero);
123
124        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
125        Keyboard.Listen(Key.Enter, ButtonState.Pressed, NewGame, "Uusi peli");
126        Keyboard.Listen(Key.Space, ButtonState.Pressed, Release, "Vapauta");
127        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu");
128    }
129    void NewGame()
130    {
131        pelaajan1Pisteet.Reset();
132        pelaajan2Pisteet.Reset();
133        pallo.X = 0;
134        pallo.Y = 0;
135        pallo.Velocity = new Vector (0, 0);
136    }
137
138    void Release()
139    {
140        pallo.Velocity = new Vector(400, 100);
141    }
142
143    void AsetaNopeus(PhysicsObject maila, Vector nopeus)
144    {
145        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom))
146        {
147            maila.Velocity = Vector.Zero;
148            return;
149        }
150
151        if ((nopeus.Y > 0) && (maila.Top > Level.Top))
152        {
153            maila.Velocity = Vector.Zero;
154            return;
155        }
156
157        maila.Velocity = nopeus;
158
159    }
160
161
162    void LisaaLaskurit()
163    {
164        pelaajan1Pisteet = LuoPisteLaskuri( Screen.Left + 100.0, Screen.Top - 100.0 );
165        pelaajan2Pisteet = LuoPisteLaskuri( Screen.Right - 100.0, Screen.Top - 100.0 );
166    }
167
168
169    IntMeter LuoPisteLaskuri ( double x, double y )
170
171    {
172        IntMeter laskuri = new IntMeter(0);
173        laskuri.MaxValue = 10;
174        Label naytto = new Label();
175        naytto.BindTo(laskuri);
176        naytto.X = x;
177        naytto.Y = y;
178        naytto.TextColor = Color.Black;
179        naytto.BorderColor = Level.BackgroundColor;
180        naytto.Color = Level.BackgroundColor;
181        Add(naytto);
182        return laskuri;
183    }
184
185    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 
186    {
187        if (kohde == oikeaReuna)
188        {
189            pelaajan1Pisteet.Value += 1;
190            pallo.X = 0;
191            pallo.Y = 0;
192            pallo.Velocity = new Vector(0, 0);
193        }
194        else if (kohde == vasenReuna)
195        {
196            pelaajan2Pisteet.Value += 1;
197            pallo.X = 0;
198            pallo.Y = 0;
199            pallo.Velocity = new Vector(0, 0);
200        }
201
202    }
203
204
205}
Note: See TracBrowser for help on using the repository browser.