source: 2012/23/AnttiR/Pong/Pong/Pong/Peli.cs @ 2773

Revision 2773, 4.9 KB checked in by anlakane, 11 years ago (diff)

Pong-peli :-)

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Peli : PhysicsGame
10{
11
12    PhysicsObject vasenReuna;
13    PhysicsObject oikeaReuna;
14    PhysicsObject www;
15    PhysicsObject qqq;
16    IntMeter pelaajan1Pisteet;
17    IntMeter pelaajan2Pisteet;
18    Vector nopeusYlos = new Vector(0, 500);
19    Vector nopeusAlas = new Vector(0, -500);
20    PhysicsObject pallo;
21    PhysicsObject maila2;
22    PhysicsObject maila1;
23    public override void Begin()
24    {
25        // TODO: Kirjoita ohjelmakoodisi tähän
26
27        LuoKentta();
28        aloitapeli();
29        ohjaimet();
30        LisaaLaskurit();
31
32    }
33
34    IntMeter LuoPisteLaskuri(double x, double y, PhysicsObject maila)
35    {
36        IntMeter laskuri = new IntMeter(0);
37        laskuri.MaxValue = 10;
38        laskuri.UpperLimit += delegate { maila.Destroy(); };
39        laskuri.UpperLimit += delegate
40        {
41            Explosion a = new Explosion(200);
42            a.Position = maila.Position;
43            Add(a);
44        };
45
46        Label naytto = new Label();
47        naytto.BindTo(laskuri);
48        naytto.X = x;
49        naytto.Y = y;
50        naytto.TextColor = Color.Black;
51        naytto.BorderColor = Level.BackgroundColor;
52        naytto.Color = Level.BackgroundColor;
53        Add(naytto);
54
55        return laskuri;
56    }
57
58    void LuoKentta()
59    {
60
61        pallo = new PhysicsObject(10, 10);
62        pallo.Shape = Shape.Circle;
63        pallo.Color = Color.Black;
64        Level.BackgroundColor = Color.White;
65        pallo.X = -150;
66        pallo.Y = 0;
67        pallo.Restitution = 1.0;
68        pallo.KineticFriction = 0.3;
69        Add(pallo);
70        AddCollisionHandler(pallo, KasittelePallonTormays);
71
72        maila2 = PhysicsObject.CreateStaticObject(10.0, 150.0);
73        maila2.Shape = Shape.Rectangle;
74        maila2.X = Level.Left + 20.0;
75        maila2.Y = 0.0;
76        maila2.Restitution = 1.0;
77        maila2.Color = Color.Black;
78        Add(maila2);
79
80        maila1 = PhysicsObject.CreateStaticObject(10.0, 150.0);
81        maila1.Shape = Shape.Rectangle;
82        maila1.X = Level.Right - 20.0;
83        maila1.Y = 0;
84        maila1.Restitution = 1.0;
85        maila1.Color = Color.Black;
86        Add(maila1);
87
88
89        PhysicsObject www = Level.CreateBottomBorder();
90        vasenReuna = Level.CreateLeftBorder();
91        oikeaReuna = Level.CreateRightBorder();
92        PhysicsObject qqq = Level.CreateTopBorder();
93        vasenReuna.Restitution = 1.0;
94        vasenReuna.IsVisible = false;
95
96        oikeaReuna.Restitution = 1.0;
97        oikeaReuna.IsVisible = false;
98
99        qqq.Restitution = 1.0;
100        qqq.IsVisible = false;
101
102        www.Restitution = 1.0;
103        www.IsVisible = false;
104        Camera.ZoomToLevel();
105
106
107    }
108    void aloitapeli()
109    {
110        Vector impulssi = new Vector(510.0, 140.0);
111        pallo.Hit(impulssi);
112    }
113    void ohjaimet()
114    {
115        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
116        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila2, nopeusYlos);
117        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
118
119        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila2, nopeusAlas);
120        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
121
122        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila1, nopeusYlos);
123        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
124        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila1, nopeusAlas);
125        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
126
127        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
128    }
129
130
131    void AsetaNopeus(PhysicsObject maila, Vector nopeus)
132    {
133        if ((nopeus.Y > 0) && (maila.Top > Level.Top))
134        {
135            maila.Velocity = Vector.Zero;
136            return;
137        }
138
139        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom))
140        {
141            maila.Velocity = Vector.Zero;
142            return;
143        }
144
145        maila.Velocity = nopeus;
146    }
147
148    void LisaaLaskurit()
149    {
150        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0, maila1);
151        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0, maila2);
152    }
153
154    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde)
155    {
156        if (maila1.IsDestroyed || maila2.IsDestroyed) return;
157        if (kohde == oikeaReuna) pelaajan1Pisteet.Value += 1;
158        if (kohde == vasenReuna) pelaajan2Pisteet.Value += 1;
159    }
160}
Note: See TracBrowser for help on using the repository browser.