Changeset 3437


Ignore:
Timestamp:
2012-07-02 14:37:14 (8 years ago)
Author:
dezhidki
Message:

Talletus.

Location:
2012/27/DenisZ/Pong/Pong/Pong
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • 2012/27/DenisZ/Pong/Pong/Pong/Pong.cs

    r3432 r3437  
    1010{ 
    1111    private PhysicsObject ball1; 
    12     private PhysicsObject player1, player2; 
     12    private Player player1, player2; 
    1313    private Vector moveUp = new Vector(0, 500); 
    1414    private Vector moveDown = new Vector(0, -500); 
     15    private PhysicsObject leftBorder, rightBorder, topBorder, bottomBorder; 
    1516 
    1617    private IntMeter meter1, meter2; 
     
    2223        Level.Width = Screen.Width + Screen.Width / 2; 
    2324        Level.Height = Screen.Height + Screen.Height / 2; 
    24         Level.CreateBorders(1.0, false); 
     25        createLevelBorders(); 
    2526        Camera.ZoomToLevel(); 
    2627 
     28        player1 = new Player(new Vector(Level.Left + 20, 0), 30, 150); 
     29        player2 = new Player(new Vector(Level.Right - 20, 0), 30, 150); 
     30 
    2731        setupBall(); 
    28         setupPlayers(); 
    2932        setupKeys(); 
    3033 
     
    3639    } 
    3740 
    38     private IntMeter createMeter(Vector pos ,int maxValue) 
     41    private void createLevelBorders() 
    3942    { 
    40         IntMeter meter = new IntMeter(0, 0 , maxValue); 
     43        leftBorder = Surface.CreateLeft(Level); 
     44        leftBorder.Restitution = 1.0; 
     45        leftBorder.KineticFriction = 0.0; 
     46        leftBorder.IsVisible = false; 
     47        Add(leftBorder); 
     48 
     49        rightBorder = Surface.CreateRight(Level); 
     50        rightBorder.Restitution = 1.0; 
     51        rightBorder.KineticFriction = 0.0; 
     52        rightBorder.IsVisible = false; 
     53        Add(rightBorder); 
     54 
     55        topBorder = Surface.CreateTop(Level); 
     56        topBorder.Restitution = 1.0; 
     57        //  topBorder.KineticFriction = 0.0; 
     58        topBorder.IsVisible = false; 
     59        Add(topBorder); 
     60 
     61        bottomBorder = Surface.CreateBottom(Level); 
     62        bottomBorder.Restitution = 1.0; 
     63        // bottomBorder.KineticFriction = 0.0; 
     64        bottomBorder.IsVisible = false; 
     65        Add(bottomBorder); 
     66    } 
     67 
     68    private IntMeter createMeter(Vector pos, int maxValue) 
     69    { 
     70        IntMeter meter = new IntMeter(0, 0, maxValue); 
    4171 
    4272        Label frame = new Label(); 
    4373        frame.BindTo(meter); 
    4474        frame.TextColor = Color.White; 
    45         frame.SizeMode = TextSizeMode.AutoSize; 
    46        // frame.TextSize = new Vector(10, 10); 
    47         frame.Color = Level.BackgroundColor; 
     75        // frame.TextSize = new Vector(10, 10); 
     76        frame.Color = Color.Transparent; 
    4877        frame.Position = pos; 
    4978        Add(frame); 
     
    5483    private void setupKeys() 
    5584    { 
    56         Keyboard.Listen(Key.W, ButtonState.Down, setVelocity, "Move Player1 Up", player1 , moveUp); 
    57         Keyboard.Listen(Key.S, ButtonState.Down, setVelocity, "Move Player1 Down", player1, moveDown); 
    58         Keyboard.Listen(Key.W, ButtonState.Released, setVelocity, null, player1, Vector.Zero); 
    59         Keyboard.Listen(Key.S, ButtonState.Released, setVelocity, null, player1, Vector.Zero); 
     85        Keyboard.Listen(Key.W, ButtonState.Down, player1.setVelocity, "Move Player1 Up", moveUp); 
     86        Keyboard.Listen(Key.S, ButtonState.Down, player1.setVelocity, "Move Player1 Down", moveDown); 
     87        Keyboard.Listen(Key.W, ButtonState.Released, player1.setVelocity, null, Vector.Zero); 
     88        Keyboard.Listen(Key.S, ButtonState.Released, player1.setVelocity, null, Vector.Zero); 
    6089 
    61         Keyboard.Listen(Key.Up, ButtonState.Down, setVelocity, "Move Player2 Up", player2, moveUp); 
    62         Keyboard.Listen(Key.Down,  ButtonState.Down, setVelocity, "Move Player2 Down", player2, moveDown); 
    63         Keyboard.Listen(Key.Up, ButtonState.Released, setVelocity, null, player2, Vector.Zero); 
    64         Keyboard.Listen(Key.Down, ButtonState.Released, setVelocity, null, player2, Vector.Zero); 
    65     } 
     90        Keyboard.Listen(Key.Up, ButtonState.Down, player2.setVelocity, "Move Player2 Up", moveUp); 
     91        Keyboard.Listen(Key.Down, ButtonState.Down, player2.setVelocity, "Move Player2 Down", moveDown); 
     92        Keyboard.Listen(Key.Up, ButtonState.Released, player2.setVelocity, null, Vector.Zero); 
     93        Keyboard.Listen(Key.Down, ButtonState.Released, player2.setVelocity, null, Vector.Zero); 
    6694 
    67     private void setVelocity(IPhysicsObject obj, Vector v) 
    68     { 
    69         if (obj.Bottom < Level.Bottom && v.Y < 0) 
    70         { 
    71             obj.Velocity = Vector.Zero; 
    72             return; 
    73         }else 
    74             if (obj.Top > Level.Top && v.Y > 0) 
    75             { 
    76                 obj.Velocity = Vector.Zero; 
    77                 return; 
    78             } 
    79         obj.Velocity = v; 
     95        Keyboard.Listen(Key.F12, ButtonState.Pressed, ball1.Hit, "CHEAT!!!", new Vector(500, 0)); 
    8096    } 
    8197 
     
    85101        ball1.Hit(new Vector(500, 0)); 
    86102        ball1.Restitution = 1.0; 
     103        ball1.KineticFriction = 0.0; 
     104        ball1.CanRotate = false; 
    87105        Add(ball1); 
     106        AddCollisionHandler(ball1, handleBallCollision); 
    88107    } 
    89108 
    90     private void setupPlayers() 
     109    private void handleBallCollision(PhysicsObject obj, PhysicsObject target) 
    91110    { 
    92         player1 = PhysicsObject.CreateStaticObject(30, 150); 
     111        if (target == leftBorder) 
     112            meter2.AddValue(1); 
     113        else 
     114            if (target == rightBorder) 
     115                meter1.AddValue(1); 
     116    } 
     117 
     118    protected override void Update(Time time) 
     119    { 
     120        if (meter1.Value == 10) 
     121            gameWonMenu("Player1"); 
     122        else 
     123            if (meter2.Value == 10) 
     124                gameWonMenu("Player2"); 
     125        base.Update(time); 
     126    } 
     127 
     128    private void gameWonMenu(string player) 
     129    { 
     130        meter1.Value = 0; 
     131        meter2.Value = 0; 
     132        ball1.Destroy(); 
     133        ball1 = null; 
     134        IsMouseVisible = true; 
     135 
     136        string wintext = "Congratulations! " + player + " won the game! Play again?"; 
     137        string[] texts = { "Yes", "No" }; 
     138 
     139        MultiSelectWindow winWindow = new MultiSelectWindow(wintext, texts); 
     140        winWindow.AddItemHandler(0, delegate { setupNewGame(winWindow); }); 
     141        winWindow.AddItemHandler(1, Exit); 
     142        Add(winWindow); 
     143    } 
     144 
     145    private void setupNewGame(MultiSelectWindow window) 
     146    { 
     147        setupBall(); 
    93148        player1.Position = new Vector(Level.Left + 20, 0); 
    94         player1.Restitution = 1.0; 
    95         player1.KineticFriction = 0.0; 
    96         Add(player1); 
    97  
    98         player2 = PhysicsObject.CreateStaticObject(30, 150); 
    99149        player2.Position = new Vector(Level.Right - 20, 0); 
    100         player2.Restitution = 1.0; 
    101         player2.KineticFriction = 0.0; 
    102         Add(player2); 
    103150    } 
    104151} 
  • 2012/27/DenisZ/Pong/Pong/Pong/Pong.csproj

    r3424 r3437  
    111111  </ItemGroup> 
    112112  <ItemGroup> 
     113    <Compile Include="Player.cs" /> 
    113114    <Compile Include="Ohjelma.cs" /> 
    114115    <Compile Include="Pong.cs" /> 
Note: See TracChangeset for help on using the changeset viewer.