source: 2012/23/HannesM/Pong/Pong/Pong/Pong.cs @ 2873

Revision 2873, 4.7 KB checked in by hasamati, 11 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 nopeusYlos = new Vector(0, 300);
12    Vector nopeusAlas = new Vector(0, -300);
13   
14    PhysicsObject pallo;
15
16    PhysicsObject vasenReuna;
17    PhysicsObject oikeaReuna;
18
19    PhysicsObject maila1;
20    PhysicsObject maila2;
21
22    IntMeter pelaajan1Pisteet;
23    IntMeter pelaajan2Pisteet;
24
25    public override void Begin()
26    {
27        LuoKentta();
28        AsetaOhjaimet();
29        AloitaPeli();
30        LisaaLaskuri();
31    }
32
33    void LuoKentta()
34    {
35        pallo = new PhysicsObject(30, 30);
36        pallo.Shape = Shape.Hexagon;
37        pallo.Color = Color.Green;
38        pallo.X = -200;
39        pallo.Y = 0;
40        pallo.Restitution = 1.0;
41        Add(pallo);
42
43        maila1 = LuoMaila(Level.Left + 20, 0);
44        maila2 = LuoMaila(Level.Right - 20, 0);
45
46        Level.BackgroundColor = Color.Black;
47
48        vasenReuna = Level.CreateLeftBorder();
49        vasenReuna.Restitution = 1.0;
50        vasenReuna.IsVisible = false;
51        oikeaReuna = Level.CreateRightBorder();
52        oikeaReuna.Restitution = 1.0;
53        oikeaReuna.IsVisible = false;
54        PhysicsObject ylaReuna = Level.CreateTopBorder();
55        ylaReuna.Restitution = 1.0;
56        ylaReuna.KineticFriction = 0.0;
57        ylaReuna.IsVisible = false;
58        PhysicsObject alaReuna = Level.CreateBottomBorder();
59        alaReuna.Restitution = 1.0;
60        alaReuna.IsVisible = false;
61        alaReuna.KineticFriction = 0.0;
62
63
64        Camera.ZoomToLevel();
65
66        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
67        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
68
69        AddCollisionHandler(pallo, PallonTormays);
70
71    }
72    void AloitaPeli()
73    {
74        Vector törmäys = new Vector(500, 0);
75        pallo.Hit(törmäys);
76    }
77    PhysicsObject LuoMaila (double x, double y)
78    {
79        PhysicsObject maila = PhysicsObject.CreateStaticObject(10.0, 100.0);
80        maila.Color = Color.Gold;
81        maila.Shape = Shape.Rectangle;
82        maila.X = x;
83        maila.Y = y;
84        maila.Restitution = 1.0;
85        Add(maila);
86
87        return maila;
88    }
89    void AsetaOhjaimet()
90    {
91        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos);
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, nopeusYlos);
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        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "poistu");
103    }
104    void AsetaNopeus(PhysicsObject maila, Vector nopeus)
105    {
106        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom))
107        {
108            maila.Velocity = Vector.Zero;
109            return;
110        }
111        if ((nopeus.Y > 0) && (maila.Top > Level.Top))
112        {
113            maila.Velocity = Vector.Zero;
114            return;
115
116        }
117
118        maila.Velocity = nopeus;
119    }
120    void LisaaLaskuri()
121    {
122        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left +100, Screen.Top - 100);
123        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100, Screen.Top - 100);
124    }
125    IntMeter LuoPisteLaskuri(double x, double y)
126    {
127        IntMeter laskuri = new IntMeter(0);
128        laskuri.MaxValue = 5;
129        Label naytto = new Label();
130        naytto.BindTo(laskuri);
131        naytto.X = x;
132        naytto.Y = y;
133        naytto.TextColor = Color.Red;
134        naytto.BorderColor = Level.BackgroundColor;
135        naytto.Color = Level.BackgroundColor;
136        Add(naytto);
137
138        return laskuri;
139
140    }
141    void PallonTormays(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.