- Timestamp:
- 2016-06-19 15:46:09 (5 years ago)
- Location:
- 2016/25/SaanaR
- Files:
-
- 32 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
2016/25/SaanaR/FysiikkaPeli1/FysiikkaPeli1/FysiikkaPeli1/Pong.cs
r7453 r7459 10 10 { 11 11 PhysicsObject pallo; 12 PhysicsObject maila1; 13 PhysicsObject maila2; 14 15 PhysicsObject vasenReuna; 16 PhysicsObject oikeaReuna; 17 18 IntMeter pelaajan1Pisteet; 19 IntMeter pelaajan2Pisteet; 20 21 Vector nopeusYlos = new Vector(0, 200); 22 Vector nopeusAlas = new Vector(0, -200); 12 23 13 24 public override void Begin() 14 25 { 15 26 LuoKentta(); 27 AloitaPeli(); 28 AsetaOhjaimet(); 29 LisaaLaskurit(); 16 30 // TODO: Kirjoita ohjelmakoodisi tähän 31 } 32 void LisaaLaskurit() 33 { 34 pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); 35 pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); 36 } 37 IntMeter LuoPisteLaskuri(double x, double y) 38 { 39 IntMeter laskuri = new IntMeter(0); 40 laskuri.MaxValue = 10; 17 41 18 Vector impulssi = new Vector(-500.0, 0.0); 19 pallo.Hit(impulssi); 42 Label naytto = new Label(); 43 naytto.BindTo(laskuri); 44 naytto.X = x; 45 naytto.Y = y; 46 naytto.TextColor = Color.Black; 47 naytto.BorderColor = Level.Background.Color; 48 naytto.Color = Level.Background.Color; 49 Add(naytto); 50 51 return laskuri; 52 } 53 // ... 54 55 void AsetaNopeus(PhysicsObject maila, Vector nopeus) 56 { 57 if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 58 { 59 maila.Velocity = Vector.Zero; 60 return; 61 } 62 if ((nopeus.Y > 0) && (maila.Top > Level.Top)) 63 { 64 maila.Velocity = Vector.Zero; 65 return; 66 } 67 maila.Velocity = nopeus; 68 69 } 70 void AsetaOhjaimet() 71 { 20 72 PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); 21 73 Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 74 Keyboard.Listen(Key.W, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 75 Keyboard.Listen(Key.W, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 76 Keyboard.Listen(Key.S, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 77 Keyboard.Listen(Key.S, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 78 79 Keyboard.Listen(Key.Space, ButtonState.Pressed, AloitaPeli, null); 80 81 Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 82 Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 83 Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); 84 Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); 85 86 87 Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 22 88 } 23 89 void LuoKentta() 24 90 { 25 pallo = new PhysicsObject(50, 50); 91 pallo = new PhysicsObject(50, 50); 92 AddCollisionHandler(pallo, KasittelePallonTormays); 26 93 pallo.Shape = Shape.Circle; 27 94 pallo.Color = Color.White; … … 30 97 Add(pallo); 31 98 32 Level.CreateBorders(1.0, false); 99 maila1 = LuoMaila(Level.Left + 20.0, 0.0); 100 maila2 = LuoMaila(Level.Right - 20.0, 0.0); 101 vasenReuna = Level.CreateLeftBorder(); 102 vasenReuna.Restitution = 1.0; 103 vasenReuna.IsVisible = false; 104 105 oikeaReuna = Level.CreateRightBorder(); 106 oikeaReuna.Restitution = 1.0; 107 oikeaReuna.IsVisible = false; 108 109 PhysicsObject alaReuna = Level.CreateBottomBorder(); 110 alaReuna.Restitution = 1.0; 111 alaReuna.IsVisible = false; 112 113 PhysicsObject ylaReuna = Level.CreateTopBorder(); 114 ylaReuna.Restitution = 1.0; 115 ylaReuna.IsVisible = false; 116 33 117 pallo.Restitution = 90; 34 118 Level.Background.CreateGradient(Color.LightBlue, Color.LightGreen); 35 119 Camera.ZoomToLevel(); 36 120 121 } 122 PhysicsObject LuoMaila(double x, double y) 123 { 37 124 PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 38 125 maila.Shape = Shape.Rectangle; 39 maila.X = Level.Left + 20.0;40 maila.Y = 0.0;126 maila.X = x; 127 maila.Y = y; 41 128 maila.Restitution = 1.0; 42 129 Add(maila); 130 return maila; 131 } 43 132 44 45 }46 133 void AloitaPeli() 47 134 { 48 135 Vector impulssi = new Vector(500.0, 0.0); 49 pallo.Hit(impulssi); Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); 136 pallo.Hit(impulssi); 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 50 149 } 51 150 } 52 53
Note: See TracChangeset
for help on using the changeset viewer.