Changeset 3172 for 2012/26/EliasY


Ignore:
Timestamp:
2012-06-25 14:59:51 (7 years ago)
Author:
anlakane
Message:

Talletus.

Location:
2012/26/EliasY
Files:
47 added
2 edited

Legend:

Unmodified
Added
Removed
  • 2012/26/EliasY/Pong/Pong/Pong/Pong.cs

    r3143 r3172  
    1111public class Pong : PhysicsGame 
    1212{ 
     13    Vector nopeusYlos = new Vector(0, 200); 
     14    Vector nopeusAlas = new Vector(0, -200); 
     15 
    1316    PhysicsObject pallo; 
     17    PhysicsObject maila1; 
     18    PhysicsObject maila2; 
     19 
     20    PhysicsObject vasenReuna; 
     21    PhysicsObject oikeaReuna; 
     22 
     23    IntMeter pelaajan1Pisteet; 
     24    IntMeter pelaajan2Pisteet; 
    1425 
    1526    public override void Begin() 
    1627    { 
    1728        LuoKentta(); 
     29        AsetaOhjaimet(); 
     30        LisaaLaskurit(); 
    1831        AloitaPeli(); 
    19  
    20         Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    2132    } 
    2233 
     
    2839        pallo.Y = 0.0; 
    2940        pallo.Restitution = 1.0; 
     41        pallo.KineticFriction = 0.0; 
     42        pallo.MomentOfInertia = Double.PositiveInfinity; 
    3043        Add(pallo); 
     44        AddCollisionHandler(pallo, KasittelePallonTormays); 
    3145 
    32         PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
    33         maila.Shape = Shape.Rectangle; 
    34         maila.X = Level.Left + 20.0; 
    35         maila.Y = 0.0; 
    36         maila.Restitution = 1.0; 
    37         Add(maila); 
     46        maila1 = LuoMaila(Level.Left + 20.0, 0.0); 
     47        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
    3848 
    39         Level.CreateBorders(1.0, false); 
     49        vasenReuna = Level.CreateLeftBorder(); 
     50        vasenReuna.Restitution = 1.0; 
     51        vasenReuna.KineticFriction = 0.0; 
     52        vasenReuna.IsVisible = false; 
     53 
     54        oikeaReuna = Level.CreateRightBorder(); 
     55        oikeaReuna.Restitution = 1.0; 
     56        oikeaReuna.KineticFriction = 0.0; 
     57        oikeaReuna.IsVisible = false; 
     58 
     59        PhysicsObject ylaReuna = Level.CreateTopBorder(); 
     60        ylaReuna.Restitution = 1.0; 
     61        ylaReuna.KineticFriction = 0.0; 
     62        ylaReuna.IsVisible = false; 
     63 
     64        PhysicsObject alaReuna = Level.CreateBottomBorder(); 
     65        alaReuna.Restitution = 1.0; 
     66        alaReuna.IsVisible = false; 
     67        alaReuna.KineticFriction = 0.0; 
     68 
    4069        Level.BackgroundColor = Color.Black; 
    4170 
    4271        Camera.ZoomToLevel(); 
     72    } 
     73 
     74    PhysicsObject LuoMaila(double x, double y) 
     75    { 
     76        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
     77        maila.Shape = Shape.Rectangle; 
     78        maila.X = x; 
     79        maila.Y = y; 
     80        maila.Restitution = 1.0; 
     81        maila.KineticFriction = 0.0; 
     82        Add(maila); 
     83        return maila; 
     84    } 
     85 
     86    void LisaaLaskurit() 
     87    { 
     88        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 
     89        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 
     90    } 
     91 
     92    IntMeter LuoPisteLaskuri(double x, double y) 
     93    { 
     94        IntMeter laskuri = new IntMeter(0); 
     95        laskuri.MaxValue = 10; 
     96 
     97        Label naytto = new Label(); 
     98        naytto.BindTo(laskuri); 
     99        naytto.X = x; 
     100        naytto.Y = y; 
     101        naytto.TextColor = Color.White; 
     102        naytto.BorderColor = Level.BackgroundColor; 
     103        naytto.Color = Level.BackgroundColor; 
     104        Add(naytto); 
     105 
     106        return laskuri; 
     107    } 
     108 
     109    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 
     110    { 
     111        if (kohde == oikeaReuna) 
     112        { 
     113            pelaajan1Pisteet.Value += 1; 
     114        } 
     115        else if (kohde == vasenReuna) 
     116        { 
     117            pelaajan2Pisteet.Value += 1; 
     118        } 
    43119    } 
    44120 
     
    48124        pallo.Hit(impulssi); 
    49125    } 
     126 
     127    void AsetaOhjaimet() 
     128    { 
     129        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 
     130        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     131        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 
     132        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     133 
     134        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 
     135        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     136        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 
     137        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     138 
     139        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 
     140        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     141 
     142        ControllerOne.Listen(Button.DPadUp, ButtonState.Down, AsetaNopeus, "Liikuta mailaa ylös", maila1, nopeusYlos); 
     143        ControllerOne.Listen(Button.DPadUp, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     144        ControllerOne.Listen(Button.DPadDown, ButtonState.Down, AsetaNopeus, "Liikuta mailaa alas", maila1, nopeusAlas); 
     145        ControllerOne.Listen(Button.DPadDown, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     146 
     147        ControllerTwo.Listen(Button.DPadUp, ButtonState.Down, AsetaNopeus, "Liikuta mailaa ylös", maila2, nopeusYlos); 
     148        ControllerTwo.Listen(Button.DPadUp, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     149        ControllerTwo.Listen(Button.DPadDown, ButtonState.Down, AsetaNopeus, "Liikuta mailaa alas", maila2, nopeusAlas); 
     150        ControllerTwo.Listen(Button.DPadDown, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     151 
     152        ControllerOne.Listen(Button.Back, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     153        ControllerTwo.Listen(Button.Back, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     154    } 
     155 
     156    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
     157    { 
     158        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     159        { 
     160            maila.Velocity = Vector.Zero; 
     161            return; 
     162        } 
     163        if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 
     164        { 
     165            maila.Velocity = Vector.Zero; 
     166            return; 
     167        } 
     168 
     169        maila.Velocity = nopeus; 
     170    } 
    50171} 
Note: See TracChangeset for help on using the changeset viewer.