Changeset 8132 for 2016/30/AapoN/Pong


Ignore:
Timestamp:
2016-07-27 09:58:23 (7 years ago)
Author:
aajonaas
Message:

Peli on valmis

Location:
2016/30/AapoN/Pong
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • 2016/30/AapoN/Pong/Pong/Pong/Pong.cs

    r8094 r8132  
    99public class Pong : PhysicsGame 
    1010{ 
     11    Vector nopeusYlos = new Vector(0, 200); 
     12    Vector nopeusAlas = new Vector(0, -200); 
     13    IntMeter Pelaajan1pisteet; 
     14    IntMeter Pelaajan2pisteet; 
     15 
    1116    PhysicsObject pallo; 
     17    PhysicsObject maila1; 
     18    PhysicsObject maila2; 
     19 
     20    PhysicsObject vasenreuna; 
     21    PhysicsObject oikeareuna; 
    1222    public override void Begin() 
    1323    { 
    1424        luokentta(); 
     25        AsetaOhjaimet(); 
    1526        AloitaPeli(); 
    16  
    17  
    18  
    19  
    20         Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     27        lisaalaskurit(); 
    2128    } 
    2229    void luokentta() 
     
    2936        pallo.Restitution = 1.0; 
    3037        Add(pallo); 
     38        AddCollisionHandler(pallo, KasittelePallonTormays); 
    3139 
    32         LuoMaila(Level.Left + 20.0, 0.0); 
    33         LuoMaila(Level.Right - 20.0, 0.0); 
     40        maila1 = LuoMaila(Level.Left + 20.0, 0.0); 
     41        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
    3442 
    35         Level.CreateBorders(1.0, false); 
    36         Level.Background.Color = Color.Turquoise; 
     43        vasenreuna = Level.CreateLeftBorder(); 
     44        vasenreuna.Restitution = 1.0; 
     45        oikeareuna = Level.CreateRightBorder(); 
     46        oikeareuna.Restitution = 1.0; 
     47        PhysicsObject alareuna = Level.CreateBottomBorder(); 
     48        alareuna.Restitution = 1.0; 
     49        PhysicsObject yläreuna = Level.CreateTopBorder(); 
     50        yläreuna.Restitution = 1.0; 
     51        Level.Background.Color = Color.Black; 
    3752 
    3853        Camera.ZoomToLevel(); 
     
    4661        pallo.Hit(impulssi); 
    4762    } 
    48     void LuoMaila(double x, double y) 
     63    PhysicsObject LuoMaila(double x, double y) 
    4964    { 
    5065        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
     
    5469        maila.Restitution = 1.0; 
    5570        Add(maila); 
     71        return maila; 
     72    } 
     73    void AsetaOhjaimet() 
     74    { 
     75        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: liikuttaa mailaa ylos", maila1, nopeusYlos); 
     76        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     77        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: liikuttaa mailaa alas", maila1, nopeusAlas); 
     78        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
    5679 
     80        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: liikuttaa mailaa ylos", maila2, nopeusYlos); 
     81        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     82        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: liikuttaa mailaa alas", maila2, nopeusAlas); 
     83        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     84 
     85        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     86    } 
     87 
     88    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
     89    { 
     90        if ((nopeus.Y < 0 && (maila.Bottom < Level.Bottom))) 
    5791        { 
    58       
     92            maila.Velocity = Vector.Zero; 
     93            return; 
     94        } 
     95 
     96        if ((nopeus.Y > 0 && (maila.Top > Level.Top))) 
     97        { 
     98            maila.Velocity = Vector.Zero; 
     99            return; 
     100        } 
     101        maila.Velocity = nopeus; 
     102    } 
     103    void lisaalaskurit() 
     104    { 
     105        Pelaajan1pisteet = luoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 
     106        Pelaajan2pisteet = luoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 
     107 
     108 
     109    } 
     110    IntMeter luoPisteLaskuri(double x, double y) 
     111    { 
     112        IntMeter laskuri = new IntMeter(0); 
     113 
     114        laskuri.MaxValue = 10; 
     115        Label naytto = new Label(); 
     116        naytto.BindTo(laskuri); 
     117        naytto.X = x; 
     118        naytto.Y = y; 
     119        naytto.TextColor = Color.White; 
     120        naytto.BorderColor = Level.Background.Color; 
     121        naytto.Color = Level.Background.Color; 
     122        Add(naytto); 
     123        return laskuri; 
     124    } 
     125    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 
     126    { 
     127        if (kohde == oikeareuna) 
     128        { 
     129            Pelaajan1pisteet.Value += 1; 
     130 
     131 
     132        } 
     133        else if (kohde == vasenreuna) 
     134        { 
     135            Pelaajan2pisteet.Value += 1; 
    59136        } 
    60137    } 
    61138} 
     139 
     140 
     141 
     142 
     143 
     144 
     145 
     146  
     147     
    62148 
    63149 
     
    74160 
    75161 
     162 
Note: See TracChangeset for help on using the changeset viewer.