source: 2015/30/EemilR/Pong/Pong/Pong/Pong.cs @ 6823

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