Changeset 2704
- Timestamp:
- 2012-04-16 14:48:09 (10 years ago)
- Location:
- 2012/JAO/John/pong
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
2012/JAO/John/pong/pong/pong/pong.cs
r2688 r2704 9 9 public class pong : PhysicsGame 10 10 { 11 Vector nopeusYlos = new Vector(0, 200); 12 Vector nopeusalas = new Vector(0, -200); 13 PhysicsObject pallo; 14 PhysicsObject maila1; 15 PhysicsObject maila2; 16 IntMeter pelaajan1Pisteet; 17 IntMeter pelaajan2Pisteet; 18 PhysicsObject vasenReuna; 19 PhysicsObject oikeaReuna; 11 20 public override void Begin() 12 21 { 13 PhysicsObject pallo = new PhysicsObject(40.0, 40.0); 22 luokentta(); 23 Aloitapeli(); 24 Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "poistu"); 25 AsetaOhjaimet(); 26 LisaaLaskurit(); 27 28 29 30 31 32 33 pallo.Restitution = 1.0; 34 35 36 37 38 39 40 } 41 void luokentta() 42 { 43 pallo = new PhysicsObject(40.0, 40.0); 14 44 Add(pallo); 15 45 pallo.Shape = Shape.Circle; 16 46 pallo.X = -200.0; 17 47 pallo.Y = 0.0; 18 Vector impulssi = new Vector ( 500.0, 0.0 ); 48 AddCollisionHandler(pallo, KasittelePallonTormays); 49 50 maila1=LuoMaila(Level.Left + 20.0, 0.0); 51 maila1.Color = Color.Brown; 52 maila2=LuoMaila(Level.Right - 20.0, 0.0); 53 maila2.Color = Color.Brown; 54 Level.BackgroundColor = Color.Black; 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 ylaReuna = Level.CreateTopBorder(); 62 ylaReuna.Restitution = 1.0; 63 ylaReuna.IsVisible = false; 64 PhysicsObject alaReuna = Level.CreateBottomBorder(); 65 alaReuna.Restitution = 1.0; 66 alaReuna.IsVisible = false; 67 Camera.ZoomToLevel(); 68 } 69 void Aloitapeli() 70 { 71 Vector impulssi = new Vector(500.0, 0.0); 19 72 pallo.Hit(impulssi); 20 pallo.Restitution = 1.0;21 Level.BackgroundColor = Color.Black; 22 Level.CreateBorders(1.0, false);23 Camera.ZoomToLevel();73 } 74 75 PhysicsObject LuoMaila(double x, double y) 76 { 24 77 PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 25 78 maila.Shape = Shape.Rectangle; 26 maila.X = Level.Left + 20.0;27 maila.Y = 0.0;79 maila.X = x; 80 maila.Y =y; 28 81 maila.Restitution = 1.0; 29 82 Add(maila); 30 PhysicsObject maila2 = PhysicsObject.CreateStaticObject(20.0, 100.0); 31 maila.Shape = Shape.Rectangle; 32 maila.X = Level.Right - 20.0; 33 maila.Y = 0.0; 34 maila2.Restitution = 1.0; 35 Add( maila2 ); 36 PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); 37 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 83 return maila; 84 } 85 void AsetaOhjaimet() 86 { 87 Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 88 Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 89 Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); 90 Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusalas); 91 Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 92 Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 93 Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 94 Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2,nopeusalas); 95 Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 96 Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 97 98 } 99 void AsetaNopeus(PhysicsObject maila, Vector nopeus) 100 { 101 if (maila.Top > Level.Top) 102 { 103 maila.Velocity = Vector.Zero; 104 return; 105 } 106 107 maila.Velocity = nopeus; 108 } 109 void LisaaLaskurit() 110 { 111 pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 112 pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 113 } 114 IntMeter LuoPisteLaskuri(double x, double y) 115 { 116 IntMeter laskuri = new IntMeter(0); 117 laskuri.MaxValue = 10; 118 Label naytto = new Label(); 119 naytto.BindTo(laskuri); 120 naytto.X = x; 121 naytto.Y = y; 122 naytto.TextColor = Color.White; 123 naytto.BorderColor = Level.BackgroundColor; 124 naytto.Color = Level.BackgroundColor; 125 Add(naytto); 126 return laskuri; 127 } 128 void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 129 { 130 if (kohde == oikeaReuna) 131 { 132 pelaajan1Pisteet.Value += 1; 133 } 134 else if (kohde == vasenReuna) 135 { 136 pelaajan2Pisteet.Value += 1; 137 } 38 138 } 39 139 }
Note: See TracChangeset
for help on using the changeset viewer.