source: 2010/24/danmarti/Pong/Peli.cs @ 811

Revision 811, 2.8 KB checked in by sijoseha, 13 years ago (diff)

Pong vaiheet 1-6.

Line 
1using System;
2using Jypeli;
3using Jypeli.ScreenObjects;
4using Jypeli.Assets;
5
6
7public class Peli : PhysicsGame
8{
9    PhysicsObject pallo;
10    PhysicsObject maila1;
11    PhysicsObject maila2;
12
13    Vector nopeusYlos = new Vector(0, 200);
14    Vector nopeusAlas = new Vector(0, -200);
15
16    protected override void Begin()
17    {
18        //TODO: Alusta peli tässä
19
20        LuoKentta();
21        AloitaPeli();
22        AsetaOhjaimet();
23
24        Vector impulssi = new Vector(500.0, 0.0);
25        pallo.Hit(impulssi);
26       
27
28    }
29
30    void LuoKentta()
31    {
32        pallo = new PhysicsObject( 50, 50 );
33        pallo.Shape = Shapes.Triangle;
34        Add( pallo );
35        pallo.Restitution = 1.1;
36        pallo.Color = Color.Black;
37
38        maila1 = LuoMaila( Level.Left + 20.0, 0.0 );
39        maila2 = LuoMaila( Level.Right - 20.0, 0.0 );
40
41        Level.CreateBorders(1.0, false);
42        Level.BackgroundColor = Color.White;
43
44        Camera.ZoomToLevel();
45
46    }
47
48    PhysicsObject LuoMaila(double x, double y)
49    {
50        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0);
51        maila.Shape = Shapes.Rectangle;
52        maila.X = x;
53        maila.Y = y;
54        maila.Restitution = 1.0;
55        Add(maila);
56        maila.Color = Color.Black;
57
58        return maila;
59
60    }
61
62    void AloitaPeli()
63    {
64        Vector impulssi = new Vector(500.0, 0.0);
65        pallo.Hit( impulssi);
66    }
67
68    void AsetaOhjaimet()
69    {
70        Keyboard.Listen( Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos );
71        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
72
73        Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas);
74        Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
75
76        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos);
77        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
78
79        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas);
80        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
81
82   
83
84        Keyboard.Listen( Key.Escape, ButtonState.Pressed, Exit, "Poistu" );
85
86    }
87
88    void AsetaNopeus(PhysicsObject maila, Vector nopeus)
89    {
90        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom))
91        {
92            maila.Velocity = Vector.Zero;
93            return;
94        }
95        if ((nopeus.Y > 0) && (maila.Top > Level.Top))
96        {
97            maila.Velocity = Vector.Zero;
98            return;
99        }
100
101        maila.Velocity = nopeus;
102    }
103
104    }
105
Note: See TracBrowser for help on using the repository browser.