source: 2010/27/frjolehm/Ping Pong/Ping Pong/Peli.cs @ 1067

Revision 1067, 3.6 KB checked in by frjolehm, 13 years ago (diff)

Uusi keskeneräinen Ping Pong peli.

Line 
1using System;
2using Jypeli;
3using Jypeli.Widgets;
4using Jypeli.Assets;
5
6public class Peli : PhysicsGame
7{
8    Vector nopeusYlös = new Vector(0, 250);
9    Vector nopeusAlas = new Vector(0, -250);
10
11    //Liikuvat kohteeet
12    PhysicsObject pallo;
13    PhysicsObject maila1;
14    PhysicsObject maila2;
15
16    IntMeter pelaaja1Pisteet;
17    IntMeter pelaaja2Pisteet;
18
19    protected override void Begin()
20    {
21        //=)
22
23        LuoKenttä();
24        Ohjaimet();
25        LisääLaskurit();
26        AloitaPeli();
27
28    }
29    //Pelin luonti
30    void LuoKenttä()
31    {
32        //mailan luonti
33        maila1 = LuoMaila(Level.Left + 30, 0, 1.0);
34        maila2 = LuoMaila(Level.Right - 30, 0, 1.0);
35        //pallon luonti
36        pallo = new PhysicsObject(35, 35);
37        pallo.Shape = Shapes.Triangle;
38        pallo.Color = Color.Cyan;
39        pallo.X = 0;
40        pallo.Y = 0;
41        pallo.Restitution = 1.0;
42        Add(pallo);
43        //taso
44        Level.CreateBorders(1.0, false);
45        Level.BackgroundColor = Color.Black;
46
47        Camera.ZoomToLevel();
48
49    }
50    void AloitaPeli()
51    {
52        Vector impullsi = new Vector(200, 0);
53        pallo.Hit(impullsi);
54    }
55    PhysicsObject LuoMaila(double x, double y, double kimmoisuus)
56    {
57        PhysicsObject maila = PhysicsObject.CreateStaticObject(25, 150);
58        maila.Shape = Shapes.Rectangle;
59        maila.X = x;
60        maila.Y = y;
61        maila.Restitution = kimmoisuus;
62        Add(maila);
63        return maila;
64    }
65    //Ohjamiet
66    void Ohjaimet()
67    {
68        //Yleiset
69        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu");
70        Keyboard.Listen(Key.H, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
71
72        //Pelaaja 1
73        Keyboard.Listen(Key.A, ButtonState.Down, LaitaNopeus, "Pelaaja 1: Liikuta mailaa ylöspän.", maila1, nopeusYlös);
74        Keyboard.Listen(Key.A, ButtonState.Released, LaitaNopeus, null, maila1, Vector.Zero);
75        Keyboard.Listen(Key.Z, ButtonState.Down, LaitaNopeus, "Pelaaja 1: Liikuta mailaa alaspäin.", maila1, nopeusAlas);
76        Keyboard.Listen(Key.Z, ButtonState.Released, LaitaNopeus, null, maila1, Vector.Zero);
77        //Pelaaja 2
78        Keyboard.Listen(Key.Up, ButtonState.Down, LaitaNopeus, "Pelaaja 2: Liikuta mailaa ylöspäin.", maila2, nopeusYlös);
79        Keyboard.Listen(Key.Up, ButtonState.Released, LaitaNopeus, null, maila2, Vector.Zero);
80        Keyboard.Listen(Key.Down, ButtonState.Down, LaitaNopeus, "Pelaaja 2: Liikuta mailaa alaspäin.", maila2, nopeusAlas);
81        Keyboard.Listen(Key.Down, ButtonState.Released, LaitaNopeus, null, maila2, Vector.Zero);
82    }
83    void LaitaNopeus(PhysicsObject maila, Vector nopeus)
84    {
85        if ((nopeus.Y < 0) && (maila.Y < Level.Bottom))
86        {
87            maila.Velocity = Vector.Zero;
88            return;
89        }
90        if ((nopeus.Y > 0) && (maila.Y > Level.Top))
91        {
92            maila.Velocity = Vector.Zero;
93            return;
94        }
95
96        maila.Velocity = nopeus;
97    }
98
99    //Piste laskurit
100    void LisääLaskurit()
101    {
102        pelaaja1Pisteet = LuoPisteLaskuri(Screen.Left + 50, Screen.Top - 50);
103        pelaaja2Pisteet = LuoPisteLaskuri(Screen.Right - 50, Screen.Top - 50);
104    }
105    IntMeter LuoPisteLaskuri(double x, double y)
106    {
107        IntMeter laskuri = new IntMeter(0);
108        laskuri.MaxValue = 10;
109
110        Label näyttö = new Label();
111        näyttö.BindTo(laskuri);
112        näyttö.X = x;
113        näyttö.Y = y;
114        näyttö.TextColor = Color.Cyan;
115        Add(näyttö);
116
117        return laskuri;
118    }
119
120
121}
Note: See TracBrowser for help on using the repository browser.