Changeset 1986 for 2011


Ignore:
Timestamp:
2011-06-14 14:37:33 (12 years ago)
Author:
hesasnel
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 2011/24/HenrikS/Hong/FysiikkaPeli1/FysiikkaPeli1/Peli.cs

    r1965 r1986  
    99public class Peli : PhysicsGame 
    1010{ 
     11    Vector nopeusYlos = new Vector(0, 700); 
     12    Vector nopeusAlas = new Vector(0, -700); 
    1113    PhysicsObject pallo; 
     14    PhysicsObject maila; 
     15    PhysicsObject maila2; 
     16    PhysicsObject vasenReuna; 
     17        PhysicsObject oikeaReuna; 
     18    PhysicsObject ylaReuna; 
     19    PhysicsObject alaReuna; 
     20    IntMeter pelaajan1pisteet; 
     21    IntMeter pelaajan2pisteet; 
    1222    public override void Begin() 
    1323    { 
    1424        LuoKentta(); 
    1525        AloitaPeli(); 
    16         
     26        LisaaLaskurit(); 
    1727        AsetaOhjaimet(); 
    1828        
     
    2535        pallo.Color = Color.Turquoise; 
    2636        pallo.Restitution = 1.0; 
     37          pallo.CanRotate = true; 
     38          pallo.KineticFriction = 0; 
    2739        Add(pallo); 
    28         PhysicsObject maila = PhysicsObject.CreateStaticObject  (20, 120); 
    29         maila.Shape = Shape.Rectangle; 
    30         maila.Color = Color.GreenYellow; 
    31         maila.X = 400; 
    32         maila.Y = 0; 
    33         maila.Restitution = 1; 
    34         Add(maila); 
    35         PhysicsObject maila2 = PhysicsObject.CreateStaticObject (20, 120); 
    36         maila2.Shape = Shape.Rectangle; 
    37         maila2.Color = Color.OrangeRed; 
    38         maila2.X = -400; 
    39         maila2.Y = 0; 
    40         maila2.Restitution = 1; 
    41         Add(maila2); 
    42           Level.CreateBorders(1, true); 
     40        maila = LuoMaila(400, Color.YellowGreen); 
     41           maila2 = LuoMaila (-400, Color.OrangeRed); 
     42       
     43           vasenReuna = Level.CreateLeftBorder(); 
     44          vasenReuna.Restitution = 1; 
     45          vasenReuna.IsVisible = false; 
     46           oikeaReuna = Level.CreateRightBorder(); 
     47          oikeaReuna.Restitution = 1; 
     48              oikeaReuna.IsVisible = false; 
     49           alaReuna = Level.CreateBottomBorder(); 
     50          alaReuna.Restitution = 1; 
     51           ylaReuna = Level.CreateTopBorder(); 
     52          ylaReuna.Restitution = 1; 
    4353        Level.BackgroundColor = Color.DarkGreen; 
    4454        Camera.ZoomToLevel(); 
     55        AddCollisionHandler(pallo, KasittelePallonTormays); 
    4556      } 
    4657      void AloitaPeli() 
     
    5162      void AsetaOhjaimet() 
    5263      { 
    53            Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); 
    54           Keyboard.Listen(Key.Up, ButtonState.Pressed, LiikutaMailaaYlos, "Pelaaja1: Liikuta mailaa ylös"); 
    55           Keyboard.Listen(Key.Up, ButtonState.Released, Pysautamaila, null); 
    56           Keyboard.Listen(Key.Down, ButtonState.Pressed, LiikutaMailaaAlas, "Pelaaja1: Liikuta mailaa alas"); 
    57         } 
    58          
     64          Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); 
     65          Keyboard.Listen(Key.Up, ButtonState.Down, Asetanopeus, "Pelaaja1: Liikuta mailaa ylos", maila, nopeusYlos); 
     66          Keyboard.Listen(Key.Up, ButtonState.Released, Asetanopeus, null, maila, Vector.Zero); 
     67          Keyboard.Listen(Key.Down, ButtonState.Down, Asetanopeus, "Pelaaja1: Liikuta mailaa alas", maila, nopeusAlas); 
     68          Keyboard.Listen(Key.Down, ButtonState.Released, Asetanopeus, null, maila, Vector.Zero); 
     69          Keyboard.Listen(Key.A, ButtonState.Down, Asetanopeus, "Pelaaja2: Liikuta mailaa ylos", maila2, nopeusYlos); 
     70          Keyboard.Listen(Key.A, ButtonState.Released, Asetanopeus, null, maila2, Vector.Zero); 
     71          Keyboard.Listen(Key.Z, ButtonState.Down, Asetanopeus, "Pelaaja2: Liikuta mailaa alas", maila2, nopeusAlas); 
     72          Keyboard.Listen(Key.Z, ButtonState.Released, Asetanopeus, null, maila2, Vector.Zero); 
     73      } 
     74      PhysicsObject LuoMaila(double x, Color vari) 
     75{ 
     76    PhysicsObject maila = PhysicsObject.CreateStaticObject  (20, 120); 
     77 
     78        maila.Shape = Shape.Rectangle; 
     79        maila.Color = vari; 
     80        maila.X = x; 
     81        maila.Y = 0; 
     82        maila.Restitution = 1; 
     83        Add(maila); 
     84        return maila; 
    5985} 
     86      void Asetanopeus(PhysicsObject maila, Vector nopeus) 
     87      { 
     88          if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 
     89          { 
     90              maila.Velocity = Vector.Zero; 
     91              return; 
     92          } 
     93          if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     94          { 
     95              maila.Velocity = Vector.Zero; 
     96              return; 
     97          } 
     98          maila.Velocity = nopeus; 
     99      } 
     100      void LisaaLaskurit() 
     101      { 
     102      
     103          pelaajan1pisteet = LuoPisteLaskuri (-400,375); 
     104          pelaajan2pisteet = LuoPisteLaskuri(400, 375); 
     105      } 
     106      IntMeter LuoPisteLaskuri(double x, double y) 
     107      { 
     108          IntMeter Laskuri = new IntMeter(0); 
     109          Laskuri.MaxValue = 1000000; 
     110          Label naytto = new Label(); 
     111          naytto.BindTo(Laskuri); 
     112          naytto.X = x; 
     113              naytto.Y = y; 
     114              naytto.TextColor = Color.DarkTurquoise; 
     115              naytto.BorderColor = Color.SkyBlue; 
     116              naytto.Color = Color.DarkGreen; 
     117              Add(naytto); 
     118                  return Laskuri; 
     119      } 
     120      void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 
     121{ 
     122    if (kohde == vasenReuna) 
     123{ 
     124pelaajan2pisteet.Value += 1; 
     125} 
     126if (kohde == oikeaReuna) 
     127{ 
     128pelaajan1pisteet.Value +=1; 
     129} 
     130} 
     131} 
     132 
     133 
Note: See TracChangeset for help on using the changeset viewer.