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

Revision 3985, 4.7 KB checked in by anlakane, 7 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
41    IntMeter create_calculator(double x, double y)
42    {
43    IntMeter counter = new IntMeter(0);
44    counter.MaxValue = 10;
45
46
47    Label screen = new Label();
48    screen.BindTo(counter);
49    screen.X = x;
50    screen.Y = y;
51    screen.TextColor = Color.Black;
52    screen.BorderColor = Level.BackgroundColor;
53    screen.Color = Level.BackgroundColor;
54    Add(screen);
55       
56    return counter;
57    }
58    void field_creation()
59    {
60        ball = new PhysicsObject(50, 50);
61        ball.Image = greenball;
62        ball.Restitution = 1;
63        ball.Shape = Shape.Circle;
64        ball.X = -200;
65        ball.Y = 0;
66        ball.MomentOfInertia = Double.PositiveInfinity;
67        ball.KineticFriction = 0.0;
68        ball.CanRotate = false;
69        Add(ball);
70
71        P1 = createplayer(Level.Left + 100, 0);
72        P2 = createplayer(Level.Right - 100, 0);
73
74       
75        leftborder = Level.CreateLeftBorder();
76        leftborder.Restitution = 1;
77        leftborder.IsVisible = false;
78       
79        rightborder = Level.CreateRightBorder();
80        rightborder.Restitution = 1;
81        rightborder.IsVisible = false;
82
83        PhysicsObject ylaReuna = Level.CreateTopBorder();
84        ylaReuna.Restitution = 1.0;
85        ylaReuna.KineticFriction = 0.0;
86        ylaReuna.IsVisible = false;
87
88        PhysicsObject alaReuna = Level.CreateBottomBorder();
89        alaReuna.Restitution = 1.0;
90        alaReuna.IsVisible = false;
91        alaReuna.KineticFriction = 0.0;
92
93        Level.BackgroundColor = Color.Azure;
94        Camera.ZoomToLevel();
95        AddCollisionHandler(ball,ballcollidecontrol);
96    }
97    void start()
98    {
99        Vector impulssi = new Vector(500.0, 0.0);
100        ball.Hit(impulssi);
101    }
102    void create_keys()
103    {
104        Keyboard.Listen(Key.W, ButtonState.Down, set_velocity, "move player 1 up", P1, P_up);
105        Keyboard.Listen(Key.W, ButtonState.Released, set_velocity, null, P1, Vector.Zero);
106        Keyboard.Listen(Key.S, ButtonState.Down, set_velocity, "move player 1 down", P1, P_down);
107        Keyboard.Listen(Key.S, ButtonState.Released, set_velocity, null, P1, Vector.Zero);
108        Keyboard.Listen(Key.Up, ButtonState.Down, set_velocity, "move player 2 up", P2, P_up);
109        Keyboard.Listen(Key.Up, ButtonState.Released, set_velocity, null, P2, Vector.Zero);
110        Keyboard.Listen(Key.Down, ButtonState.Down, set_velocity, "move player 2 down", P2, P_down);
111        Keyboard.Listen(Key.Down, ButtonState.Released, set_velocity, null, P2, Vector.Zero);
112        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Stop game");
113        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "show controls");
114
115
116    }
117    PhysicsObject createplayer(double x,double y)
118    {
119        PhysicsObject player = PhysicsObject.CreateStaticObject(20.0, 100);
120        player.Shape = Shape.Rectangle;
121        player.Color = Color.Black;
122        player.X = x;
123        player.Y = y;
124        player.Restitution = 1.0;
125        Add(player);
126        return player;
127    }
128    void set_velocity(PhysicsObject player, Vector speed)
129    {
130        if ((player.Top > Level.Top)&& (speed.Y > 0))
131        {
132            player.Velocity = Vector.Zero;
133            return;
134        }
135        if ((player.Bottom < Level.Bottom) && (speed.Y < 0))
136        {
137            player.Velocity = Vector.Zero;
138            return;
139        }
140        player.Velocity = speed;
141    }
142    void ballcollidecontrol(PhysicsObject ball, PhysicsObject kohde)
143    {
144        if (kohde == rightborder)
145        {
146            player1Points.Value += 1;
147        }
148        else if (kohde == leftborder)
149        {
150            player2Points.Value += 1;
151        }
152    }
153}
154   
Note: See TracBrowser for help on using the repository browser.