Changeset 7463 for 2016/25


Ignore:
Timestamp:
2016-06-19 15:46:51 (7 years ago)
Author:
koannak
Message:
 
Location:
2016/25/JesseK
Files:
132 added
6 edited

Legend:

Unmodified
Added
Removed
  • 2016/25/JesseK/Pong/Pong/Pong/Pong.cs

    r7452 r7463  
    1010{ 
    1111    PhysicsObject ball; 
     12    Vector nopeusYlos = new Vector(0, 400); 
     13    Vector nopeusAlas = new Vector(0, -400); 
     14 
     15    PhysicsObject maila1; 
     16    PhysicsObject maila2; 
     17    PhysicsObject vasenReuna; 
     18    PhysicsObject oikeaReuna; 
     19    IntMeter pelaajan1Pisteet; 
     20    IntMeter pelaajan2Pisteet; 
     21 
     22 
     23 
    1224    public override void Begin() 
    1325    { 
    14         
    15            // TODO: Kirjoita ohjelmakoodisi tähän 
    16            LuoKentta(); 
    17         Vector impulssi = new Vector(500.0, 0.0); 
    18         ball.Hit(impulssi); 
     26         
     27            LuoKentta(); 
     28            AsetaOhjaimet(); 
     29            LisaaLaskurit(); 
     30            AloitaPeli(); 
     31         
     32         
    1933 
    20         Level.Background.CreateGradient(Color.Black,Color.Blue); 
     34        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     35 
     36        // TODO: Kirjoita ohjelmakoodisi tähän 
     37 
     38 
     39 
     40        Level.Background.CreateGradient(Color.Black, Color.Blue); 
    2141        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); 
    2242        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    2343        Camera.ZoomToLevel(); 
    24          
     44 
     45    } 
     46 
     47 
     48     
     49     
     50 
     51     
     52 
     53    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 
     54    {  
     55        if (kohde == oikeaReuna) 
     56        { 
     57            pelaajan1Pisteet.Value += 1; 
     58        } 
     59        else if (kohde == vasenReuna) 
     60        { 
     61            pelaajan2Pisteet.Value += 1; 
     62        } 
    2563    } 
    2664    void LuoKentta() 
     65     
    2766    { 
     67        
    2868        ball = new PhysicsObject(50, 50); 
    2969        ball.Shape = Shape.Circle; 
     70        AddCollisionHandler(ball, KasittelePallonTormays); 
    3071        ball.Color = Color.Black; 
    3172        Add(ball); 
     
    3374        ball.X = -200.0; 
    3475        ball.Restitution = 1.0; 
    35         Level.CreateBorders(1.0, false); 
     76         vasenReuna = Level.CreateLeftBorder(); 
     77 oikeaReuna = Level.CreateRightBorder(); 
     78        PhysicsObject ylaReuna = Level.CreateTopBorder(); 
     79        ylaReuna.Restitution = 1.0; 
     80        ylaReuna.IsVisible = false; 
     81        PhysicsObject alaReuna = Level.CreateBottomBorder(); 
     82        alaReuna.Restitution = 1.0; 
     83        alaReuna.IsVisible = false; 
     84        oikeaReuna.Restitution = 1.0; 
     85        oikeaReuna.IsVisible = false; 
     86        vasenReuna.Restitution = 1.0; 
     87        vasenReuna.IsVisible = false; 
    3688 
     89 
     90        maila1 = LuoMaila(Level.Left + 20.0, 0.0); 
     91        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
     92 
     93    } 
     94 
     95    void LisaaLaskurit() 
     96    { 
     97        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 
     98        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 
     99    }      
     100     
     101     
     102     
     103 
     104 
     105    IntMeter LuoPisteLaskuri(double x, double y) 
     106    { 
     107        IntMeter laskuri = new IntMeter(0); 
     108        laskuri.MaxValue = 10; 
     109        Label naytto = new Label(); 
     110        naytto.BindTo(laskuri); 
     111        naytto.X = x; 
     112        naytto.Y = y; 
     113        naytto.TextColor = Color.White; 
     114        naytto.BorderColor = Level.Background.Color; 
     115        naytto.Color = Level.Background.Color; 
     116        Add(naytto); 
     117        return laskuri; 
     118    } 
     119 
     120 
     121    PhysicsObject LuoMaila(double x, double y) 
     122    { 
    37123        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
    38124        maila.Shape = Shape.Rectangle; 
    39         maila.X = Level.Left + 20.0; 
    40         maila.Y = 0.0; 
     125        maila.X = x; 
     126        maila.Y = y; 
    41127        maila.Restitution = 1.0; 
    42128        Add(maila); 
    43          
    44          
     129        return maila; 
     130    } 
     131    void AloitaPeli() 
     132    { 
     133        Vector impulssi = new Vector(500.0, 0.0); 
     134        ball.Hit(impulssi); 
    45135 
    46          
     136 
    47137    } 
    48138 
    49     void AloitaPeli() 
     139 
     140    void AsetaOhjaimet() 
     141     
    50142    { 
     143        Keyboard.Listen(Key.F, ButtonState.Pressed,LyoPalloa, null); 
     144        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 
     145        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     146        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     147        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 
     148        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     149        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 
     150        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     151        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 
     152        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     153        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 
     154    } 
    51155 
     156    void LyoPalloa() 
     157    { 
     158        ball.Velocity = new Vector(500, 10); 
    52159    } 
     160    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
     161 
     162 
     163    { 
     164        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     165        { 
     166            maila.Velocity = Vector.Zero; 
     167            return; 
     168        } 
     169        if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 
     170        { 
     171            maila.Velocity = Vector.Zero; 
     172            return; 
     173        } 
     174 
     175        maila.Velocity = nopeus; 
     176    } 
     177 
    53178} 
     179 
     180    
     181     
    54182 
    55183 
    56184 
    57  
Note: See TracChangeset for help on using the changeset viewer.