Advertisement
Guest User

tasohyppy

a guest
Jun 26th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Jypeli;
  4. using Jypeli.Assets;
  5. using Jypeli.Controls;
  6. using Jypeli.Widgets;
  7.  
  8. public class Tasohyppelipeli1 : PhysicsGame
  9. {
  10.     IntMeter keratytTahdet;
  11.     IntMeter tahtienLkm;
  12.  
  13.     const double Nopeus = 200;
  14.     const double HyppyNopeus = 750;
  15.     const int RUUDUN_KOKO = 40;
  16.  
  17.     PlatformCharacter pelaaja1;
  18.     PlatformCharacter vihu;
  19.  
  20.     Image pelaajanKuva = LoadImage("norsu");
  21.     Image tahtiKuva = LoadImage("tahti");
  22.  
  23.     SoundEffect maaliAani = LoadSoundEffect("maali");
  24.  
  25.     public override void Begin()
  26.     {
  27.         AloitaPeli();
  28.     }
  29.  
  30.  
  31.     void AloitaPeli()
  32.     {
  33.         ClearAll();
  34.         Gravity = new Vector(0, -1000);
  35.  
  36.  
  37.         LuoPistelaskurit();
  38.         LuoKentta();
  39.         LisaaNappaimet();
  40.  
  41.         Camera.Follow(pelaaja1);
  42.         Camera.ZoomFactor = 1.2;
  43.         Camera.StayInLevel = true;
  44.     }
  45.  
  46.     void LuoPistelaskurit()
  47.     {
  48.         tahtienLkm = new IntMeter(0);
  49.         keratytTahdet = new IntMeter(0);
  50.  
  51.         Label tahtiLaskuri = new Label();
  52.         tahtiLaskuri.X = Level.Right - 20;
  53.         tahtiLaskuri.Y = Level.Top - 20;
  54.     }
  55.  
  56.     void LuoKentta()
  57.     {
  58.         TileMap kentta = TileMap.FromLevelAsset("kentta1");
  59.         kentta.SetTileMethod('#', LisaaTaso);
  60.         kentta.SetTileMethod('*', LisaaTahti);
  61.         kentta.SetTileMethod('N', LisaaPelaaja);
  62.         kentta.SetTileMethod('-', LisaaVihu);
  63.  
  64.         kentta.Execute(RUUDUN_KOKO, RUUDUN_KOKO);
  65.         Level.CreateBorders();
  66.         Level.Background.CreateGradient(Color.White, Color.SkyBlue);
  67.     }
  68.  
  69.     void LisaaVihu(Vector paikka, double leveys, double korkeus)
  70.     {
  71.         vihu = new PlatformCharacter(leveys, korkeus);
  72.         vihu.Shape = Shape.Circle;
  73.         vihu.Position = paikka;
  74.         vihu.Mass = 200.0;
  75.         vihu.Tag = "vihu";
  76.  
  77.         PlatformWandererBrain aivot = new PlatformWandererBrain();
  78.         aivot.Speed = 100;
  79.         vihu.Brain = aivot;
  80.  
  81.         Add(vihu);
  82.  
  83.  
  84.     }
  85.  
  86.  
  87.     void LisaaTaso(Vector paikka, double leveys, double korkeus)
  88.     {
  89.         PhysicsObject taso = PhysicsObject.CreateStaticObject(leveys, korkeus);
  90.         taso.Position = paikka;
  91.         taso.Color = Color.Green;
  92.         Add(taso);
  93.     }
  94.  
  95.     void LisaaTahti(Vector paikka, double leveys, double korkeus)
  96.     {
  97.         tahtienLkm.AddValue(1);
  98.  
  99.         PhysicsObject tahti = PhysicsObject.CreateStaticObject(leveys, korkeus);
  100.         tahti.IgnoresCollisionResponse = true;
  101.         tahti.Position = paikka;
  102.         tahti.Image = tahtiKuva;
  103.         tahti.Tag = "tahti";
  104.         Add(tahti);
  105.     }
  106.  
  107.     void LisaaPelaaja(Vector paikka, double leveys, double korkeus)
  108.     {
  109.         pelaaja1 = new PlatformCharacter(leveys, korkeus);
  110.         pelaaja1.Position = paikka;
  111.         pelaaja1.Mass = 4.0;
  112.         pelaaja1.Image = pelaajanKuva;
  113.         pelaaja1.Tag = "pelaaja";
  114.         AddCollisionHandler(pelaaja1, "tahti", TormaaTahteen);
  115.         AddCollisionHandler(pelaaja1, "vihu", TormaaVihuun);
  116.         Add(pelaaja1);
  117.     }
  118.  
  119.     void TormaaVihuun(PhysicsObject tormaaja, PhysicsObject kohde)
  120.     {
  121.         AloitaPeli();
  122.     }
  123.  
  124.     void LisaaNappaimet()
  125.     {
  126.         Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
  127.         Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
  128.  
  129.         Keyboard.Listen(Key.Left, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", pelaaja1, -Nopeus);
  130.         Keyboard.Listen(Key.Right, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", pelaaja1, Nopeus);
  131.         Keyboard.Listen(Key.Up, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", pelaaja1, HyppyNopeus);
  132.  
  133.         ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä");
  134.  
  135.         ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, Liikuta, "Pelaaja liikkuu vasemmalle", pelaaja1, -Nopeus);
  136.         ControllerOne.Listen(Button.DPadRight, ButtonState.Down, Liikuta, "Pelaaja liikkuu oikealle", pelaaja1, Nopeus);
  137.         ControllerOne.Listen(Button.A, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", pelaaja1, HyppyNopeus);
  138.  
  139.         PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
  140.     }
  141.  
  142.     void Liikuta(PlatformCharacter hahmo, double nopeus)
  143.     {
  144.         hahmo.Walk(nopeus);
  145.     }
  146.  
  147.     void Hyppaa(PlatformCharacter hahmo, double nopeus)
  148.     {
  149.         hahmo.Jump(nopeus);
  150.     }
  151.  
  152.    
  153.     void TormaaTahteen(PhysicsObject hahmo, PhysicsObject tahti)
  154.     {
  155.         maaliAani.Play();
  156.         MessageDisplay.Add("Keräsit tähden!");
  157.         tahti.Destroy();
  158.  
  159.         keratytTahdet.AddValue(1); //seuraava koodinpätkä tarkistaa onko tähtiä kerätty tarpeeksi
  160.  
  161.         if (keratytTahdet.Value == tahtienLkm.Value)
  162.         {
  163.             AloitaPeli();          
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement