source: 2012/30/TuomasW/Pong/Pong/Pong/Pong.cs @ 3691

Revision 3691, 5.1 KB checked in by juiitamm, 11 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 NopeusYlös = new Vector(0, 200);
12    Vector NopeusAlas = new Vector(0, -200);
13
14
15
16    PhysicsObject Pallo;
17
18
19    PhysicsObject Maila1;
20    PhysicsObject Maila2;
21
22    PhysicsObject VasenReuna;
23    PhysicsObject OikeaReuna;
24
25
26    IntMeter Pelaajan1Pisteet;
27    IntMeter Pelaajan2Pisteet;
28
29
30    public override void Begin()
31    {
32        LuoKenttä();
33        AloitaPeli();
34        AsetaOhjaimet();
35        LisääLaskurit();
36
37
38        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
39
40
41    }
42
43        void LuoKenttä ()
44        {
45            Pallo = new PhysicsObject(50, 50);
46            Pallo.Shape = Shape.Circle;
47            Add(Pallo);
48            Pallo.X = -200;
49            Pallo.Y = 0;
50            Pallo.Restitution = 1;
51
52
53            Maila1 = LuoMaila(Level.Left + 20, 0);
54            Maila2 = LuoMaila(Level.Right - 20, 0);
55
56
57
58            VasenReuna = Level.CreateLeftBorder();
59            VasenReuna.Restitution = 1;
60            VasenReuna.IsVisible = false;
61
62            OikeaReuna = Level.CreateRightBorder();
63            OikeaReuna.Restitution = 1;
64            OikeaReuna.IsVisible = false;
65
66            PhysicsObject AlaReuna = Level.CreateBottomBorder();
67            AlaReuna.Restitution = 1;
68            AlaReuna.IsVisible = false;
69
70            PhysicsObject YläReuna = Level.CreateTopBorder();
71            YläReuna.Restitution = 1;
72            YläReuna.IsVisible = false;
73
74
75            Level.BackgroundColor = Color.Black;
76
77            Camera.ZoomToLevel();
78
79            AddCollisionHandler(Pallo, KäsittelePallonTörmäys);
80        }
81
82        void AloitaPeli()
83        {
84            Vector impulssi = new Vector(500, 0);
85            Pallo.Hit(impulssi);
86        }
87
88            PhysicsObject LuoMaila (double x, double y)
89        {
90
91            PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0);
92            maila.Shape = Shape.Rectangle;
93            maila.X = x;
94            maila.Y = y;
95            maila.Restitution = 1.0;
96            Add(maila);
97            return maila;
98
99        }
100
101            void AsetaOhjaimet()
102            {
103                Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", Maila1, NopeusYlös);
104                Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, Maila1, Vector.Zero);
105                Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", Maila1, NopeusAlas);
106                Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, Maila1, Vector.Zero);
107
108                Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", Maila2, NopeusYlös);
109                Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, Maila2, Vector.Zero);
110                Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", Maila2, NopeusAlas);
111                Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, Maila2, Vector.Zero);
112
113
114                Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
115
116            }
117
118            void AsetaNopeus(PhysicsObject Maila, Vector Nopeus)
119            {
120
121                if ((Nopeus.Y < 0) && (Maila.Bottom < Level.Bottom))
122                {
123                    Maila.Velocity = Vector.Zero;
124                    return;
125                }
126
127                if ((Nopeus.Y > 0) && (Maila.Top > Level.Top))
128                {
129                    Maila.Velocity = Vector.Zero;
130                    return;
131                }
132                Maila.Velocity = Nopeus;
133            }
134
135            IntMeter LuoPisteLaskuri(double x, double y)
136            {
137                IntMeter Laskuri = new IntMeter(0);
138                Laskuri.MaxValue = 10;
139
140                Label Näyttö = new Label();
141                Näyttö.BindTo(Laskuri);
142                Näyttö.X = x;
143                Näyttö.Y = y;
144                Näyttö.TextColor = Color.White;
145                Näyttö.BorderColor = Level.BackgroundColor;
146                Näyttö.Color = Level.BackgroundColor;
147                Add(Näyttö);
148
149                return Laskuri;
150            }
151
152            void LisääLaskurit()
153            {
154                Pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100, Screen.Top - 100);
155                Pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100, Screen.Top - 100);
156
157            }
158
159
160            void KäsittelePallonTörmäys(PhysicsObject Pallo, PhysicsObject kohde)
161            {
162                if (kohde == OikeaReuna)
163                {
164                    Pelaajan1Pisteet.Value += 1;
165
166                }
167                else if (kohde == VasenReuna)
168                {
169                    Pelaajan2Pisteet.Value +=1;
170                }
171
172
173            }
174
175
176
177        }
178
Note: See TracBrowser for help on using the repository browser.