source: 2013/30/AleksiK/Pong/Pong/Pong/Pong.cs @ 4511

Revision 4511, 4.3 KB checked in by alraniko, 10 years ago (diff)

Talletus.

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