Changeset 7285


Ignore:
Timestamp:
2016-06-14 13:45:49 (7 years ago)
Author:
tesatapa
Message:
 
Location:
2016/24/HenriH
Files:
3 added
6 edited

Legend:

Unmodified
Added
Removed
  • 2016/24/HenriH/FysiikkaPeli1/FysiikkaPeli1/FysiikkaPeli1/FysiikkaPeli1.cs

    r7269 r7285  
    1111    PhysicsObject pallo; 
    1212    const double PALLON_MIN_NOPEUS = 500; 
     13    PhysicsObject maila1; 
     14    PhysicsObject maila2; 
    1315 
     16    PhysicsObject vasenReuna; 
     17    PhysicsObject oikeaReuna; 
     18 
     19    IntMeter pelaajan1Pisteet; 
     20    IntMeter pelaajan2Pisteet; 
     21 
     22    Vector nopeusYlos = new Vector(0, 400); 
     23    Vector nopeusAlas = new Vector(0, -400); 
    1424    protected override void Update(Time time) 
    1525    { 
     
    2737        LuoKentta(); 
    2838        AsetaOhjaimet(); 
     39        LisaaLaskurit(); 
    2940        AloitaPeli(); 
    3041 
     
    3950        pallo.Y = 0.0; 
    4051 
     52        AddCollisionHandler(pallo, KasittelePallonTormays); 
     53 
    4154        maila1 = LuoMaila(Level.Left + 20.0, 0.0); 
    42        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
     55        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
    4356 
    4457        pallo.Restitution = 1.0; 
    45         Level.Background.Color = Color.DarkYellowGreen; 
     58        Level.Background.Color = Color.Yellow; 
    4659        Camera.ZoomToLevel(); 
    4760        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    48         
    49         Level.CreateBorders(1.0, false); 
     61 
     62        vasenReuna = Level.CreateLeftBorder(); 
     63        vasenReuna.Restitution = 1.0; 
     64        vasenReuna.IsVisible = false; 
     65 
     66        oikeaReuna = Level.CreateRightBorder(); 
     67        oikeaReuna.Restitution = 1.0; 
     68        oikeaReuna.IsVisible = false; 
     69 
     70        PhysicsObject alaReuna = Level.CreateBottomBorder(); 
     71        alaReuna.Restitution = 1.0; 
     72        alaReuna.IsVisible = false; 
     73 
     74        PhysicsObject yläReuna = Level.CreateTopBorder(); 
     75        yläReuna.Restitution = 1.0; 
     76        yläReuna.IsVisible = false; 
    5077    } 
    5178    void AloitaPeli() 
     
    5481        pallo.Hit(impulssi); 
    5582    } 
    56      
     83 
    5784    PhysicsObject LuoMaila(double x, double y) 
    5885    { 
    5986        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
    6087        maila.Shape = Shape.Rectangle; 
    61         maila.X =x; 
     88        maila.X = x; 
    6289        maila.Y = y; 
    6390        maila.Restitution = 1.0; 
     
    6794    void AsetaOhjaimet() 
    6895    { 
    69         
     96        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 
     97        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
    7098 
     99        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 
     100        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     101 
     102        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 
     103        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     104        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 
     105        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     106 
     107        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 
    71108        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    72         Keyboard.Listen(Key.A, ButtonState.Down, LiikutaMaila1Ylos, "Pelaaja 1: Liikuta mailaa ylös"); 
    73         Keyboard.Listen(Key.A, ButtonState.Released, PysaytaMaila1, null); 
     109 
     110 
    74111    } 
    75112    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
    76113    { 
     114 
     115        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     116        { 
     117            maila.Velocity = Vector.Zero; 
     118            return; 
     119        } 
     120        if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 
     121        { 
     122            maila.Velocity = Vector.Zero; 
     123            return; 
     124        } 
     125 
    77126        maila.Velocity = nopeus; 
     127 
     128 
     129 
     130        if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 
     131        { 
     132            maila.Velocity = Vector.Zero; 
     133            return; 
     134        } 
     135        maila.Velocity = nopeus; 
     136    } 
     137 
     138    void LisaaLaskurit() 
     139    { 
     140        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 
     141        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 
     142    } 
     143    IntMeter LuoPisteLaskuri(double x, double y) 
     144    { 
     145        IntMeter laskuri = new IntMeter(0); 
     146        laskuri.MaxValue = 10; 
     147 
     148        Label naytto = new Label(); 
     149        naytto.BindTo(laskuri); 
     150        naytto.X = x; 
     151        naytto.Y = y; 
     152        naytto.TextColor = Color.Black; 
     153        naytto.BorderColor = Level.Background.Color; 
     154        naytto.Color = Level.Background.Color; 
     155        Add(naytto); 
     156 
     157        return laskuri; 
     158    } 
     159    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 
     160    { 
     161        if (kohde == oikeaReuna) 
     162        { 
     163            pelaajan1Pisteet.Value += 1; 
     164        } 
     165        else if (kohde == vasenReuna) 
     166        { 
     167            pelaajan2Pisteet.Value += 1; 
     168        } 
    78169    } 
    79170} 
     
    82173 
    83174 
    84  
Note: See TracChangeset for help on using the changeset viewer.