Ignore:
Timestamp:
2011-06-17 13:53:13 (12 years ago)
Author:
lakaharj
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 2011/24/LauraH/Pong/Pong/Pong/Peli.cs

    r1970 r2116  
    99public class Peli : PhysicsGame 
    1010{ 
     11    Vector nopeusYlos = new Vector(0, 200); 
     12    Vector nopeusAlas = new Vector(0, -200); 
    1113 
    1214    PhysicsObject pallo; 
     15    PhysicsObject maila1; 
     16    PhysicsObject maila2; 
    1317 
     18    PhysicsObject vasenReuna; 
     19    PhysicsObject oikeaReuna; 
    1420 
     21    IntMeter pelaajan1Pisteet; 
     22    IntMeter pelaajan2Pisteet; 
    1523 
    1624    public override void Begin() 
    1725    { 
    1826        LuoKentta(); 
     27        AsetaOhjaimet(); 
     28        LisaaLaskurit(); 
    1929        AloitaPeli(); 
    2030 
     
    3343        pallo.X = -200.0; 
    3444        pallo.Y = 0.0; 
    35         Level.CreateBorders(1.0, false); 
     45       vasenReuna = Level.CreateLeftBorder(); 
     46        vasenReuna.Restitution = 1.0; 
     47        vasenReuna.IsVisible = false; 
    3648        pallo.Restitution = 1.0; 
    3749        Level.BackgroundColor = Color.Black; 
    3850        Camera.ZoomToLevel(); 
    39         LuoMaila(Level.Left + 20.0, 0.0); 
    40         LuoMaila(Level.Right - 20.0, 0.0); 
     51        maila1 = LuoMaila(Level.Left + 20.0, 0.0); 
     52        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
     53        MessageDisplay.TextColor = Color.Gray; 
     54        AddCollisionHandler(pallo, KasittelePallonTormays); 
     55        PhysicsObject alaReuna = Level.CreateBottomBorder(); 
     56        alaReuna.Restitution = 1.0; 
     57        alaReuna.IsVisible = false; 
     58        oikeaReuna = Level.CreateRightBorder(); 
     59        oikeaReuna.Restitution = 1.0; 
     60        oikeaReuna.IsVisible = false; 
     61        PhysicsObject ylaReuna = Level.CreateTopBorder(); 
     62        ylaReuna.Restitution = 1.0; 
     63        ylaReuna.IsVisible = false; 
    4164    } 
    4265    void AloitaPeli() 
     
    4669    } 
    4770 
    48     void LuoMaila(double x, double y) 
     71    PhysicsObject LuoMaila(double x, double y) 
    4972    { 
    5073        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
     
    5679        maila.Color = Color.Cyan; 
    5780 
     81        return maila; 
     82    } 
     83    void AsetaOhjaimet() 
     84    { 
     85 
     86        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja1: Liikuta mailaa ylös", maila1, nopeusYlos); 
     87        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     88        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 
     89        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     90 
     91        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 
     92        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     93        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 
     94        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     95 
     96        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 
     97 
     98        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); 
     99 
     100 
     101 
     102    } 
     103 
     104    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
     105    { 
     106        maila.Velocity = nopeus; 
     107        if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 
     108        { 
     109            maila.Velocity = Vector.Zero; 
     110            return; 
     111        } 
     112        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     113        { 
     114            maila.Velocity = Vector.Zero; 
     115            return; 
     116        } 
     117 
     118        maila.Velocity = nopeus; 
     119 
     120    } 
     121 
     122    void LisaaLaskurit() 
     123    { 
     124        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 
     125        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 
     126    } 
     127    IntMeter LuoPisteLaskuri(double x, double y) 
     128    { 
     129        IntMeter laskuri = new IntMeter(0); 
     130        laskuri.MaxValue = 10; 
     131        Label naytto = new Label(); 
     132        naytto.BindTo(laskuri); 
     133        naytto.X = x; 
     134        naytto.Y = y; 
     135        naytto.TextColor = Color.Gray; 
     136        naytto.BorderColor = Level.BackgroundColor; 
     137        naytto.Color = Level.BackgroundColor; 
     138        Add(naytto); 
     139        return laskuri; 
     140 
     141    } 
     142 
     143    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 
     144    { 
     145        if (kohde == oikeaReuna) 
     146        { 
     147            pelaajan1Pisteet.Value += 1; 
     148        } 
     149        else if (kohde == vasenReuna) 
     150        { 
     151            pelaajan2Pisteet.Value += 1; 
     152        } 
    58153    } 
    59154} 
Note: See TracChangeset for help on using the changeset viewer.