source: 2013/26/MikkoTi/Pong/Pong/Pong/Pong.cs @ 4195

Revision 4195, 3.6 KB checked in by tesatapa, 10 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    Vector nopeusYlös = 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   
22public override void Begin()
23
24{
25        LuoKentta();
26        AsetaOhjaimet();
27        LisaaLaskurit();
28        AloitaPeli();
29
30    }
31
32    void LuoKentta()
33    {
34        Pallo = new PhysicsObject(40.0, 40.0);
35        Pallo.Shape = Shape.Circle;
36        Add(Pallo);
37        Pallo.X = 200.0;
38        Pallo.Y = 0.0;
39        Pallo.Restitution = 1.0;
40
41        Maila1 = LuoMaila(Level.Left + 20.0, 0.0);
42        Maila2 = LuoMaila(Level.Right - 20.0, 0.0);
43
44        Level.CreateBorders(1.0, false);
45        Level.BackgroundColor = Color.Black;
46
47        Camera.ZoomToLevel();
48}
49
50    void AloitaPeli()
51    {
52        Vector Impulssi = new Vector(500.0, 0.0);
53        Pallo.Hit(Impulssi);
54    }
55   
56    PhysicsObject LuoMaila(double x, double y)
57    {
58        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0);
59        maila.Shape = Shape.Rectangle;
60        maila.X = x;
61        maila.Y = y;
62        maila.Restitution = 1.0;
63        Add(maila);
64        return maila;
65    }
66     
67    void LisaaLaskurit()
68        {
69            pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0);
70            pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0);
71        }
72    IntMeter LuoPisteLaskuri(double x, double y)
73    {
74        IntMeter laskuri = new IntMeter(0);
75        laskuri.MaxValue = 10;
76
77        Label naytto = new Label();
78        naytto.BindTo (laskuri);
79        naytto.X = x;
80        naytto.Y = y;
81        naytto.TextColor = Color.White;
82        naytto.BorderColor = Level.BackgroundColor;
83        naytto.Color = Level.BackgroundColor;
84        Add(naytto);
85
86        return laskuri;
87    }
88   
89    void AsetaOhjaimet()
90    {
91        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1 Liikuta Mailaa ylös", Maila1, nopeusYlös);
92        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, Maila1, Vector.Zero);
93        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1 Liikuta Mailaa alas", Maila1, nopeusAlas);
94        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, Maila1, Vector.Zero);
95
96        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2 Liikuta Mailaa Ylös", Maila2, nopeusYlös);
97        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, Maila2, Vector.Zero);
98        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2 Liikuta Mailaa Alas", Maila2, nopeusAlas);
99        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, Maila2, Vector.Zero);
100
101        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
102        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
103        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
104    }
105    void AsetaNopeus(PhysicsObject Maila, Vector nopeus)
106    {
107        if ((nopeus.Y > 0) && (Maila.Top > Level.Top))
108        {
109            Maila.Velocity = Vector.Zero;
110            return;
111        }
112        if ((nopeus.Y < 0) && (Maila.Bottom < Level.Bottom))
113        {
114            Maila.Velocity = Vector.Zero;
115            return;
116        }
117        Maila.Velocity = nopeus;
118    }
119}
Note: See TracBrowser for help on using the repository browser.