Changeset 9051


Ignore:
Timestamp:
2017-07-24 13:43:51 (6 years ago)
Author:
npo17_64
Message:

Pongi valmis

Location:
2017/30/HugoE/Pong/Pong/Pong
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • 2017/30/HugoE/Pong/Pong/Pong/Pong.cs

    r9042 r9051  
    99public class Pong : PhysicsGame 
    1010{ 
    11     IPhysicsObject pallo; 
     11    PhysicsObject pallo; 
     12    PhysicsObject maila1; 
     13    PhysicsObject maila2; 
     14    PhysicsObject vasenReuna; 
     15    PhysicsObject oikeaReuna; 
     16 
     17    IntMeter pelaajan1Pisteet; 
     18    IntMeter pelaajan2Pisteet; 
     19 
     20    Vector nopeusYlos = new Vector(0, 200); 
     21    Vector nopeusAlas = new Vector(0, -200); 
     22 
    1223    public override void Begin() 
    1324    { 
     
    1627        AsetaOhjaimet(); 
    1728        AloitaPeli(); 
    18          
    19          
     29        LisaaLaskurit(); 
     30 
     31 
     32 
    2033    } 
    21     
    22      
     34 
     35 
    2336 
    2437    void LuoKentta() 
     
    3043        Vector impulssi = new Vector(500.0, 0.0); 
    3144 
    32         PhysicsObject maila1; 
    33         PhysicsObject maila2; 
    3445 
    35         Vector nopeusylos = new Vector(0, 200); 
    36         Vector nopeusalas = new Vector(0, -200); 
    3746 
    3847        maila1 = LuoMaila(Level.Left + 20.0, 0.0); 
     
    4352        pallo.Hit(impulssi); 
    4453        pallo.Shape = Shape.Circle; 
    45         Level.CreateBorders(1.0, false); 
     54 
     55        AddCollisionHandler(pallo, KasittelePallonTormays); 
     56 
     57        vasenReuna = Level.CreateLeftBorder(); 
     58        vasenReuna.Restitution = 1.0; 
     59        vasenReuna.IsVisible = false; 
     60 
     61        oikeaReuna = Level.CreateRightBorder(); 
     62        oikeaReuna.Restitution = 1.0; 
     63        oikeaReuna.IsVisible = false; 
     64 
     65        PhysicsObject alaReuna = Level.CreateBottomBorder(); 
     66        alaReuna.Restitution = 1.0; 
     67        alaReuna.IsVisible = false; 
     68 
     69        PhysicsObject ylaReuna = Level.CreateTopBorder(); 
     70        ylaReuna.Restitution = 1.0; 
     71        ylaReuna.IsVisible = false; 
     72 
     73 
    4674        Level.Background.Color = Color.Black; 
    4775        Camera.ZoomToLevel(); 
     
    6290    PhysicsObject LuoMaila(double x, double y) 
    6391    { 
    64         IPhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
     92        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
    6593        maila.Shape = Shape.Rectangle; 
    6694        maila.X = x; 
     
    75103    { 
    76104        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    77         Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 
    78         Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
    79          
     105        Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 
     106        Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     107        Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 
     108        Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     109 
     110        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 
     111        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     112        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 
     113        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     114 
     115        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 
    80116 
    81117 
    82118    } 
    83     void AsetaNopeus(IPhysicsObject maila, Vector nopeus) 
     119    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
    84120    { 
     121        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     122        { 
     123            maila.Velocity = Vector.Zero; 
     124            return; 
     125        } 
     126        if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 
     127        { 
     128            maila.Velocity = Vector.Zero; 
     129            return; 
     130        } 
    85131        maila.Velocity = nopeus; 
    86132    } 
     133 
     134    void LisaaLaskurit() 
     135    { 
     136        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 
     137        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 
     138 
     139 
     140    } 
     141    IntMeter LuoPisteLaskuri(double x, double y) 
     142    { 
     143        IntMeter laskuri = new IntMeter(0); 
     144        laskuri.MaxValue = 10; 
     145        Label naytto = new Label(); 
     146        naytto.BindTo(laskuri); 
     147        naytto.X = x; 
     148        naytto.Y = y; 
     149        naytto.TextColor = Color.White; 
     150        naytto.BorderColor = Level.Background.Color; 
     151        naytto.Color = Level.Background.Color; 
     152        Add(naytto); 
     153        return laskuri; 
     154    } 
     155    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 
     156    { 
     157        if (kohde == oikeaReuna) 
     158        { 
     159            pelaajan1Pisteet.Value += 1; 
     160        } 
     161        else if (kohde == vasenReuna) 
     162        { 
     163            pelaajan2Pisteet.Value += 1; 
     164        } 
     165    } 
     166    const double PALLON_MIN_NOPEUS = 500; 
     167 
     168    protected override void Update(Time time) 
     169    { 
     170        if (pallo != null && Math.Abs(pallo.Velocity.X) < PALLON_MIN_NOPEUS) 
     171        { 
     172            pallo.Velocity = new Vector(pallo.Velocity.X * 1.1, pallo.Velocity.Y); 
     173        } 
     174        base.Update(time); 
     175    } 
    87176} 
     177     
     178     
     179 
    88180 
    89181     
Note: See TracChangeset for help on using the changeset viewer.