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

Revision 3432, 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 + 300, Level.Top - 200), 10);
32        meter2 = createMeter(new Vector(Level.Right - 300, 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.SizeMode = TextSizeMode.AutoSize;
46       // frame.TextSize = new Vector(10, 10);
47        frame.Color = Level.BackgroundColor;
48        frame.Position = pos;
49        Add(frame);
50
51        return meter;
52    }
53
54    private void setupKeys()
55    {
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);
60
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    }
66
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;
80    }
81
82    private void setupBall()
83    {
84        ball1 = new PhysicsObject(75, 75, Shape.Circle);
85        ball1.Hit(new Vector(500, 0));
86        ball1.Restitution = 1.0;
87        Add(ball1);
88    }
89
90    private void setupPlayers()
91    {
92        player1 = PhysicsObject.CreateStaticObject(30, 150);
93        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);
99        player2.Position = new Vector(Level.Right - 20, 0);
100        player2.Restitution = 1.0;
101        player2.KineticFriction = 0.0;
102        Add(player2);
103    }
104}
Note: See TracBrowser for help on using the repository browser.