source: 2015/27/LukaP/Pong/Pong/Pong/Pong.cs @ 6526

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