source: 2014/27/ArttuH/pong leikkiminen/pong/pong/pong.cs @ 5175

Revision 5175, 4.5 KB checked in by arjuhoyl, 9 years ago (diff)

idea lista.txt luotu, pong leikkiminen lisätty

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