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