source: 2016/26/JennyN/Pong/Pong/Pong/Pong.cs @ 7523

Revision 7523, 4.6 KB checked in by jehinous, 7 years ago (diff)

Valmis!

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