Changeset 7462 for 2016/25


Ignore:
Timestamp:
2016-06-19 15:46:16 (7 years ago)
Author:
koannak
Message:

peli valmis

Location:
2016/25/AaroV/Pong!
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • 2016/25/AaroV/Pong!/Pong!/Pong_/Pong_.cs

    r7457 r7462  
    99public class Pong_ : PhysicsGame 
    1010{ 
     11    PhysicsObject vasenReuna; 
     12    PhysicsObject oikeaReuna; 
     13 
     14    Vector nopeusYlos = new Vector(0, 700); 
     15    Vector nopeusAlas = new Vector(0, -700); 
    1116    PhysicsObject pallo; 
     17    PhysicsObject maila1; 
     18    PhysicsObject maila2; 
     19    IntMeter pelaajan1Pisteet; 
     20    IntMeter pelaajan2Pisteet; 
     21 
    1222    public override void Begin() 
    1323    { 
    1424        LuoKentta(); 
    1525        AloitaPeli(); 
     26        AsetaOhjaimet(); 
     27        LisaaLaskurit(); 
    1628 
    17         PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); 
    1829        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    1930    } 
     
    2435        pallo.Shape = Shape.Circle; 
    2536        pallo.Color = Color.Emerald; 
     37 
     38        maila1 = LuoMaila(Level.Left + 20.0, 0.0); 
     39        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
     40        AddCollisionHandler(pallo, KasittelePallonTormays); 
     41        vasenReuna = Level.CreateLeftBorder(); 
     42        vasenReuna.Restitution = 1.0; 
     43        vasenReuna.IsVisible = false; 
    2644        Add(pallo); 
    2745        pallo.X = 0.0; 
    2846        pallo.Y = 0.0; 
    29  
    30         Level.CreateBorders(1.0, false); 
     47        PhysicsObject alaReuna = Level.CreateBottomBorder(); 
     48        vasenReuna.Restitution = 1.0; 
     49        vasenReuna.IsVisible = false; 
     50        oikeaReuna = Level.CreateRightBorder(); 
     51        vasenReuna.Restitution = 1.0; 
     52        vasenReuna.IsVisible = false; 
    3153        pallo.Restitution = 1.0; 
    3254        Camera.ZoomToLevel(); 
     55        PhysicsObject yläReuna = Level.CreateTopBorder(); 
     56        vasenReuna.Restitution = 1.0; 
     57        vasenReuna.IsVisible = false; 
     58        Level.Background.CreateGradient(Color.Red, Color.Turquoise); 
     59    } 
     60 
     61    void AloitaPeli() 
     62    { 
     63        Vector impulssi = new Vector(800.0, 0.0); 
     64        pallo.Hit(impulssi); 
     65    } 
     66    protected override void Update(Time time) 
     67    { 
     68        if (pallo != null) 
     69        { 
     70            if (pallo.Velocity.Magnitude < PALLON_MIN_NOPEUS) 
     71            { 
     72                pallo.Velocity = new Vector(pallo.Velocity.X, pallo.Velocity.Y) * 1.1; 
     73            } 
     74            if (pallo.Velocity.Magnitude > PALLON_MAX_NOPEUS) 
     75            { 
     76                pallo.Velocity = new Vector(pallo.Velocity.X, pallo.Velocity.Y) / 1.1; 
     77            } 
     78 
     79        } 
     80        base.Update(time); 
     81 
     82    } 
     83    const double PALLON_MAX_NOPEUS = 800; 
     84    const double PALLON_MIN_NOPEUS = 800; 
     85 
     86    PhysicsObject LuoMaila(double x, double y) 
     87    { 
    3388        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
    3489        maila.Shape = Shape.Rectangle; 
    35         maila.X = Level.Left + 20.0; 
     90        maila.X = x; 
     91 
    3692        maila.Y = 0.0; 
    3793        maila.Restitution = 1.0; 
    38         Add(maila); 
    39         Level.Background.CreateGradient(Color.Red, Color.Turquoise); 
     94        Add(maila); return maila; 
     95    } 
     96 
     97    void AsetaOhjaimet() 
     98    { 
     99        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 
     100        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     101 
     102 
     103        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 
     104        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     105 
     106 
     107        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 
     108        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     109        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 
     110        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     111 
     112 
     113        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 
     114 
     115 
     116        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     117        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     118    } 
     119 
     120    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
     121    { 
     122        if ((nopeus.Y >= 0) && (maila.Top >= Level.Top)) 
     123        { 
     124            maila.Velocity = Vector.Zero; 
     125            return; 
     126        } 
     127        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     128        { 
     129            maila.Velocity = Vector.Zero; 
     130            return; 
     131        } 
     132        maila.Velocity = nopeus; 
     133 
     134 
     135 
     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 
     144 
     145    IntMeter LuoPisteLaskuri(double x, double y) 
     146    { 
     147        IntMeter laskuri = new IntMeter(0); 
     148        laskuri.MaxValue = 10; 
     149        Label naytto = new Label(); 
     150        naytto.BindTo(laskuri); 
     151        naytto.X = x; 
     152        naytto.Y = y; 
     153        naytto.TextColor = Color.White; 
     154        naytto.BorderColor = Level.Background.Color; 
     155        naytto.Color = Level.Background.Color; 
     156        Add(naytto); 
     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        } 
    40169    } 
    41170} 
    42 void AloitaPeli() 
    43 { 
    44     Vector impulssi = new Vector(900.0, 0.0); 
    45     .Hit(impulssi); 
    46 } 
    47  
Note: See TracChangeset for help on using the changeset viewer.