source: 2010/24/joaamaka/Pong/Peli.cs @ 805

Revision 805, 2.8 KB checked in by joaamaka, 13 years ago (diff)

Tein Pong-peliä vaiheeseen 6 asti.

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