Changeset 3444


Ignore:
Timestamp:
2012-07-02 14:55:30 (8 years ago)
Author:
jejokiil
Message:

Talletus.

Location:
2012/27/JesseK
Files:
13 added
1 edited

Legend:

Unmodified
Added
Removed
  • 2012/27/JesseK/Pong/Pong/Pong/Pong.cs

    r3426 r3444  
    99public class Pong : 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    PhysicsObject vasenReuna; 
     18    PhysicsObject oikeaReuna; 
     19 
     20    IntMeter pelaajan1Pisteet; 
     21    IntMeter pelaajan2Pisteet; 
     22  
    1223 
    1324    public override void Begin() 
     
    1526        LuoKentta(); 
    1627        AloitaPeli(); 
     28        LisaaLaskurit(); 
     29        AsetaOhjaimet(); 
    1730         
    1831         
     
    2639        pallo.Y = 0; 
    2740        pallo.Restitution = 1.0; 
     41        pallo.KineticFriction = 0; 
     42        pallo.CanRotate = true; 
    2843        Add(pallo); 
    2944 
    30         LuoMaila(Level.Left + 20.0, 0.0); 
    31         LuoMaila(Level.Right - 20.0, 0.0); 
     45        AddCollisionHandler(pallo, KasittelePallonTormays); 
    3246 
    33         Level.CreateBorders(1.0, false); 
     47        maila1 = LuoMaila(Level.Left + 20.0, 0.0); 
     48        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
     49 
     50        vasenReuna = Level.CreateLeftBorder(); 
     51        vasenReuna.Restitution = 1.0; 
     52        vasenReuna.KineticFriction = 0; 
     53        vasenReuna.IsVisible = false; 
     54        oikeaReuna = Level.CreateRightBorder(); 
     55        oikeaReuna.Restitution = 1.0; 
     56        vasenReuna.KineticFriction = 0; 
     57        oikeaReuna.IsVisible = false; 
     58        PhysicsObject ylaReuna = Level.CreateTopBorder(); 
     59        ylaReuna.Restitution = 1.0; 
     60        ylaReuna.KineticFriction = 0; 
     61        ylaReuna.IsVisible = false; 
     62        PhysicsObject alaReuna = Level.CreateBottomBorder(); 
     63        alaReuna.Restitution = 1.0; 
     64        alaReuna.KineticFriction = 0; 
     65        alaReuna.IsVisible = false; 
    3466        Level.BackgroundColor = Color.Black; 
    3567 
     
    4173        pallo.Hit(impulssi); 
    4274    } 
    43     void LuoMaila(double x, double y) 
     75    PhysicsObject LuoMaila(double x, double y) 
    4476    { 
    4577        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
     
    4981        maila.Y = y; 
    5082        maila.Restitution = 1.0; 
     83        maila.KineticFriction = 0; 
    5184        Add(maila); 
     85        return maila; 
    5286    } 
    5387    void AsetaOhjaimet() 
    5488    { 
    5589        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     90        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: liikuta mailaa ylös", maila1, nopeusYlos); 
     91        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     92        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: liikuta mailaa alas", maila1, nopeusAlas); 
     93        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     94 
     95        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: liikuta mailaa ylös", maila2, nopeusYlos); 
     96        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     97        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: liikuta mailaa alas", maila2, nopeusAlas); 
     98        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     99        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 
     100 
     101        ControllerOne.Listen(Button.DPadUp, ButtonState.Down, AsetaNopeus, "Liikuta mailaa ylös", maila1, nopeusYlos); 
     102        ControllerOne.Listen(Button.DPadUp, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     103        ControllerOne.Listen(Button.DPadDown, ButtonState.Down, AsetaNopeus, "Liikuta mailaa alas", maila1, nopeusAlas); 
     104        ControllerOne.Listen(Button.DPadDown, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     105        ControllerTwo.Listen(Button.DPadUp, ButtonState.Down, AsetaNopeus, "Liikuta mailaa ylös", maila2, nopeusYlos); 
     106        ControllerTwo.Listen(Button.DPadUp, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     107        ControllerTwo.Listen(Button.DPadDown, ButtonState.Down, AsetaNopeus, "Liikuta mailaa alas", maila2, nopeusAlas); 
     108        ControllerTwo.Listen(Button.DPadDown, ButtonState.Released, AsetaNopeus, null, maila2, nopeusAlas); 
     109        ControllerTwo.Listen(Button.Back, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     110        ControllerOne.Listen(Button.Back, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    56111    } 
     112    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
     113    { 
     114        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     115        { 
     116            maila.Velocity = Vector.Zero; 
     117            return; 
     118        } 
     119        if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 
     120        { 
     121            maila.Velocity = Vector.Zero; 
     122            return; 
     123        } 
     124        maila.Velocity = nopeus; 
     125    } 
     126    void LisaaLaskurit() 
     127    { 
     128        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 
     129        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 
     130    } 
     131    IntMeter LuoPisteLaskuri(double x, double y) 
     132    { 
     133        IntMeter laskuri = new IntMeter(0); 
     134        laskuri.MaxValue = 10; 
    57135 
     136        Label naytto = new Label(); 
     137        naytto.BindTo(laskuri); 
     138        naytto.X = x; 
     139        naytto.Y = y; 
     140        naytto.TextColor = Color.White; 
     141        naytto.BorderColor = Level.BackgroundColor; 
     142        naytto.Color = Level.BackgroundColor; 
     143        Add(naytto); 
     144 
     145        return laskuri; 
     146    } 
     147    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 
     148    { 
     149        if (kohde == oikeaReuna) 
     150        { 
     151            pelaajan1Pisteet.Value += 1; 
     152        } 
     153        else if (kohde == vasenReuna) 
     154        { 
     155            pelaajan2Pisteet.Value += 1; 
     156        } 
     157    } 
    58158} 
Note: See TracChangeset for help on using the changeset viewer.