source: 2011/23/tueesuih/Pong/Pong/Pong/Peli.cs @ 1720

Revision 1720, 4.5 KB checked in by tueesuih, 12 years ago (diff)

Pong

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Peli : PhysicsGame
10{
11    Vector nopeusYlos = new Vector(0, 200);
12    Vector nopeusAlas = new Vector(0, -200);
13
14    PhysicsObject pallo;
15
16    PhysicsObject maila1;
17    PhysicsObject maila2;
18
19    IntMeter pelaajan1Pisteet;
20    IntMeter pelaajan2Pisteet;
21
22    PhysicsObject vasenReuna;
23    PhysicsObject oikeaReuna;
24
25
26    public override void Begin()
27    {
28        // TODO: Kirjoita ohjelmakoodisi tähän
29        Luokentta();
30        AsetaOhjaimet();
31        AloitaPeli();
32        LisaaLaskurit();
33    }
34
35    void Luokentta()
36    {
37        pallo = new PhysicsObject(50.0, 50.0);
38        pallo.Shape = Shape.Octagon;
39        pallo.Color = Color.Green;
40        pallo.X = -200.0;
41        pallo.Y = 0.0;
42        pallo.Restitution = 1.0;
43        Add(pallo);
44
45        vasenReuna = Level.CreateLeftBorder();
46        vasenReuna.Restitution = 1.0;
47        vasenReuna.IsVisible = false;
48
49        Level.BackgroundColor = Color.DarkGray;
50
51        oikeaReuna = Level.CreateRightBorder();
52        oikeaReuna.Restitution = 1.0;
53        oikeaReuna.IsVisible = false;
54
55        PhysicsObject ylaReuna = Level.CreateTopBorder();
56        ylaReuna.Restitution = 1.0;
57        ylaReuna.IsVisible = false;
58
59        PhysicsObject alaReuna = Level.CreateBottomBorder();
60        alaReuna.Restitution = 1.0;
61        alaReuna.IsVisible = false;
62
63
64        Camera.ZoomToLevel();
65
66        maila1 = LuoMaila(Level.Left + 20.0, 0.0);
67        maila2 = LuoMaila(Level.Right - 20.0, 0.0);
68
69        AddCollisionHandler(pallo, KasittelePallonTormays);
70
71    }
72    void AloitaPeli()
73    {
74        Vector impulssi = new Vector(500.0, 0.0);
75        pallo.Hit(impulssi);
76    }
77    PhysicsObject LuoMaila(double x, double y)
78    {
79        PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0);
80        maila.Shape = Shape.Rectangle;
81        maila.X = x;
82        maila.Y = y;
83        maila.Restitution = 1.0;
84        Add(maila);
85        return maila;
86    }
87    void AsetaOhjaimet()
88    {
89        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu");
90        Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos);
91        Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
92        Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas);
93        Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero);
94        Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos);
95        Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
96        Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas);
97        Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero);
98        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
99
100    }
101    void AsetaNopeus(PhysicsObject maila, Vector nopeus)
102    {
103        if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom))
104        {
105            maila.Velocity = Vector.Zero;
106            return;
107        }
108
109        if ((nopeus.Y > 0) && (maila.Top > Level.Top))
110        {
111            maila.Velocity = Vector.Zero;
112            return;
113        }
114
115        maila.Velocity = nopeus;
116    }
117    void LisaaLaskurit()
118    {
119        pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0);
120        pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0);
121
122    }
123
124    IntMeter LuoPisteLaskuri(double x, double y)
125    {
126        IntMeter laskuri = new IntMeter(0);
127        laskuri.MaxValue = 10;
128        Label naytto = new Label();
129        naytto.BindTo(laskuri);
130        naytto.X = x;
131        naytto.Y = y;
132        naytto.TextColor = Color.White;
133        naytto.BorderColor = Level.BackgroundColor;
134        naytto.Color = Level.BackgroundColor;
135        Add(naytto);
136        return laskuri;
137    }
138
139    void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde)
140    {
141        if (kohde == oikeaReuna)
142        {
143            pelaajan1Pisteet.Value += 1;
144        }
145        else if (kohde == vasenReuna)
146        {
147            pelaajan2Pisteet.Value += 1;
148        }
149    }
150}
Note: See TracBrowser for help on using the repository browser.