Changeset 7461 for 2016/25


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

Pong

Location:
2016/25/ToukoP/Pong
Files:
6 edited

Legend:

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

    r7454 r7461  
    99public class Pong : PhysicsGame 
    1010{ 
     11    Vector nopeusYlos = new Vector(0, 200); 
     12    Vector nopeusAlas = new Vector(0, -200); 
     13    PhysicsObject pallo; 
     14 
     15    PhysicsObject maila1; 
     16    PhysicsObject maila2; 
     17    PhysicsObject vasenReuna; 
     18    PhysicsObject oikeaReuna; 
     19    IntMeter pelaajan1pisteet; 
     20    IntMeter pelaajan2pisteet; 
    1121    public override void Begin() 
    1222    { 
    13         // TODO: Kirjoita ohjelmakoodisi tähän 
    14         PhysicsObject 
    15            pallo = new PhysicsObject(50, 50); 
     23 
     24        LuoKentta(); 
     25        Asetaohjaimet(); 
     26        AloitaPeli(); 
     27        LisaaLaskurit(); 
     28        IsFullScreen = true; 
     29        Vector impulssi = new Vector(500.0, 0.0); 
     30        pallo.Hit(impulssi); 
     31        Level.CreateBorders(1.0, false); 
     32         
     33        Camera.ZoomToLevel(); 
     34        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); 
     35 
     36    } 
     37    void LuoKentta() 
     38    { 
     39        pallo = new PhysicsObject(50, 50); 
     40        AddCollisionHandler(pallo, KasittelePallonTormays); 
     41 
    1642        Add(pallo); 
    1743        pallo.Shape = Shape.Circle; 
    1844        pallo.Color = Color.Gold; 
     45        pallo.Restitution = 1.0; 
     46        maila1 = Luomaila(Level.Left + 20.0, 0.0); 
     47        maila2 = Luomaila(Level.Right - 20.0, 0.0); 
    1948        Level.Background.CreateGradient(Color.Black, Color.Blue); 
    2049        pallo.X = -200.0; 
    2150        pallo.Y = 0.0; 
     51 
     52 
     53        vasenReuna = Level.CreateLeftBorder(); 
     54        vasenReuna.Restitution = 1.0; 
     55        vasenReuna.IsVisible = false; 
     56 
     57 
     58        oikeaReuna = Level.CreateRightBorder(); 
     59        oikeaReuna.Restitution = 1.0; 
     60        oikeaReuna.IsVisible = false; 
     61 
     62 
     63        PhysicsObject alaReuna = Level.CreateBottomBorder(); 
     64        alaReuna.Restitution = 1.0; 
     65        alaReuna.IsVisible = false; 
     66 
     67        PhysicsObject yläReuna = Level.CreateTopBorder(); 
     68        yläReuna.Restitution = 1.0; 
     69        yläReuna.IsVisible = false; 
     70 
     71    } 
     72    void AloitaPeli() 
     73    { 
     74 
     75 
     76 
    2277        Vector impulssi = new Vector(500.0, 0.0); 
    2378        pallo.Hit(impulssi); 
    24            PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); 
     79    } 
     80    PhysicsObject Luomaila(double x, double y) 
     81    { 
     82        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
     83        maila.Shape = Shape.Rectangle; 
     84        maila.X = Level.Left + 20.0; 
     85        maila.Y = 0.0; 
     86        maila.X = x; 
     87        maila.Y = y; 
     88        maila.Restitution = 1.0; 
     89        Add(maila); 
     90        return maila; 
     91    } 
     92 
     93 
     94    void Asetaohjaimet() 
     95    { 
     96        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa Ylos", maila1, nopeusYlos); 
     97        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
    2598        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     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        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     102        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 
     103 
     104        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     105        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 
     106        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     107        Keyboard.Listen(Key.H, ButtonState.Pressed, AloitaPeli, null); 
     108    } 
     109 
     110    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
     111    { 
     112        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     113        { 
     114            maila.Velocity = Vector.Zero; 
     115            return; 
     116        } 
     117        if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 
     118        { 
     119            maila.Velocity = Vector.Zero; 
     120            return; 
     121        } 
     122        maila.Velocity = nopeus; 
     123    } 
     124 
     125    void LisaaLaskurit() 
     126    { 
     127        pelaajan1pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 
     128        pelaajan2pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 
     129    } 
     130    IntMeter LuoPisteLaskuri(double x, double y) 
     131    { 
     132        IntMeter laskuri = new IntMeter(0); 
     133        laskuri.MaxValue = 10; 
     134        Label naytto = new Label(); 
     135        naytto.BindTo(laskuri); 
     136        naytto.X = x; 
     137        naytto.Y = y; 
     138        naytto.TextColor = Color.White; 
     139        naytto.BorderColor = Level.Background.Color; 
     140        naytto.Color = Level.Background.Color; 
     141        Add(naytto); 
     142        return laskuri; 
     143    } 
     144 
     145 
     146    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 
     147    { 
     148        if (kohde == oikeaReuna) 
     149        { 
     150            pelaajan1pisteet.Value += 1; 
     151        } 
     152        else if (kohde == vasenReuna) 
     153        { 
     154            pelaajan2pisteet.Value += 1; 
     155        } 
    26156    } 
    27157} 
Note: See TracChangeset for help on using the changeset viewer.