- Timestamp:
- 2010-08-03 15:00:47 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
2010/31/hesasnel/Pong/Peli.cs
r1537 r1578 6 6 public class Peli : PhysicsGame 7 7 { 8 Vector nopeusYlos = new Vector(0, 200); 9 Vector nopeusAlas = new Vector ( 0,-200); 10 PhysicsObject pallo; 11 PhysicsObject maila1; 12 PhysicsObject maila2; 13 PhysicsObject vasenReuna 14 PhysicsObject oikeaReuna 15 IntMeter pelaajan1Pisteet; 16 IntMeter pelaajan2Pisteet; 8 17 protected override void Begin() 9 18 { 10 PhysicsObject pallo = new PhysicsObject(60.0, 60.0); 11 Add(pallo); 12 pallo.Shape = Shapes.Triangle; 13 pallo.Restitution = 0.1; 14 Level.BackgroundColor = Color.Purple; 15 Level.CreateBorders( 1.0, false); 16 Vector impulssi = new Vector(400.0, 600.0); 17 pallo.Hit(impulssi); 18 Camera.ZoomToLevel(); 19 LuoKentta(); 20 AsetaOhjaimet(); 21 LisaaLaskurit(); 22 AloitaPeli(); 23 19 24 } 20 25 void LuoKentta() 21 26 { 27 pallo = new PhysicsObject(100.0, 80.0); 28 Add(pallo); 29 pallo.Shape = Shapes.Triangle; 30 pallo.Restitution = 1.0; 31 32 AddCollisionHandler(pallo, KasittelePallonTormays); 33 34 maila1 = LuoMaila (Level.Left + 20.0, 0.0 ); 35 maila2 = LuoMaila (Level.Right - 20.0, 0.0 ); 36 37 Level.BackgroundColor = Color.Purple; 38 vasenReuna = Level.CreateLeftBorder(); 39 vasenReuna.Restitution = 1.0; 40 vasenReuna.IsVisible = false; 41 42 oikeaReuna = Level.CreateRightBorder(); 43 oikeaReuna.Restitution = 1.0; 44 oikeaReuna.IsVisible = false; 45 46 PhysicsObject alaReuna = Level.CreateBottomBorder(); 47 alaReuna.Restitution = 1.0; 48 alaReuna.IsVisible = false; 49 50 PhysicsObject yläReuna = Level.CreateTopBorder(); 51 yläReuna.Restitution = 1.0; 52 yläReuna.IsVisible = false; 53 54 55 56 Camera.ZoomToLevel(); 22 57 } 23 58 void AloitaPeli() 59 { 60 Vector impulssi = new Vector(1500.0, 0.0); 61 pallo.Hit(impulssi); 62 } 63 PhysicsObject LuoMaila(double x, double y) 64 { 65 PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); 66 maila.Shape = Shapes.Rectangle; 67 maila.X = x; 68 maila.Y = y; 69 maila.Restitution = 1.0; 70 Add(maila); 71 return maila; 72 } 73 void AsetaOhjaimet() 74 { 75 Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); 76 Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 77 Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); 78 Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); 79 Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); 80 Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus,null,maila2,Vector.Zero); 81 Keyboard.Listen(Key.Down,ButtonState.Down, AsetaNopeus,"Pelaaja 2: Liikuta mailaa alas",maila2,nopeusAlas); 82 Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus,null,maila2,Vector.Zero); 83 Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); 84 Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu" ); 85 } 86 void AsetaNopeus(PhysicsObject maila, Vector nopeus) 87 { 88 if ((nopeus.Y < 0) && (maila.Bottom < Level.Bottom)) 89 { 90 maila.Velocity = Vector.Zero; 91 return; 92 } 93 if ((nopeus.Y>0)&&(maila.Top > Level.Top)) 94 { 95 maila.Velocity = Vector.Zero; 96 return; 97 } 98 maila.Velocity = nopeus; 99 } 100 void LisaaLaskurit() 101 { 102 pelaajan1Pisteet = LuoPisteLaskuri (Screen.Left + 100.0, Screen.Top - 100.0); 103 pelaajan2Pisteet = LuoPisteLaskuri (Screen.Right - 100.0,Screen.Top -100.0); 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.Cyan ; 114 Add(naytto); 115 return laskuri; 116 } 117 void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) 118 { 119 if (kohde == oikeaReuna) 120 { 121 pelaajan1Pisteet.Value += 1; 122 } 123 else if (kohde == vasenReuna) 124 { 125 pelaajan2Pisteet.Value += 1; 126 } 127 128 } 24 129 }
Note: See TracChangeset
for help on using the changeset viewer.