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