- Timestamp:
- 2016-06-19 15:46:51 (7 years ago)
- Location:
- 2016/25/JesseK
- Files:
-
- 132 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
2016/25/JesseK/Pong/Pong/Pong/Pong.cs
r7452 r7463 10 10 { 11 11 PhysicsObject ball; 12 Vector nopeusYlos = new Vector(0, 400); 13 Vector nopeusAlas = new Vector(0, -400); 14 15 PhysicsObject maila1; 16 PhysicsObject maila2; 17 PhysicsObject vasenReuna; 18 PhysicsObject oikeaReuna; 19 IntMeter pelaajan1Pisteet; 20 IntMeter pelaajan2Pisteet; 21 22 23 12 24 public override void Begin() 13 25 { 14 15 // TODO: Kirjoita ohjelmakoodisi tähän 16 LuoKentta(); 17 Vector impulssi = new Vector(500.0, 0.0); 18 ball.Hit(impulssi); 26 27 LuoKentta(); 28 AsetaOhjaimet(); 29 LisaaLaskurit(); 30 AloitaPeli(); 31 32 19 33 20 Level.Background.CreateGradient(Color.Black,Color.Blue); 34 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 35 36 // TODO: Kirjoita ohjelmakoodisi tähän 37 38 39 40 Level.Background.CreateGradient(Color.Black, Color.Blue); 21 41 PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); 22 42 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 23 43 Camera.ZoomToLevel(); 24 44 45 } 46 47 48 49 50 51 52 53 void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 54 { 55 if (kohde == oikeaReuna) 56 { 57 pelaajan1Pisteet.Value += 1; 58 } 59 else if (kohde == vasenReuna) 60 { 61 pelaajan2Pisteet.Value += 1; 62 } 25 63 } 26 64 void LuoKentta() 65 27 66 { 67 28 68 ball = new PhysicsObject(50, 50); 29 69 ball.Shape = Shape.Circle; 70 AddCollisionHandler(ball, KasittelePallonTormays); 30 71 ball.Color = Color.Black; 31 72 Add(ball); … … 33 74 ball.X = -200.0; 34 75 ball.Restitution = 1.0; 35 Level.CreateBorders(1.0, false); 76 vasenReuna = Level.CreateLeftBorder(); 77 oikeaReuna = Level.CreateRightBorder(); 78 PhysicsObject ylaReuna = Level.CreateTopBorder(); 79 ylaReuna.Restitution = 1.0; 80 ylaReuna.IsVisible = false; 81 PhysicsObject alaReuna = Level.CreateBottomBorder(); 82 alaReuna.Restitution = 1.0; 83 alaReuna.IsVisible = false; 84 oikeaReuna.Restitution = 1.0; 85 oikeaReuna.IsVisible = false; 86 vasenReuna.Restitution = 1.0; 87 vasenReuna.IsVisible = false; 36 88 89 90 maila1 = LuoMaila(Level.Left + 20.0, 0.0); 91 maila2 = LuoMaila(Level.Right - 20.0, 0.0); 92 93 } 94 95 void LisaaLaskurit() 96 { 97 pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 98 pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 99 } 100 101 102 103 104 105 IntMeter LuoPisteLaskuri(double x, double y) 106 { 107 IntMeter laskuri = new IntMeter(0); 108 laskuri.MaxValue = 10; 109 Label naytto = new Label(); 110 naytto.BindTo(laskuri); 111 naytto.X = x; 112 naytto.Y = y; 113 naytto.TextColor = Color.White; 114 naytto.BorderColor = Level.Background.Color; 115 naytto.Color = Level.Background.Color; 116 Add(naytto); 117 return laskuri; 118 } 119 120 121 PhysicsObject LuoMaila(double x, double y) 122 { 37 123 PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 38 124 maila.Shape = Shape.Rectangle; 39 maila.X = Level.Left + 20.0;40 maila.Y = 0.0;125 maila.X = x; 126 maila.Y = y; 41 127 maila.Restitution = 1.0; 42 128 Add(maila); 43 44 129 return maila; 130 } 131 void AloitaPeli() 132 { 133 Vector impulssi = new Vector(500.0, 0.0); 134 ball.Hit(impulssi); 45 135 46 136 47 137 } 48 138 49 void AloitaPeli() 139 140 void AsetaOhjaimet() 141 50 142 { 143 Keyboard.Listen(Key.F, ButtonState.Pressed,LyoPalloa, null); 144 Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 145 Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 146 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 147 Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 148 Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 149 Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 150 Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 151 Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 152 Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 153 Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 154 } 51 155 156 void LyoPalloa() 157 { 158 ball.Velocity = new Vector(500, 10); 52 159 } 160 void AsetaNopeus(PhysicsObject maila, Vector nopeus) 161 162 163 { 164 if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 165 { 166 maila.Velocity = Vector.Zero; 167 return; 168 } 169 if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 170 { 171 maila.Velocity = Vector.Zero; 172 return; 173 } 174 175 maila.Velocity = nopeus; 176 } 177 53 178 } 179 180 181 54 182 55 183 56 184 57
Note: See TracChangeset
for help on using the changeset viewer.