source: 2010/24/ramipasa/Pong/Peli.cs @ 810

Revision 810, 4.9 KB checked in by ramipasa, 13 years ago (diff)

Tein ruudukkomoottorini valmiiksi.

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