source: 2010/30/oosaelka/Pong/Peli.cs @ 1278

Revision 1278, 4.4 KB checked in by oosaelka, 13 years ago (diff)

Pong peli vaiheen 7 puoliväliin.

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