Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Jypeli;
- using Jypeli.Widgets;
- using Jypeli.Assets;
- using Jypeli.Effects;
- class Tasohyppely : PhysicsGame
- {
- #region Muuttujat
- const double nopeus = 60;
- const double hyppyVoima = 2500;
- const double RuudunLeveys = 20;
- const double RuudunKorkeus = 20;
- Image[] vihollisenKuvat = LoadImages(
- "pallo1", "pallo2", "pallo3", "pallo4", "pallo5", "pallo6", "pallo7", "pallo8",
- "pallo7", "pallo6", "pallo5", "pallo4", "pallo3", "pallo2", "pallo1" );
- Image[] pelaajanKuvat = LoadImages( "heppu_1", "heppu_2", "heppu_3", "heppu_4", "heppu_5" );
- Image[] vesiKuvat = LoadImages( "vesi1", "vesi2", "vesi3" );
- Image palikanKuva = LoadImage( "palikka" );
- Image tahdenKuva = LoadImage( "tahti" );
- IntMeter pisteLaskuri;
- Label pisteNaytto;
- PlatformCharacter pelaaja1;
- PlatformCharacter pelaaja2;
- List<PlatformCharacter> pelaajat = new List<PlatformCharacter>();
- ExplosionSystem es;
- int kenttaNro; // monesko kenttä on menossa
- #endregion
- #region Alustukset
- protected override void Begin()
- {
- // asetetaan kentän numero nollaksi, jota kasvatetaan kentän latauksessa (aina siis vähintään 1)
- kenttaNro = 0;
- // luodaan pistelaskuri
- pisteLaskuri = new IntMeter( 0 );
- // Zoomataan lähemmäksi
- Camera.ZoomFactor = 2.0;
- Camera.StayInLevel = true;
- //MediaPlayer.Play( "taustamusiikki" );
- MediaPlayer.IsRepeating = true;
- AloitaUusiPeli();
- }
- void AloitaUusiPeli()
- {
- MessageDisplay.Clear();
- kenttaNro = 0;
- seuraavaKentta();
- es = new ExplosionSystem( tahdenKuva, 100 );
- Add( es );
- MessageDisplay.Add( "Etsi iso tähti!" );
- }
- #endregion
- #region KentanLataus
- void seuraavaKentta()
- {
- ClearAll();
- pelaajat.Clear();
- kenttaNro += 1; // lisätään kenttänumeroa yhdellä
- MessageDisplay.Add( "Kenttä " + kenttaNro );
- LataaKentta();
- LisaaNaytot();
- lisaaNappaimet();
- // asetetaan painovoima
- Gravity = new Vector( 0, -500 );
- Camera.Follow( pelaaja1 );
- }
- void LisaaNaytot()
- {
- pisteNaytto = new Label();
- pisteNaytto.X = Screen.RightSafe - 250;
- pisteNaytto.Y = Screen.TopSafe - 50;
- pisteNaytto.BindTo( pisteLaskuri );
- Add( pisteNaytto );
- }
- void LataaKentta()
- {
- Level.Background.CreateGradient( Color.DarkGreen, Color.SkyBlue );
- TileMap ruudut = TileMap.FromFile( "kentta1.txt" );
- ruudut['P'] = LuoPelaaja;
- ruudut['Q'] = LuoPelaaja;
- ruudut['V'] = LuoVihollinen;
- ruudut['*'] = LuoTahti;
- ruudut['#'] = LuoPalikka;
- ruudut['_'] = LuoTaso;
- ruudut['='] = LuoVesi;
- ruudut['M'] = LuoMaali;
- ruudut.Insert( RuudunLeveys, RuudunKorkeus );
- Level.CreateBorders();
- }
- PhysicsObject LuoPelaaja()
- {
- PlatformCharacter pelaaja = new PlatformCharacter( 5, 10 );
- pelaaja.Mass = 10;
- AssaultRifle ase = new AssaultRifle( 4, 1 );
- ase.BulletCollision = AmmusOsuu;
- ase.Power.Value = 1000;
- pelaaja.Weapon = ase;
- Animation kavelyTekstuuri = new Animation( pelaajanKuvat );
- kavelyTekstuuri.FPS = 20;
- pelaaja.RightWalkingAnimation = kavelyTekstuuri;
- pelaaja.LeftWalkingAnimation = Animation.Mirror( kavelyTekstuuri );
- pelaaja.RightIdleAnimation = new Animation( pelaajanKuvat[0] );
- pelaaja.LeftIdleAnimation = new Animation( Image.Mirror( pelaajanKuvat[0] ) );
- AddCollisionHandler( pelaaja, OsuiMaaliin );
- if ( pelaajat.Count == 0 )
- {
- pelaaja1 = pelaaja;
- }
- else
- {
- pelaaja2 = pelaaja;
- }
- pelaajat.Add( pelaaja );
- return pelaaja;
- }
- void AmmusOsuu( PhysicsObject ammus, PhysicsObject toinen )
- {
- //ammus.Destroy();
- if ( toinen.Tag.ToString() == "vihu" )
- {
- toinen.Destroy();
- }
- }
- PhysicsObject LuoMaali()
- {
- PhysicsObject maali = PhysicsObject.CreateStaticObject( RuudunLeveys, RuudunKorkeus, Shapes.Circle );
- maali.Tag = "maali";
- maali.IgnoresCollisionResponse = true;
- maali.Image = tahdenKuva;
- return maali;
- }
- PhysicsObject LuoPalikka()
- {
- PhysicsObject taso = PhysicsObject.CreateStaticObject( RuudunLeveys, RuudunKorkeus, Shapes.Rectangle );
- taso.Image = palikanKuva;
- return taso;
- }
- PhysicsObject LuoVesi()
- {
- PhysicsObject vesi = PhysicsObject.CreateStaticObject( RuudunLeveys, RuudunKorkeus );
- vesi.Animation = new Animation( vesiKuvat );
- vesi.Animation.FPS = 2;
- vesi.Animation.Start();
- AddCollisionHandler( vesi, Tuhoa );
- return vesi;
- }
- PhysicsObject LuoTaso()
- {
- PhysicsObject taso = PhysicsObject.CreateStaticObject( RuudunLeveys, RuudunKorkeus / 4, Shapes.Rectangle, CollisionShapeQuality.Good );
- taso.Color = Color.Green;
- return taso;
- }
- PhysicsObject LuoTahti()
- {
- PhysicsObject tahti = new PhysicsObject( 5, 5 );
- tahti.Mass = 0.005;
- tahti.Shape = Shapes.Circle;
- tahti.Restitution = 1.0;
- tahti.Image = tahdenKuva;
- AddCollisionHandler( tahti, Keraa );
- return tahti;
- }
- PhysicsObject LuoVihollinen()
- {
- PlatformCharacter vihollinen = new PlatformCharacter( 10, 10, Shapes.Circle );
- vihollinen.Tag = "vihu";
- vihollinen.Mass = 100;
- vihollinen.Animation = new Animation( vihollisenKuvat );
- vihollinen.Animation.Start();
- vihollinen.Brain = new RandomMoverBrain();
- vihollinen.IgnoresGravity = true;
- AddCollisionHandler( vihollinen, Tuuppaa );
- return vihollinen;
- }
- #endregion
- #region Nappaimet
- void lisaaNappaimet()
- {
- Keyboard.Listen( Key.Escape, ButtonState.Pressed, Exit, "Poistu" );
- Keyboard.Listen( Key.Enter, ButtonState.Pressed, AloitaUusiPeli, "Aloittaa uuden pelin" );
- Keyboard.Listen( Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet" );
- Keyboard.Listen( Key.Left, ButtonState.Down, Liikuta, null, pelaaja1, -nopeus );
- Keyboard.Listen( Key.Right, ButtonState.Down, Liikuta, null, pelaaja1, nopeus );
- Keyboard.Listen( Key.Left, ButtonState.Released, pelaaja1.StopHorizontal, null );
- Keyboard.Listen( Key.Right, ButtonState.Released, pelaaja1.StopHorizontal, null );
- Keyboard.Listen( Key.Up, ButtonState.Pressed, Hyppaa, "Hyppää", pelaaja1, hyppyVoima );
- Keyboard.Listen( Key.Space, ButtonState.Down, pelaaja1.Weapon.Use, "Ammu" );
- Keyboard.AddHelpText<PlatformCharacter, double>( Liikuta, "Liiku" );
- if ( pelaaja2 != null )
- {
- lisaaGamePadNappaimet( pelaaja2 );
- }
- }
- void lisaaGamePadNappaimet( PlatformCharacter pelaaja )
- {
- ControllerOne.Listen( Button.Back, ButtonState.Pressed, Exit, "Poistu" );
- ControllerOne.Listen( Button.Start, ButtonState.Pressed, AloitaUusiPeli, "Aloittaa uuden pelin" );
- ControllerOne.Listen( Button.DPadLeft, ButtonState.Down, Liikuta, null, pelaaja, -nopeus );
- ControllerOne.Listen( Button.DPadRight, ButtonState.Down, Liikuta, null, pelaaja, nopeus );
- ControllerOne.Listen( Button.DPadLeft, ButtonState.Released, pelaaja.StopHorizontal, null );
- ControllerOne.Listen( Button.DPadRight, ButtonState.Released, pelaaja.StopHorizontal, null );
- ControllerOne.Listen( Button.A, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", pelaaja, hyppyVoima );
- ControllerOne.Listen( Button.B, ButtonState.Down, pelaaja.Weapon.Use, "Ammu" );
- ControllerOne.AddHelpText<PlatformCharacter, double>( Liikuta, "Liiku" );
- }
- void Liikuta( PlatformCharacter hahmo, double nopeus )
- {
- hahmo.Walk( nopeus );
- }
- void Hyppaa( PlatformCharacter hahmo, double voima )
- {
- hahmo.Jump( voima );
- }
- #endregion
- #region Tapahtumat
- void OsuiMaaliin( PhysicsObject collidingObject, PhysicsObject otherObject )
- {
- if ( otherObject.Tag.ToString() == "maali" )
- {
- this.PlaySound( "maali" );
- int edellisenKentanPisteet = pisteLaskuri.Value;
- seuraavaKentta();
- MessageDisplay.Add( "Pääsit läpi kentän " + kenttaNro + ". Pisteitä: " + edellisenKentanPisteet );
- }
- }
- // Kerää tähden ja antaa pisteen
- void Keraa( PhysicsObject collidingObject, PhysicsObject otherObject )
- {
- if ( otherObject == pelaaja1 || otherObject == pelaaja2 )
- {
- es.AddEffect( collidingObject.X, collidingObject.Y, 50 );
- PhysicsObject tahti = collidingObject;
- tahti.Destroy();
- this.PlaySound( "kerays" );
- pisteLaskuri.Value += 1;
- }
- }
- void Tuhoa( PhysicsObject vesi, PhysicsObject toinen )
- {
- if ( toinen.Mass < 10000 )
- {
- toinen.Destroy();
- }
- }
- void Tuuppaa( PhysicsObject collidingObject, PhysicsObject otherObject )
- {
- if ( otherObject is PlatformCharacter )
- {
- double luku1 = RandomGen.NextIntRange( -1000, -800, 800, 1000 );
- double luku2 = RandomGen.NextIntRange( -1000, -800, 800, 1000 );
- if ( otherObject == pelaaja2 )
- {
- // pieni värinä törmäyksestä
- ControllerOne.Vibrate( 0.5, 0.5, 0.0, 0.0, 0.1 );
- }
- this.PlaySound( "tuuppaus" );
- otherObject.Hit( new Vector( luku1, luku2 ) );
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement