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

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