source: 2012/23/OsamaA/MyTemp/Pong/Pong/Pong/Pong.cs @ 2779

Revision 2779, 4.9 KB checked in by osomalab, 11 years ago (diff)

Pong lähes valmis

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