Changeset 1984 for 2011


Ignore:
Timestamp:
2011-06-14 13:05:04 (12 years ago)
Author:
jajusaar
Message:

Kaikki tehty, VALMIS!

File:
1 edited

Legend:

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

    r1972 r1984  
    99public class Peli : PhysicsGame 
    1010{ 
     11    Vector nopeusYlos = new Vector(0, 200); 
     12    Vector nopeusAlas = new Vector(0, -200); 
     13 
    1114    PhysicsObject pallo; 
     15    PhysicsObject maila1; 
     16    PhysicsObject maila2; 
     17 
     18    PhysicsObject vasenReuna; 
     19    PhysicsObject oikeaReuna; 
     20 
     21    IntMeter pelaajan1Pisteet; 
     22    IntMeter pelaajan2Pisteet; 
     23 
    1224    public override void Begin() 
    1325    { 
     
    1527        LuoKentta(); 
    1628        AsetaOhjaimet(); 
     29        LisaaLaskurit(); 
    1730        AloitaPeli(); 
    1831 
     
    2942        pallo.X = -200; 
    3043        pallo.Restitution = 1.0; 
    31         Level.CreateBorders(1.0, false); 
     44        vasenReuna = Level.CreateLeftBorder(); 
     45        vasenReuna.Restitution = 1.0; 
     46        vasenReuna.IsVisible = false; 
     47 
     48        oikeaReuna = Level.CreateRightBorder(); 
     49        oikeaReuna.Restitution = 1.0; 
     50        oikeaReuna.IsVisible = false; 
     51 
     52        PhysicsObject alaReuna = Level.CreateBottomBorder(); 
     53        alaReuna.Restitution = 1.0; 
     54        alaReuna.IsVisible = false; 
     55 
     56        PhysicsObject ylaReuna = Level.CreateTopBorder(); 
     57        ylaReuna.Restitution = 1.0; 
     58        ylaReuna.IsVisible = false; 
     59 
    3260        Level.BackgroundColor = Color.Black; 
    3361        Camera.ZoomToLevel(); 
    34         LuoMaila (Level.Left +20.0, 0.0); 
    35         LuoMaila(Level.Right - 20.0, 0.0); 
     62        AddCollisionHandler(pallo, KasittelePallonTormays); 
     63 
     64        MessageDisplay.TextColor = Color.White; 
     65 
     66        maila1 = LuoMaila (Level.Left +20.0, 0.0); 
     67        maila1.Color = Color.Blue; 
     68        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
     69        maila2.Color = Color.Red; 
    3670    } 
    3771    void AloitaPeli() 
     
    4074        pallo.Hit(impulssi); 
    4175    } 
    42     void LuoMaila( double x, double y ) 
     76    PhysicsObject LuoMaila( double x, double y ) 
    4377    { 
    4478        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
     
    4882        maila.Restitution = 1.0; 
    4983        Add(maila); 
     84 
     85        return maila; 
    5086    } 
    5187    void AsetaOhjaimet() 
    5288    { 
    53         Keyboard.Listen( Key.A, ButtonState.Down, LiikutaMailaaYlos, "Pelaaja 1: Liikuta mailaa ylös" ); 
    54         Keyboard.Listen(Key.A, ButtonState.Released, PysaytaMaila, null); 
     89        Keyboard.Listen( Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos ); 
     90        Keyboard.Listen( Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero ); 
     91        Keyboard.Listen( Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas ); 
     92        Keyboard.Listen( Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero ); 
     93 
     94        Keyboard.Listen( Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos ); 
     95        Keyboard.Listen( Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero ); 
     96        Keyboard.Listen( Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2,nopeusAlas ); 
     97        Keyboard.Listen( Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     98 
     99        Keyboard.Listen( Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet" ); 
    55100        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); 
    56101    } 
    57 }   void AsetaNopeus( PhysicObject maila, Vector nopeus) 
    58 { 
    59 } 
     102    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
     103    { 
     104        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     105        { 
     106            maila.Velocity = Vector.Zero; 
     107            return; 
     108        } 
     109         
     110        if ( (nopeus.Y > 0) && (maila.Top > Level.Top) ) 
     111        { 
     112            maila.Velocity = Vector.Zero; 
     113            return; 
     114        } 
     115         
     116        maila.Velocity = nopeus; 
     117  } 
     118    void LisaaLaskurit() 
     119    { 
     120        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 
     121        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 
     122  
     123    } 
     124    IntMeter LuoPisteLaskuri(double x, double y) 
     125    { 
     126        IntMeter laskuri = new IntMeter( 0 ); 
     127        laskuri.MaxValue = 10; 
     128        Label naytto = new Label(); 
     129        naytto.BindTo(laskuri); 
     130        naytto.X = x; 
     131        naytto.Y = y; 
     132        naytto.TextColor = Color.White; 
     133        naytto.BorderColor = Level.BackgroundColor; 
     134        naytto.Color = Level.BackgroundColor; 
     135        Add(naytto); 
     136        return laskuri;  
     137    } 
     138    void KasittelePallonTormays( PhysicsObject pallo, PhysicsObject kohde ) 
     139    { 
     140        if (kohde == oikeaReuna) 
     141        { 
     142            pelaajan1Pisteet.Value += 1; 
     143        } 
     144        else if (kohde == vasenReuna) 
     145        { 
     146            pelaajan2Pisteet.Value += 1; 
     147        } 
     148 
     149 
     150 
     151    }    
     152}    
Note: See TracChangeset for help on using the changeset viewer.