Changeset 2956
- Timestamp:
- 2012-06-12 14:39:31 (11 years ago)
- Location:
- 2012/24/AnnaS
- Files:
-
- 135 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
2012/24/AnnaS/pong/pong/pong/pong.cs
r2929 r2956 9 9 public class pong : PhysicsGame 10 10 { 11 Vector nopeusYlos = new Vector(0, 200); 12 Vector nopeusAlas = new Vector(0, -200); 13 11 14 PhysicsObject pallo; 15 PhysicsObject maila1; 16 PhysicsObject maila2; 17 PhysicsObject vasenReuna; 18 PhysicsObject oikeaReuna; 19 20 21 IntMeter pelaajan1Pisteet; 22 IntMeter pelaajan2Pisteet; 23 12 24 13 25 public override void Begin() 14 26 { 15 16 27 // TODO: Kirjoita ohjelmakoodisi tähän 17 28 LuoKentta(); 18 Vector impulssi = new Vector(800.0, 0.0); 19 pallo.Hit(impulssi); 29 AsetaOhjaimet(); 30 LisaaLaskurit(); 31 AloitaPeli(); 32 20 33 21 34 PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); … … 25 38 void LuoKentta() 26 39 { 27 LuoMaila(Level.Left + 20.0, 0.0); 28 LuoMaila(Level.Right - 20.0, 0.0); 29 30 31 32 pallo = new PhysicsObject(60, 50); 33 40 maila1 = LuoMaila(Level.Left + 20.0, 0.0); 41 maila2 = LuoMaila(Level.Right - 20.0, 0.0); 42 43 pallo = new PhysicsObject(60, 60); 34 44 35 45 Add(pallo); 36 46 pallo.Shape = Shape.Circle; 37 47 pallo.Color = Color.Fuchsia; 38 pallo.X = - 500.0;48 pallo.X = -300.0; 39 49 pallo.Y = 300.0; 40 50 pallo.Restitution = 1.0; 51 pallo.KineticFriction = 0.0; 52 pallo.MomentOfInertia = Double.PositiveInfinity; 41 53 42 43 54 44 Level.CreateBorders(1.0, false); 55 56 57 AddCollisionHandler(pallo, KasittelePallonTormays); 58 59 vasenReuna = Level.CreateLeftBorder(); 60 vasenReuna.Restitution = 1.0; 61 vasenReuna.IsVisible = false; 62 vasenReuna.KineticFriction = 0.0; 63 64 oikeaReuna = Level.CreateRightBorder(); 65 oikeaReuna.Restitution = 1.0; 66 oikeaReuna.IsVisible = false; 67 oikeaReuna.KineticFriction = 0.0; 68 69 PhysicsObject alaReuna = Level.CreateBottomBorder(); 70 alaReuna.Restitution = 1.0; 71 alaReuna.IsVisible = false; 72 alaReuna.KineticFriction = 0.0; 73 74 PhysicsObject yläReuna = Level.CreateTopBorder(); 75 yläReuna.Restitution = 1.0; 76 yläReuna.IsVisible = false; 77 yläReuna.KineticFriction = 0.0; 78 79 45 80 Level.BackgroundColor = Color.Pink; 46 81 Camera.ZoomToLevel(); … … 49 84 50 85 86 void AsetaOhjaimet() 87 { 88 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 89 Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 90 Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 91 Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 92 Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 51 93 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 99 Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 100 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 101 } 102 103 104 void AsetaNopeus(PhysicsObject maila, Vector nopeus) 105 { 106 if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 107 { 108 maila.Velocity = Vector.Zero; 109 return; 110 } 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 } 52 122 53 123 void AloitaPeli() … … 55 125 Vector impulssi = new Vector(500.0, 0.0); 56 126 pallo.Hit(impulssi); 127 128 129 57 130 } 58 131 59 void LuoMaila() 132 133 PhysicsObject LuoMaila(double x, double y) 60 134 { 61 62 135 PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 63 136 maila.Shape = Shape.Rectangle; 64 maila.X = Level.Left + 20.0;65 maila.Y = 0.0;137 maila.X = x; 138 maila.Y = y; 66 139 maila.Restitution = 1.0; 67 140 Add(maila); 68 141 142 return maila; 143 } 144 145 void LisaaLaskurit() 146 { 147 148 pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 149 pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 150 } 151 152 153 154 155 IntMeter LuoPisteLaskuri(double x, double y) 156 { 157 IntMeter laskuri = new IntMeter(0); 158 laskuri.MaxValue = 10; 159 Label naytto = new Label(); 160 naytto.BindTo(laskuri); 161 naytto.X = x; 162 naytto.Y = y; 163 naytto.TextColor = Color.White; 164 naytto.BorderColor = Level.BackgroundColor; 165 naytto.Color = Level.BackgroundColor; 166 Add(naytto); 167 168 return laskuri; 169 } 170 171 void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 172 { 173 if (kohde == oikeaReuna) 174 { 175 pelaajan1Pisteet.Value += 1; 176 } 177 else if (kohde == vasenReuna) 178 { 179 pelaajan2Pisteet.Value += 1; 180 181 } 69 182 70 183 } 71 184 } 185 186
Note: See TracChangeset
for help on using the changeset viewer.