Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Jypeli;
  4. using Jypeli.Widgets;
  5. using Jypeli.Assets;
  6. using Jypeli.Effects;
  7.  
  8.  
  9. class Tasohyppely : PhysicsGame
  10. {
  11.     #region Muuttujat
  12.  
  13.     const double nopeus = 60;
  14.     const double hyppyVoima = 2500;
  15.  
  16.     const double RuudunLeveys = 20;
  17.     const double RuudunKorkeus = 20;
  18.  
  19.     Image[] vihollisenKuvat = LoadImages(
  20.             "pallo1", "pallo2", "pallo3", "pallo4", "pallo5", "pallo6", "pallo7", "pallo8",
  21.             "pallo7", "pallo6", "pallo5", "pallo4", "pallo3", "pallo2", "pallo1" );
  22.     Image[] pelaajanKuvat = LoadImages( "heppu_1", "heppu_2", "heppu_3", "heppu_4", "heppu_5" );
  23.     Image[] vesiKuvat = LoadImages( "vesi1", "vesi2", "vesi3" );
  24.     Image palikanKuva = LoadImage( "palikka" );
  25.     Image tahdenKuva = LoadImage( "tahti" );
  26.  
  27.     IntMeter pisteLaskuri;
  28.     Label pisteNaytto;
  29.  
  30.     PlatformCharacter pelaaja1;
  31.     PlatformCharacter pelaaja2;
  32.     List<PlatformCharacter> pelaajat = new List<PlatformCharacter>();
  33.  
  34.     ExplosionSystem es;
  35.  
  36.     int kenttaNro; // monesko kenttä on menossa
  37.  
  38.     #endregion
  39.  
  40.  
  41.     #region Alustukset
  42.     protected override void Begin()
  43.     {
  44.         // asetetaan kentän numero nollaksi, jota kasvatetaan kentän latauksessa (aina siis vähintään 1)
  45.         kenttaNro = 0;
  46.  
  47.         // luodaan pistelaskuri
  48.         pisteLaskuri = new IntMeter( 0 );
  49.  
  50.         // Zoomataan lähemmäksi
  51.         Camera.ZoomFactor = 2.0;
  52.  
  53.         Camera.StayInLevel = true;
  54.  
  55.         //MediaPlayer.Play( "taustamusiikki" );
  56.         MediaPlayer.IsRepeating = true;
  57.  
  58.         AloitaUusiPeli();
  59.     }
  60.  
  61.     void AloitaUusiPeli()
  62.     {
  63.         MessageDisplay.Clear();
  64.         kenttaNro = 0;
  65.         seuraavaKentta();
  66.         es = new ExplosionSystem( tahdenKuva, 100 );
  67.         Add( es );
  68.  
  69.         MessageDisplay.Add( "Etsi iso tähti!" );
  70.     }
  71.  
  72.     #endregion
  73.  
  74.  
  75.     #region KentanLataus
  76.     void seuraavaKentta()
  77.     {
  78.         ClearAll();
  79.  
  80.         pelaajat.Clear();
  81.  
  82.         kenttaNro += 1; // lisätään kenttänumeroa yhdellä
  83.         MessageDisplay.Add( "Kenttä " + kenttaNro );
  84.  
  85.         LataaKentta();
  86.         LisaaNaytot();
  87.         lisaaNappaimet();
  88.  
  89.         // asetetaan painovoima
  90.         Gravity = new Vector( 0, -500 );
  91.  
  92.         Camera.Follow( pelaaja1 );
  93.     }
  94.  
  95.     void LisaaNaytot()
  96.     {
  97.         pisteNaytto = new Label();
  98.         pisteNaytto.X = Screen.RightSafe - 250;
  99.         pisteNaytto.Y = Screen.TopSafe - 50;
  100.         pisteNaytto.BindTo( pisteLaskuri );
  101.         Add( pisteNaytto );
  102.     }
  103.  
  104.     void LataaKentta()
  105.     {
  106.         Level.Background.CreateGradient( Color.DarkGreen, Color.SkyBlue );
  107.  
  108.         TileMap ruudut = TileMap.FromFile( "kentta1.txt" );
  109.         ruudut['P'] = LuoPelaaja;
  110.         ruudut['Q'] = LuoPelaaja;
  111.         ruudut['V'] = LuoVihollinen;
  112.         ruudut['*'] = LuoTahti;
  113.         ruudut['#'] = LuoPalikka;
  114.         ruudut['_'] = LuoTaso;
  115.         ruudut['='] = LuoVesi;
  116.         ruudut['M'] = LuoMaali;
  117.         ruudut.Insert( RuudunLeveys, RuudunKorkeus );
  118.  
  119.         Level.CreateBorders();
  120.     }
  121.  
  122.     PhysicsObject LuoPelaaja()
  123.     {
  124.         PlatformCharacter pelaaja = new PlatformCharacter( 5, 10 );
  125.         pelaaja.Mass = 10;
  126.  
  127.         AssaultRifle ase = new AssaultRifle( 4, 1 );
  128.         ase.BulletCollision = AmmusOsuu;
  129.         ase.Power.Value = 1000;
  130.         pelaaja.Weapon = ase;
  131.  
  132.         Animation kavelyTekstuuri = new Animation( pelaajanKuvat );
  133.         kavelyTekstuuri.FPS = 20;
  134.  
  135.         pelaaja.RightWalkingAnimation = kavelyTekstuuri;
  136.         pelaaja.LeftWalkingAnimation = Animation.Mirror( kavelyTekstuuri );
  137.         pelaaja.RightIdleAnimation = new Animation( pelaajanKuvat[0] );
  138.         pelaaja.LeftIdleAnimation = new Animation( Image.Mirror( pelaajanKuvat[0] ) );
  139.  
  140.         AddCollisionHandler( pelaaja, OsuiMaaliin );
  141.  
  142.         if ( pelaajat.Count == 0 )
  143.         {
  144.             pelaaja1 = pelaaja;
  145.         }
  146.         else
  147.         {
  148.             pelaaja2 = pelaaja;
  149.         }
  150.         pelaajat.Add( pelaaja );
  151.         return pelaaja;
  152.     }
  153.  
  154.     void AmmusOsuu( PhysicsObject ammus, PhysicsObject toinen )
  155.     {
  156.         //ammus.Destroy();
  157.         if ( toinen.Tag.ToString() == "vihu" )
  158.         {
  159.             toinen.Destroy();
  160.         }
  161.     }
  162.  
  163.     PhysicsObject LuoMaali()
  164.     {
  165.         PhysicsObject maali = PhysicsObject.CreateStaticObject( RuudunLeveys, RuudunKorkeus, Shapes.Circle );
  166.         maali.Tag = "maali";
  167.         maali.IgnoresCollisionResponse = true;
  168.         maali.Image = tahdenKuva;
  169.         return maali;
  170.     }
  171.  
  172.     PhysicsObject LuoPalikka()
  173.     {
  174.         PhysicsObject taso = PhysicsObject.CreateStaticObject( RuudunLeveys, RuudunKorkeus, Shapes.Rectangle );
  175.         taso.Image = palikanKuva;
  176.         return taso;
  177.     }
  178.  
  179.     PhysicsObject LuoVesi()
  180.     {
  181.         PhysicsObject vesi = PhysicsObject.CreateStaticObject( RuudunLeveys, RuudunKorkeus );
  182.         vesi.Animation = new Animation( vesiKuvat );
  183.         vesi.Animation.FPS = 2;
  184.         vesi.Animation.Start();
  185.         AddCollisionHandler( vesi, Tuhoa );
  186.         return vesi;
  187.     }
  188.  
  189.     PhysicsObject LuoTaso()
  190.     {
  191.         PhysicsObject taso = PhysicsObject.CreateStaticObject( RuudunLeveys, RuudunKorkeus / 4, Shapes.Rectangle, CollisionShapeQuality.Good );
  192.         taso.Color = Color.Green;
  193.         return taso;
  194.     }
  195.  
  196.     PhysicsObject LuoTahti()
  197.     {
  198.         PhysicsObject tahti = new PhysicsObject( 5, 5 );
  199.         tahti.Mass = 0.005;
  200.         tahti.Shape = Shapes.Circle;
  201.         tahti.Restitution = 1.0;
  202.         tahti.Image = tahdenKuva;
  203.         AddCollisionHandler( tahti, Keraa );
  204.         return tahti;
  205.     }
  206.  
  207.     PhysicsObject LuoVihollinen()
  208.     {
  209.         PlatformCharacter vihollinen = new PlatformCharacter( 10, 10, Shapes.Circle );
  210.         vihollinen.Tag = "vihu";
  211.         vihollinen.Mass = 100;
  212.         vihollinen.Animation = new Animation( vihollisenKuvat );
  213.         vihollinen.Animation.Start();
  214.         vihollinen.Brain = new RandomMoverBrain();
  215.         vihollinen.IgnoresGravity = true;
  216.         AddCollisionHandler( vihollinen, Tuuppaa );
  217.         return vihollinen;
  218.     }
  219.  
  220.     #endregion
  221.  
  222.  
  223.     #region Nappaimet
  224.  
  225.     void lisaaNappaimet()
  226.     {
  227.         Keyboard.Listen( Key.Escape, ButtonState.Pressed, Exit, "Poistu" );
  228.         Keyboard.Listen( Key.Enter, ButtonState.Pressed, AloitaUusiPeli, "Aloittaa uuden pelin" );
  229.         Keyboard.Listen( Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet" );
  230.         Keyboard.Listen( Key.Left, ButtonState.Down, Liikuta, null, pelaaja1, -nopeus );
  231.         Keyboard.Listen( Key.Right, ButtonState.Down, Liikuta, null, pelaaja1, nopeus );
  232.         Keyboard.Listen( Key.Left, ButtonState.Released, pelaaja1.StopHorizontal, null );
  233.         Keyboard.Listen( Key.Right, ButtonState.Released, pelaaja1.StopHorizontal, null );
  234.         Keyboard.Listen( Key.Up, ButtonState.Pressed, Hyppaa, "Hyppää", pelaaja1, hyppyVoima );
  235.         Keyboard.Listen( Key.Space, ButtonState.Down, pelaaja1.Weapon.Use, "Ammu" );
  236.  
  237.         Keyboard.AddHelpText<PlatformCharacter, double>( Liikuta, "Liiku" );
  238.  
  239.         if ( pelaaja2 != null )
  240.         {
  241.             lisaaGamePadNappaimet( pelaaja2 );
  242.         }
  243.     }
  244.  
  245.     void lisaaGamePadNappaimet( PlatformCharacter pelaaja )
  246.     {
  247.         ControllerOne.Listen( Button.Back, ButtonState.Pressed, Exit, "Poistu" );
  248.         ControllerOne.Listen( Button.Start, ButtonState.Pressed, AloitaUusiPeli, "Aloittaa uuden pelin" );
  249.         ControllerOne.Listen( Button.DPadLeft, ButtonState.Down, Liikuta, null, pelaaja, -nopeus );
  250.         ControllerOne.Listen( Button.DPadRight, ButtonState.Down, Liikuta, null, pelaaja, nopeus );
  251.         ControllerOne.Listen( Button.DPadLeft, ButtonState.Released, pelaaja.StopHorizontal, null );
  252.         ControllerOne.Listen( Button.DPadRight, ButtonState.Released, pelaaja.StopHorizontal, null );
  253.         ControllerOne.Listen( Button.A, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", pelaaja, hyppyVoima );
  254.         ControllerOne.Listen( Button.B, ButtonState.Down, pelaaja.Weapon.Use, "Ammu" );
  255.  
  256.         ControllerOne.AddHelpText<PlatformCharacter, double>( Liikuta, "Liiku" );
  257.     }
  258.  
  259.     void Liikuta( PlatformCharacter hahmo, double nopeus )
  260.     {
  261.         hahmo.Walk( nopeus );
  262.     }
  263.  
  264.     void Hyppaa( PlatformCharacter hahmo, double voima )
  265.     {
  266.         hahmo.Jump( voima );
  267.     }
  268.  
  269.     #endregion
  270.  
  271.  
  272.     #region Tapahtumat
  273.  
  274.     void OsuiMaaliin( PhysicsObject collidingObject, PhysicsObject otherObject )
  275.     {
  276.         if ( otherObject.Tag.ToString() == "maali" )
  277.         {
  278.             this.PlaySound( "maali" );
  279.             int edellisenKentanPisteet = pisteLaskuri.Value;
  280.             seuraavaKentta();
  281.             MessageDisplay.Add( "Pääsit läpi kentän " + kenttaNro + ". Pisteitä: " + edellisenKentanPisteet );
  282.         }
  283.     }
  284.  
  285.     // Kerää tähden ja antaa pisteen
  286.     void Keraa( PhysicsObject collidingObject, PhysicsObject otherObject )
  287.     {
  288.         if ( otherObject == pelaaja1 || otherObject == pelaaja2 )
  289.         {
  290.             es.AddEffect( collidingObject.X, collidingObject.Y, 50 );
  291.             PhysicsObject tahti = collidingObject;
  292.             tahti.Destroy();
  293.  
  294.             this.PlaySound( "kerays" );
  295.             pisteLaskuri.Value += 1;
  296.         }
  297.     }
  298.  
  299.     void Tuhoa( PhysicsObject vesi, PhysicsObject toinen )
  300.     {
  301.         if ( toinen.Mass < 10000 )
  302.         {
  303.             toinen.Destroy();
  304.         }
  305.     }
  306.  
  307.     void Tuuppaa( PhysicsObject collidingObject, PhysicsObject otherObject )
  308.     {
  309.         if ( otherObject is PlatformCharacter )
  310.         {
  311.             double luku1 = RandomGen.NextIntRange( -1000, -800, 800, 1000 );
  312.             double luku2 = RandomGen.NextIntRange( -1000, -800, 800, 1000 );
  313.  
  314.             if ( otherObject == pelaaja2 )
  315.             {
  316.                 // pieni värinä törmäyksestä
  317.                 ControllerOne.Vibrate( 0.5, 0.5, 0.0, 0.0, 0.1 );
  318.             }
  319.             this.PlaySound( "tuuppaus" );
  320.             otherObject.Hit( new Vector( luku1, luku2 ) );
  321.         }
  322.     }
  323.     #endregion
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement