Changeset 850


Ignore:
Timestamp:
2010-06-15 12:49:28 (9 years ago)
Author:
maiilomo
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 2010/24/maiilomo/pong/Peli.cs

    r848 r850  
    55public class Peli : PhysicsGame 
    66{ 
     7    Vector nopeusYlos = new Vector(0, 200); 
     8    Vector nopeusAlas = new Vector(0, -200); 
     9 
    710    PhysicsObject pallo; 
     11    PhysicsObject maila1; 
     12    PhysicsObject maila2; 
    813 
    914    protected override void Begin() 
    10                 { 
    11             LuoKentta(); 
    12             Keyboard.Listen( Key.Escape, ButtonState.Pressed, Exit, "Poistu" ); 
    13             AloitaPeli(); 
    14             Asetaohjaimet(); 
     15    { 
     16        LuoKentta(); 
     17        AsetaOhjaimet(); 
     18        AloitaPeli(); 
    1519    } 
    1620 
    1721    void LuoKentta() 
    1822    { 
    19         pallo = new PhysicsObject( 40, 40 ); 
     23        pallo = new PhysicsObject(40.0, 40.0); 
    2024        pallo.Shape = Shapes.Circle; 
    21         pallo.X = 0.0; 
     25        pallo.X = -200.0; 
    2226        pallo.Y = 0.0; 
    2327        pallo.Restitution = 1.0; 
    2428        pallo.Color = Color.Green; 
    25         Add( pallo ); 
     29        Add(pallo); 
    2630 
    27         LuoMaila( Level.Left + 20.0, 0.0 ); 
    28         LuoMaila( Level.Right - 20.0, 0.0 ); 
     31        maila1 = LuoMaila(Level.Left + 20.0, 0.0); 
     32        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
    2933 
    30         Level.CreateBorders( 1.0, false ); 
     34        Level.CreateBorders(1.0, false); 
    3135        Level.BackgroundColor = Color.Black; 
    3236 
     
    3438    } 
    3539 
    36     void AloitaPeli() 
     40    PhysicsObject LuoMaila(double x, double y) 
    3741    { 
    38         Vector impulssi = new Vector( 500.0, 0.0 ); 
    39         pallo.Hit( impulssi ); 
    40     } 
    41  
    42     void LuoMaila( double x, double y ) 
    43     { 
    44         PhysicsObject maila = PhysicsObject.CreateStaticObject( 20.0, 100.0 ); 
     42        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
    4543        maila.Shape = Shapes.Rectangle; 
    4644        maila.X = x; 
    4745        maila.Y = y; 
    4846        maila.Restitution = 1.0; 
    49         maila.Color = Color.Red; 
    50         Add( maila ); 
     47 
     48        Add(maila); 
     49        return maila; 
    5150    } 
    5251 
    53     void asetaohjaimet() 
     52    void AloitaPeli() 
    5453    { 
     54        Vector impulssi = new Vector(500.0, 0.0); 
     55        pallo.Hit(impulssi); 
     56    } 
    5557 
     58    void AsetaOhjaimet() 
     59    { 
     60        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 
     61        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     62        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 
     63        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
    5664 
     65        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 
     66        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     67        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 
     68        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     69 
     70        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 
     71        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); 
     72 
     73        ControllerOne.Listen(Button.DPadUp, ButtonState.Down, AsetaNopeus, "Liikuta mailaa ylös", maila1, nopeusYlos); 
     74        ControllerOne.Listen(Button.DPadUp, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     75        ControllerOne.Listen(Button.DPadDown, ButtonState.Down, AsetaNopeus, "Liikuta mailaa alas", maila1, nopeusAlas); 
     76        ControllerOne.Listen(Button.DPadDown, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     77 
     78        ControllerTwo.Listen(Button.DPadUp, ButtonState.Down, AsetaNopeus, "Liikuta mailaa ylös", maila2, nopeusYlos); 
     79        ControllerTwo.Listen(Button.DPadUp, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     80        ControllerTwo.Listen(Button.DPadDown, ButtonState.Down, AsetaNopeus, "Liikuta mailaa alas", maila2, nopeusAlas); 
     81        ControllerTwo.Listen(Button.DPadDown, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     82 
     83        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu"); 
     84        ControllerTwo.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu"); 
     85    } 
     86 
     87    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
     88    { 
     89        if ((nopeus.Y < 0) && (maila.Y < Level.Bottom)) 
     90        { 
     91            maila.Velocity = Vector.Zero; 
     92            return; 
     93        } 
     94        if ((nopeus.Y > 0) && (maila.Y > Level.Top)) 
     95        { 
     96            maila.Velocity = Vector.Zero; 
     97            return; 
     98        } 
     99 
     100        maila.Velocity = nopeus; 
    57101    } 
    58102} 
Note: See TracChangeset for help on using the changeset viewer.