Changeset 6521


Ignore:
Timestamp:
2015-06-29 14:48:17 (8 years ago)
Author:
misakana
Message:

Valmis

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 2015/27/TeemuH/Pong/Pong/Pong/Pong.cs

    r6489 r6521  
    99public class Pong : PhysicsGame 
    1010{ 
    11     Vector nopeysYlos = new Vector(0, 200); 
     11    Vector nopeusYlos = new Vector(0, 200); 
    1212    Vector nopeusAlas = new Vector(0, -200); 
     13 
     14    PhysicsObject pallo; 
    1315    PhysicsObject maila1; 
    1416    PhysicsObject maila2; 
    1517 
    16     PhysicsObject pallo; 
     18    PhysicsObject vasenReuna; 
     19    PhysicsObject oikeaReuna; 
     20 
     21    IntMeter pelaajan1Pisteet; 
     22    IntMeter pelaajan2Pisteet; 
    1723     
    1824    public override void Begin() 
     
    2026        LuoKentta(); 
    2127        AsetaOhjaimet(); 
    22         LuoKentta(); 
     28        LisaaLaskurit(); 
     29        AloitaPeli(); 
     30 
     31         
    2332 
    2433        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); 
     
    3140        Add(pallo); 
    3241        pallo.Shape = Shape.Circle; 
     42 
     43 
     44 
     45            ; 
    3346        pallo.X = -200.0; 
    3447        pallo.Y = 0.0; 
    3548        Vector impulssi = new Vector(500.0, 0.0); 
    3649        pallo.Hit(impulssi); 
    37         maila1 = LuoMaila(Level1.Left + 20.0, 0.0); 
     50        AddCollisionHandler(pallo, KasittelePallonTormays); 
     51 
     52        maila1 = LuoMaila(Level.Left + 20.0, 0.0); 
    3853        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
    3954 
    40          
    41         Level.CreateBorders(1.0, false); 
     55 
     56        vasenReuna = Level.CreateLeftBorder(); 
     57        vasenReuna.Restitution = 1.0; 
     58        vasenReuna.IsVisible = false; 
     59 
     60        oikeaReuna = Level.CreateRightBorder(); 
     61        oikeaReuna.Restitution = 1.0; 
     62        oikeaReuna.IsVisible = false; 
     63 
     64        PhysicsObject ylaReuna = Level.CreateTopBorder(); 
     65        ylaReuna.Restitution = 1.0; 
     66        ylaReuna.IsVisible = false; 
     67 
     68        PhysicsObject alaReuna = Level.CreateTopBorder(); 
     69        alaReuna.Restitution = 1.0; 
     70        alaReuna.IsVisible = false; 
     71 
     72 
     73 
    4274        pallo.Restitution = 1.0; 
    4375 
    44         LuoMaila(Level.Left + 20.0, 0.0); 
    45         LuoMaila(Level.Right - 20.0, 0.0); 
     76         
    4677 
    4778        Level.CreateBorders(1.0, false); 
    48         Level.Background.Color = Color.Black; 
     79        Level.Background.Color = Color.LightBlue; 
    4980 
    5081        Camera.ZoomToLevel(); 
     
    5990        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    6091    } 
    61      PhysicsObject LuoMaila(double x, double y) 
     92    PhysicsObject LuoMaila(double x, double y) 
    6293    { 
    6394        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
     
    6697        maila.Y = y; 
    6798        maila.Restitution = 1.0; 
     99        maila.Color = Color.Pink; 
     100         
    68101        Add(maila); 
    69102        return maila; 
     
    72105    { 
    73106 
    74     Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, ", maila1, nopeusYlos)"; 
    75     Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     107        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 
     108        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     109        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja1: Liikuta mailaa alas", maila1, nopeusAlas); 
     110        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null,  maila1, Vector.Zero); 
    76111 
    77     Keyborad.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     112        Keyboard.Listen(Key.Up, ButtonState.Down,     AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 
     113        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     114        Keyboard.Listen(Key.Down, ButtonState.Down,   AsetaNopeus,"Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 
     115        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 
     116 
     117        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 
     118        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     119     
     120         Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
    78121    } 
    79  void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
    80 { 
    81 maila.Velocity=nopeus; 
     122    void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
     123    { 
     124        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     125        { 
     126            maila.Velocity = Vector.Zero; 
     127            return; 
     128        } 
     129        if  ((nopeus.Y >0) && (maila.Top > Level.Top)) 
     130        { 
     131            maila.Velocity = Vector.Zero; 
     132            return; 
     133 
     134        } 
     135        maila.Velocity = nopeus; 
     136 
     137    } 
     138    void LiikutaMailaaYlos(PhysicsObject Maila) 
     139    { 
     140        Vector nopeus = new Vector(0, 200); 
     141        maila1.Velocity = nopeus; 
     142    } 
     143    void LiikutaMailaaAlas(PhysicsObject maila1) 
     144    { 
     145        Vector nopeus = new Vector(0, -200); 
     146        maila1.Velocity = nopeus; 
     147 
     148    } 
     149    void PysaytaMaila(PhysicsObject maila) 
     150    { 
     151        Vector nopeus = Vector.Zero; 
     152        maila.Velocity = nopeus; 
     153 
     154    } 
     155    void LisaaLaskurit() 
     156    { 
     157        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 
     158        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 
     159         
     160    } 
     161    IntMeter LuoPisteLaskuri(double x, double y) 
     162    { 
     163        IntMeter laskuri = new IntMeter(0); 
     164        laskuri.MaxValue = 10; 
     165 
     166        Label naytto = new Label(); 
     167        naytto.BindTo(laskuri); 
     168        naytto.X = x; 
     169        naytto.Y = y; 
     170        naytto.TextColor = Color.Brown; 
     171        naytto.BorderColor = Level.Background.Color; 
     172        naytto.Color = Level.Background.Color; 
     173        Add(naytto); 
     174 
     175        return laskuri; 
     176    } 
     177    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 
     178    { 
     179        if (kohde == oikeaReuna) 
     180        { 
     181            pelaajan1Pisteet.Value += 1; 
     182        } 
     183        else if (kohde == vasenReuna) 
     184        { 
     185            pelaajan2Pisteet.Value += 1; 
     186 
     187        } 
     188        pallo.Color = Color.Purple;      
     189    } 
    82190} 
    83 } 
Note: See TracChangeset for help on using the changeset viewer.