source: 2012/27/DenisZ/Pong/Pong/Pong/Pong.cs @ 3424

Revision 3424, 3.4 KB checked in by dezhidki, 9 years ago (diff)

Talletus.

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Pong : PhysicsGame
10{
11    private PhysicsObject ball1;
12    private PhysicsObject player1, player2;
13    private Vector moveUp = new Vector(0, 500);
14    private Vector moveDown = new Vector(0, -500);
15
16    private IntMeter meter1, meter2;
17
18    public override void Begin()
19    {
20        SetWindowSize(800, 600);
21        Level.BackgroundColor = Color.Black;
22        Level.Width = Screen.Width + Screen.Width / 2;
23        Level.Height = Screen.Height + Screen.Height / 2;
24        Level.CreateBorders(1.0, false);
25        Camera.ZoomToLevel();
26
27        setupBall();
28        setupPlayers();
29        setupKeys();
30
31        meter1 = createMeter(new Vector(Level.Left + 500, Level.Top - 200), 10);
32        meter2 = createMeter(new Vector(Level.Right - 500, Level.Top - 200), 10);
33
34        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
35        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopeta peli");
36    }
37
38    private IntMeter createMeter(Vector pos ,int maxValue)
39    {
40        IntMeter meter = new IntMeter(0, 0 , maxValue);
41
42        Label frame = new Label();
43        frame.BindTo(meter);
44        frame.TextColor = Color.White;
45        frame.TextScale = new Vector(1.5, 1.5);
46        frame.Color = Level.BackgroundColor;
47        frame.Position = pos;
48        Add(frame);
49
50        return meter;
51    }
52
53    private void setupKeys()
54    {
55        Keyboard.Listen(Key.W, ButtonState.Down, setVelocity, "Move Player1 Up", player1 , moveUp);
56        Keyboard.Listen(Key.S, ButtonState.Down, setVelocity, "Move Player1 Down", player1, moveDown);
57        Keyboard.Listen(Key.W, ButtonState.Released, setVelocity, null, player1, Vector.Zero);
58        Keyboard.Listen(Key.S, ButtonState.Released, setVelocity, null, player1, Vector.Zero);
59
60        Keyboard.Listen(Key.Up, ButtonState.Down, setVelocity, "Move Player2 Up", player2, moveUp);
61        Keyboard.Listen(Key.Down,  ButtonState.Down, setVelocity, "Move Player2 Down", player2, moveDown);
62        Keyboard.Listen(Key.Up, ButtonState.Released, setVelocity, null, player2, Vector.Zero);
63        Keyboard.Listen(Key.Down, ButtonState.Released, setVelocity, null, player2, Vector.Zero);
64    }
65
66    private void setVelocity(IPhysicsObject obj, Vector v)
67    {
68        if (obj.Bottom < Level.Bottom && v.Y < 0)
69        {
70            obj.Velocity = Vector.Zero;
71            return;
72        }else
73            if (obj.Top > Level.Top && v.Y > 0)
74            {
75                obj.Velocity = Vector.Zero;
76                return;
77            }
78        obj.Velocity = v;
79    }
80
81    private void setupBall()
82    {
83        ball1 = new PhysicsObject(75, 75, Shape.Circle);
84        ball1.Hit(new Vector(500, 0));
85        ball1.Restitution = 1.0;
86        Add(ball1);
87    }
88
89    private void setupPlayers()
90    {
91        player1 = PhysicsObject.CreateStaticObject(30, 150);
92        player1.Position = new Vector(Level.Left + 20, 0);
93        player1.Restitution = 1.0;
94        player1.KineticFriction = 0.0;
95        Add(player1);
96
97        player2 = PhysicsObject.CreateStaticObject(30, 150);
98        player2.Position = new Vector(Level.Right - 20, 0);
99        player2.Restitution = 1.0;
100        player2.KineticFriction = 0.0;
101        Add(player2);
102    }
103}
Note: See TracBrowser for help on using the repository browser.