source: 2012/JAO/Tero/Pong/Pong/Pong/Pong.cs @ 2706

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