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

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