source: 2010/23/vajuanse/Pong/Peli.cs @ 548

Revision 548, 4.8 KB checked in by vajuanse, 13 years ago (diff)
Line 
1using System;
2using Jypeli;
3using Jypeli.ScreenObjects;
4using Jypeli.Assets;
5
6namespace Pong
7{
8    public class Peli : PhysicsGame
9    {
10        PhysicsObject pallo;
11        PhysicsObject maila1;
12        PhysicsObject maila2;
13
14        PhysicsObject vasenreuna;
15        PhysicsObject oikeareuna;
16
17        Vector nopeusylos = new Vector(0, 200);
18        Vector nopeusalas = new Vector(0, -200);
19
20        IntMeter pelaaja1pisteet;
21        IntMeter pelaaja2pisteet;
22
23        protected override void Begin()
24        {
25            Vector impulssi = new Vector(500.0, 0.0);
26            Luokentta();
27            AsetaOhjaimet();
28            Aloitapeli();
29            pallo.Hit(impulssi);
30            Lisaalaskurit();
31
32        }
33
34        void Luokentta()
35        {
36            pallo = new PhysicsObject(40.0, 40.0);
37            AddCollisionHandler(pallo, KasittelePallonTormays);
38
39            vasenreuna = Level.CreateLeftBorder();
40            vasenreuna.Restitution = 1.0;
41            vasenreuna.IsVisible = false;
42
43            oikeareuna = Level.CreateRightBorder();
44            oikeareuna.Restitution = 1.0;
45            oikeareuna.IsVisible = false;
46
47            PhysicsObject alareuna = Level.CreateBottomBorder();
48            alareuna.Restitution = 1.0;
49            alareuna.IsVisible = false;
50
51            PhysicsObject ylareuna = Level.CreateTopBorder();
52            ylareuna.Restitution = 1.0;
53            ylareuna.IsVisible = false;
54
55           
56            pallo.Shape = Shapes.Circle;
57            Add(pallo);
58            pallo.Restitution = 1.0;
59            Level.BackgroundColor = Color.LightGray;
60            Camera.ZoomToLevel(1.0);
61            pallo.X = -200.0;
62            pallo.Y = 0.0;
63
64            maila1 = LuoMaila(Level.Left + 20.0, 0.0);
65            maila2 = LuoMaila(Level.Right - 20.0, 0.0);
66        }
67
68        void Aloitapeli()
69        {
70            Vector impulssi = new Vector(500.0, 0.0);
71            pallo.Hit(impulssi);
72        }
73
74        PhysicsObject LuoMaila(double x, double y)
75        {
76            PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0);
77            maila.Shape = Shapes.Rectangle;
78            maila.X = x;
79            maila.Y = y;
80            maila.Restitution = 1.0;
81            Add(maila);
82
83            return maila;
84        }
85
86        void AsetaOhjaimet()
87        {
88            Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "pelaaja 1: liikutaa mailaa ylös", maila1, nopeusylos);
89            Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
90            Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusalas);
91            Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
92
93            Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusylos);
94            Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
95            Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusalas);
96            Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
97
98            Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
99            Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu");
100        }
101
102        void AsetaNopeus(PhysicsObject maila, Vector nopeus)
103        {
104            if ((nopeus.Y < 0) && (maila.Y < Level.Bottom))
105            {
106                maila.Velocity = Vector.Zero;
107                return;
108            }
109            if ((nopeus.Y > 0) && (maila.Y > Level.Top))
110            {
111                maila.Velocity = Vector.Zero;
112                return;
113            }
114
115            maila.Velocity = nopeus;
116
117
118        }
119
120        void Lisaalaskurit()
121        {
122            pelaaja1pisteet = luopistelaskuri(Screen.Left + 100.0, Screen.Top - 100.0);
123            pelaaja2pisteet = luopistelaskuri(Screen.Right - 100.0, Screen.Top - 100.0);
124        }
125
126        IntMeter luopistelaskuri(double x, double y)
127        {
128            IntMeter laskuri = new IntMeter(0);
129            laskuri.MaxValue = 10;
130            ValueDisplay naytto = new ValueDisplay();
131            naytto.BindTo(laskuri);
132            naytto.X = x;
133            naytto.Y = y;
134            naytto.ValueColor = Color.White;
135            Add(naytto);
136            return laskuri;
137
138        }
139        void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde)
140        {
141            if (kohde == oikeareuna)
142            {
143                pelaaja1pisteet.Value += 1;
144            }
145            else if (kohde == vasenreuna)
146            {
147                pelaaja2pisteet.Value += 1;
148            }
149
150
151        }
152    }
153}
Note: See TracBrowser for help on using the repository browser.