source: 2014/30/OonaH/Pong/Pong/Pong/Pong.cs @ 5505

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