Changeset 8157 for 2016/30/SeveriU


Ignore:
Timestamp:
2016-07-27 14:04:02 (7 years ago)
Author:
seeijuuo
Message:
 
Location:
2016/30/SeveriU
Files:
15 added
6 edited

Legend:

Unmodified
Added
Removed
  • 2016/30/SeveriU/FysiikkaPeli1/FysiikkaPeli1/FysiikkaPeli1/FysiikkaPeli1.cs

    r8092 r8157  
    99public class FysiikkaPeli1 : PhysicsGame 
    1010{ 
     11    Vector nopeusYlos = new Vector(0, 200); 
     12    Vector nopeusAlas = new Vector(0, -200); 
     13 
    1114    PhysicsObject pallo; 
     15 
     16    PhysicsObject maila1; 
     17    PhysicsObject maila2; 
     18 
     19PhysicsObject vasenReuna; 
     20    PhysicsObject oikeaReuna; 
     21 
     22IntMeter pelaajan1Pisteet; 
     23    IntMeter pelaajan2Pisteet; 
    1224    public override void Begin() 
    1325    { 
     
    1527        LuoKentta(); 
    1628        AloitaPeli(); 
     29        AsetaOhjaimet(); 
     30        LisaaLaskurit(); 
    1731 
    1832        Vector impulssi = new Vector(500.0, 0.0); 
     
    2337    void LuoKentta() 
    2438    { 
    25 pallo = new PhysicsObject(40.0, 40.0); 
     39        pallo = new PhysicsObject(40.0, 40.0); 
    2640        pallo.Shape = Shape.Circle; 
    2741        pallo.X = -200.0; 
     
    3044        Add(pallo); 
    3145 
    32         LuoMaila(Level.Left + 20.0, 0.0); 
    33         LuoMaila(Level.Right - 20.0, 0.0); 
    34  
    35         Level.CreateBorders(1.0, false); 
     46        maila1 = LuoMaila(Level.Left + 20.0, 0.0); 
     47        maila2 = LuoMaila(Level.Right - 20.0, 0.0); 
     48        vasenReuna = Level.CreateLeftBorder(); 
     49        vasenReuna.Restitution = 1.0; 
     50        vasenReuna.IsVisible = false; 
    3651        Level.Background.Color = Color.Pink; 
    3752 
    3853        Camera.ZoomToLevel(); 
    39        
     54 
     55        AddCollisionHandler(pallo, Kasittelepallontormays); 
     56        PhysicsObject AlaReuna = Level.CreateBottomBorder(); 
     57        PhysicsObject Yläreuna = Level.CreateTopBorder(); 
     58        oikeaReuna = Level.CreateRightBorder(); 
     59        oikeaReuna.IsVisible = false; 
    4060    } 
    4161    void AloitaPeli() 
     
    4464        pallo.Hit(impulssi); 
    4565    } 
    46     void LuoMaila(double x, double y) 
     66 PhysicsObject LuoMaila(double x, double y) 
    4767    { 
    4868        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 
     
    5272        maila.Restitution = 1.0; 
    5373        Add(maila); 
     74        return maila; 
    5475    } 
    5576    void AsetaOhjaimet() 
    5677    { 
    57         Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     78Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 
     79Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 
     80Keyboard.Listen(Key.S, ButtonState.Down,     AsetaNopeus,     "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 
     81Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus,     null,                             maila1, Vector.Zero); 
     82 
     83Keyboard.Listen(Key.Up,     ButtonState.Down,     AsetaNopeus,     "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 
     84Keyboard.Listen(Key.Up,     ButtonState.Released, AsetaNopeus,     null,                             maila2, Vector.Zero); 
     85Keyboard.Listen(Key.Down,   ButtonState.Down,     AsetaNopeus,     "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 
     86 Keyboard.Listen(Key.Down,   ButtonState.Released, AsetaNopeus,     null,                             maila2, Vector.Zero); 
     87 
     88Keyboard.Listen(Key.F1,     ButtonState.Pressed,  ShowControlHelp, "Näytä ohjeet"); 
     89Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 
     90    } 
     91void AsetaNopeus(PhysicsObject maila, Vector nopeus) 
     92    { 
     93 
     94         if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 
     95        { 
     96        maila.Velocity = Vector.Zero; 
     97            return; 
     98        } 
     99 
     100         if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 
     101        { 
     102        maila.Velocity = Vector.Zero; 
     103             return; 
     104        } 
     105            
     106        maila.Velocity = nopeus; 
     107    } 
     108    void LisaaLaskurit() 
     109    { 
     110    pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 
     111    pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 
     112    } 
     113IntMeter LuoPisteLaskuri(double x, double y) 
     114    { 
     115IntMeter laskuri = new IntMeter(0); 
     116    laskuri.MaxValue = 10; 
     117Label naytto = new Label(); 
     118    naytto.BindTo(laskuri); 
     119    naytto.X = x; 
     120    naytto.Y = y; 
     121    naytto.TextColor = Color.White; 
     122    naytto.BorderColor = Level.Background.Color; 
     123    naytto.Color = Level.Background.Color; 
     124    Add(naytto); 
     125 
     126        return laskuri; 
     127 
     128    } 
     129void Kasittelepallontormays(PhysicsObject pallo, PhysicsObject kohde) 
     130    { 
     131if (kohde == oikeaReuna) 
     132    { 
     133        pelaajan1Pisteet.Value += 1; 
     134    } 
     135    else if (kohde == vasenReuna) 
     136    { 
     137        pelaajan2Pisteet.Value += 1; 
     138    } 
    58139    } 
    59140} 
Note: See TracChangeset for help on using the changeset viewer.