source: 2013/23/JoniK/pp/pp/pp/Pong2.cs @ 4030

Revision 4030, 4.7 KB checked in by jonkurki, 8 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Pong2 : PhysicsGame
10{
11    Image greenball = LoadImage("ball main");
12    PhysicsObject ball;
13    PhysicsObject P1;
14    PhysicsObject P2;
15    IntMeter player1Points;
16    IntMeter player2Points;
17    PhysicsObject leftborder;
18    PhysicsObject rightborder;
19   
20    Vector P_up = new Vector(0, 500);
21    Vector P_down = new Vector (0,-500);
22
23        public override void Begin()
24    {
25        field_creation();
26        start();
27        create_keys();
28        add_calculator();
29        // TODO: Kirjoita ohjelmakoodisi tähän
30
31        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
32        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
33
34    }
35    void add_calculator()
36    {
37    player1Points = create_calculator(Screen.Left + 100.0, Screen.Top - 100.0);
38    player2Points = create_calculator(Screen.Right - 100.0, Screen.Top - 100.0);
39    }
40    IntMeter create_calculator(double x, double y)
41    {
42    IntMeter counter = new IntMeter(0);
43    counter.MaxValue = 10;
44
45
46    Label screen = new Label();
47    screen.BindTo(counter);
48    screen.X = x;
49    screen.Y = y;
50    screen.TextColor = Color.Black;
51    screen.BorderColor = Level.BackgroundColor;
52    screen.Color = Level.BackgroundColor;
53    Add(screen);
54       
55    return counter;
56    }
57    void field_creation()
58    {
59        ball = new PhysicsObject(50, 50);
60        ball.Image = greenball;
61        ball.Restitution = 1;
62        ball.Shape = Shape.Circle;
63        ball.X = -200;
64        ball.Y = 0;
65        ball.MomentOfInertia = Double.PositiveInfinity;
66        ball.KineticFriction = 0.0;
67        ball.CanRotate = false;
68        Add(ball);
69
70        P1 = createplayer(Level.Left + 100, 0);
71        P2 = createplayer(Level.Right - 100, 0);
72
73       
74        leftborder = Level.CreateLeftBorder();
75        leftborder.Restitution = 1;
76        leftborder.IsVisible = false;
77       
78        rightborder = Level.CreateRightBorder();
79        rightborder.Restitution = 1;
80        rightborder.IsVisible = false;
81
82        PhysicsObject ylaReuna = Level.CreateTopBorder();
83        ylaReuna.Restitution = 1.0;
84        ylaReuna.KineticFriction = 0.0;
85        ylaReuna.IsVisible = false;
86
87        PhysicsObject alaReuna = Level.CreateBottomBorder();
88        alaReuna.Restitution = 1.0;
89        alaReuna.IsVisible = false;
90        alaReuna.KineticFriction = 0.0;
91
92        Level.BackgroundColor = Color.Azure;
93        Camera.ZoomToLevel();
94        AddCollisionHandler(ball,ballcollidecontrol);
95    }
96    void start()
97    {
98        Vector impulssi = new Vector(500.0, 0.0);
99        ball.Hit(impulssi);
100    }
101    void create_keys()
102    {
103        Keyboard.Listen(Key.W, ButtonState.Down, set_velocity, "move player 1 up", P1, P_up);
104        Keyboard.Listen(Key.W, ButtonState.Released, set_velocity, null, P1, Vector.Zero);
105        Keyboard.Listen(Key.S, ButtonState.Down, set_velocity, "move player 1 down", P1, P_down);
106        Keyboard.Listen(Key.S, ButtonState.Released, set_velocity, null, P1, Vector.Zero);
107        Keyboard.Listen(Key.Up, ButtonState.Down, set_velocity, "move player 2 up", P2, P_up);
108        Keyboard.Listen(Key.Up, ButtonState.Released, set_velocity, null, P2, Vector.Zero);
109        Keyboard.Listen(Key.Down, ButtonState.Down, set_velocity, "move player 2 down", P2, P_down);
110        Keyboard.Listen(Key.Down, ButtonState.Released, set_velocity, null, P2, Vector.Zero);
111        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Stop game");
112        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "show controls");
113
114
115    }
116    PhysicsObject createplayer(double x,double y)
117    {
118        PhysicsObject player = PhysicsObject.CreateStaticObject(20.0, 100);
119        player.Shape = Shape.Rectangle;
120        player.Color = Color.Black;
121        player.X = x;
122        player.Y = y;
123        player.Restitution = 1.0;
124        Add(player);
125        return player;
126    }
127    void set_velocity(PhysicsObject player, Vector speed)
128    {
129        if ((player.Top > Level.Top)&& (speed.Y > 0))
130        {
131            player.Velocity = Vector.Zero;
132            return;
133        }
134        if ((player.Bottom < Level.Bottom) && (speed.Y < 0))
135        {
136            player.Velocity = Vector.Zero;
137            return;
138        }
139        player.Velocity = speed;
140    }
141    void ballcollidecontrol(PhysicsObject ball, PhysicsObject kohde)
142    {
143        if (kohde == rightborder)
144        {
145            player1Points.Value += 1;
146        }
147        else if (kohde == leftborder)
148        {
149            player2Points.Value += 1;
150        }
151    }
152}
153   
Note: See TracBrowser for help on using the repository browser.