source: 2017/27/OskariK/Pong/Pong/Pong/Pong.cs @ 8863

Revision 8863, 4.9 KB checked in by npo17_42, 6 years ago (diff)

tekeminen jatkuu.

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