source: werner_v/Pong-1/Peli.cs @ 229

Revision 229, 6.5 KB checked in by wevantti, 14 years ago (diff)

Pong valmis

Line 
1#region Usings
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using Microsoft.Xna.Framework;
6using Microsoft.Xna.Framework.Audio;
7using Microsoft.Xna.Framework.Content;
8using Microsoft.Xna.Framework.Graphics;
9using Microsoft.Xna.Framework.Input;
10using Microsoft.Xna.Framework.Media;
11using Jypeli;
12using Jypeli.ScreenObjects;
13using Jypeli.Assets;
14using AdvanceMath;
15using Physics2DDotNet;
16using Physics2DDotNet.Shapes;
17#endregion
18
19namespace Pong
20{
21    public class Peli : PhysicsGame
22    {
23        PhysicsObject pallo;
24        PhysicsObject maila1;
25        PhysicsObject maila2;
26        Vector2D nopeusYlos = new Vector2D(0, 400);
27        Vector2D nopeusAlas = new Vector2D(0, -400);
28
29        Meter<int> Pelaajan1Pisteet;
30        Meter<int> Pelaajan2Pisteet;
31
32        void LisaaLaskurit()
33        {
34            Pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0);
35            Pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0);
36        }
37
38        Meter<int> LuoPisteLaskuri(double x, double y)
39        {
40            Meter<int> laskuri = new Meter<int>(0, 0, 10);
41            ValueDisplay naytto = new ValueDisplay(this);
42            naytto.BindTo(laskuri);
43            naytto.X = x;
44            naytto.Y = y;
45            naytto.ValueColor = Color.White;
46            Add(naytto);
47            return laskuri;
48        }
49
50        protected override void LoadContent()
51        {
52            Level = LuoKentta();
53            AsetaOhjaimet();
54            LisaaLaskurit();
55            AloitaPeli();
56        }
57        void KasittelePallonTormays(Collision collision)
58        {
59            PhysicsObject pallo = collision.Obj;
60            PhysicsObject kohde = collision.Other;
61
62            if (kohde == Level.RightBorder)
63            {
64                Pelaajan1Pisteet.Value += 1;
65            }
66            else if (kohde == Level.LeftBorder)
67            {
68                Pelaajan2Pisteet.Value += 1;
69            }
70        }
71        Level LuoKentta()
72        {
73            Level kentta = new Level(this);
74            kentta.BackgroundColor = Color.Black;
75
76            IShape ympyra = Shapes.CreateCircle(20.0);
77            pallo = new PhysicsObject(10.0, ympyra);
78            pallo.X = -200.0;
79            pallo.Y = 0.0;
80            pallo.Restitution = 1.0;
81            kentta.Objects.Add(pallo);
82            AddCollisionHandler(pallo, KasittelePallonTormays);
83
84            maila1 = LuoMaila(kentta.Left + 20.0, 0.0, kentta);
85            maila2 = LuoMaila(kentta.Right - 20.0, 0.0, kentta);
86            kentta.CreateBorder(1.0, false);
87
88            return kentta;
89        }
90        void AloitaPeli()
91        {
92            Vector2D impulssi = new Vector2D(5000.0, 0.0);
93            pallo.Hit(impulssi);
94        }
95        PhysicsObject LuoMaila(double x, double y, Level kentta)
96        {
97            IShape suorakulmio = Shapes.CreateRectangle(20.0, 100.0);
98            PhysicsObject maila = PhysicsObject.CreateStaticObject(suorakulmio);
99            maila.X = x;
100            maila.Y = y;
101            maila.Restitution = 1.0;
102            kentta.Objects.Add(maila);
103            return maila;
104        }
105        void AsetaOhjaimet()
106        {
107            Controls.Listen(Keys.A, ButtonPosition.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos);
108            Controls.Listen(Keys.A, ButtonPosition.Released, AsetaNopeus, null, maila1, Vector2D.Zero);
109            Controls.Listen(Keys.Z, ButtonPosition.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas);
110            Controls.Listen(Keys.Z, ButtonPosition.Released, AsetaNopeus, null, maila1, Vector2D.Zero);
111
112            Controls.Listen(Keys.Up, ButtonPosition.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos);
113            Controls.Listen(Keys.Up, ButtonPosition.Released, AsetaNopeus, null, maila2, Vector2D.Zero);
114            Controls.Listen(Keys.Down, ButtonPosition.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas);
115            Controls.Listen(Keys.Down, ButtonPosition.Released, AsetaNopeus, null, maila2, Vector2D.Zero);
116            AsetaGamePadOhjaimet(PlayerIndex.One, maila1);
117            AsetaGamePadOhjaimet(PlayerIndex.Two, maila2);
118        }
119        void AsetaGamePadOhjaimet(PlayerIndex pelaajaNumero, PhysicsObject maila)
120        {
121            Controls.Listen(pelaajaNumero, Buttons.DPadUp, ButtonPosition.Down, AsetaNopeus, "Liikuta mailaa ylös", maila, nopeusYlos);
122            Controls.Listen(pelaajaNumero, Buttons.DPadUp, ButtonPosition.Released, AsetaNopeus, null, maila, Vector2D.Zero);
123            Controls.Listen(pelaajaNumero, Buttons.DPadDown, ButtonPosition.Down, AsetaNopeus, "Liikuta mailaa alas", maila, nopeusAlas);
124            Controls.Listen(pelaajaNumero, Buttons.DPadDown, ButtonPosition.Released, AsetaNopeus, null, maila, Vector2D.Zero);
125        }
126        bool AsetaNopeus(ControlEvent e)
127        {
128            PhysicsObject maila = e.Parameter0.ToPhysicsObject();
129            Vector2D nopeus = e.Parameter1.ToVector2D();
130
131            if ((nopeus.Y < 0) && (maila.Y < Level.Bottom))
132            {
133                maila.Velocity = Vector2D.Zero;
134                return false;
135            }
136            if ((nopeus.Y > 0) && (maila.Y > Level.Top))
137            {
138                maila.Velocity = Vector2D.Zero;
139                return false;
140            }
141
142            maila.Velocity = nopeus;
143
144            return false;
145        }
146        /*bool LiikutaMailaaYlos( ControlEvent e )
147        {
148            PhysicsObject maila = e.Parameter0.ToPhysicsObject();
149            if (maila.Y >= Level.Top)
150            {
151                maila.Velocity = Vector2D.Zero;
152                return false;
153            }
154            Vector2D nopeus = new Vector2D(0, 200);
155            maila.Velocity = nopeus;
156            return false;
157        }
158        bool LiikutaMailaaAlas(ControlEvent e)
159        {
160            PhysicsObject maila = e.Parameter0.ToPhysicsObject();
161            if (maila.Y <= Level.Bottom)
162            {
163                maila.Velocity = Vector2D.Zero;
164                return false;
165            }
166            Vector2D nopeus = new Vector2D(0, -200);
167            maila.Velocity = nopeus;
168            return false;
169        }
170        bool PysaytaMaila( ControlEvent e )
171        {
172            PhysicsObject maila = e.Parameter0.ToPhysicsObject();
173            maila.Velocity = Vector2D.Zero;
174            return false;
175        }*/
176    }
177}
Note: See TracBrowser for help on using the repository browser.