source: 2017/31/PatrikK/pong_peli.cs @ 9356

Revision 9264, 2.7 KB checked in by npo17_73, 6 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
10
11public class Pong : PhysicsGame
12{
13    Vector nopeusYlos = new Vector(0, 200);
14    Vector nopeusAlas = new Vector(0, -200);
15
16    PhysicsObject pallo;
17
18    PhysicsObject maila1;
19    PhysicsObject maila2;
20
21    public override void Begin()
22    {
23        LuoKentta();
24        AloitaPeli();
25        AsetaOhjaimet();
26
27
28    }
29
30    void LuoKentta()
31    {
32        pallo = new PhysicsObject(40.0, 40.0);
33        pallo.Shape = Shape.Circle;
34        pallo.X = -200.0;
35        pallo.Y = 0.0;
36        pallo.Restitution = 1.0;
37        Add(pallo);
38        maila1 = LuoMaila(Level.Left + 20.0, 0.0);
39        maila2 = LuoMaila(Level.Right - 20.0, 0.0);
40        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0);
41        maila.Shape = Shape.Rectangle;
42        maila.X = Level.Left + 20.0;
43        maila.Y = 0.0;
44        maila.Restitution = 1.0;
45
46        Level.CreateBorders(1.0, false);
47        Level.Background.Color = Color.Black;
48        Camera.ZoomToLevel();
49    }
50    void AloitaPeli()
51    {
52        Vector impulssi = new Vector(500.0, 0.0);
53        pallo.Hit(impulssi);
54    }
55    PhysicsObject LuoMaila(double x, double y)
56    {
57        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0);
58        maila.Shape = Shape.Rectangle;
59        maila.X = x;
60        maila.Y = y;
61        maila.Restitution = 1.0;
62        Add(maila);
63        return maila;
64    }
65    void AsetaOhjaimet()
66    {
67        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos);
68        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
69        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas);
70        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
71
72        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos);
73        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
74        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas);
75        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
76
77        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
78
79        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
80
81    }
82    void AsetaNopeus(PhysicsObject maila, Vector nopeus)
83    {
84        maila.Velocity = nopeus;
85    }
86}
Note: See TracBrowser for help on using the repository browser.