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