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