source: 2010/23/leperoih/Pong/Peli.cs @ 509

Revision 509, 3.2 KB checked in by leperoih, 13 years ago (diff)

tein aika paljon tavaraa.

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