source: 2013/27/PihlaM/Pong/Pong/Pong/Pong.cs @ 4350

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