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