Advertisement
pkteamrocket

BallCatcher

Mar 28th, 2018
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.42 KB | None | 0 0
  1. #===============================================================================
  2. # * Ball Catch Game - by FL (Credits will be apreciated)
  3. #===============================================================================
  4. #
  5. # This script is for Pokémon Essentials. It's a simple minigame where the player
  6. # must pick the balls that are falling at screen.
  7. #
  8. #===============================================================================
  9. #
  10. # To this script works, put it above main, put a 512x384 background for this
  11. # screen in "Graphics/Pictures/backgroundcatch" location, a 80x44
  12. # catcher/basket at "Graphics/Pictures/catcher" and a 20x20 ball at
  13. # "Graphics/Pictures/ballcatch". May works with other image sizes.
  14. #  
  15. # To call this script, use the script command 'pbCatchGame(X)' where X is the
  16. # number of total balls. This method will return the number of picked balls.
  17. #
  18. #===============================================================================
  19.  
  20. pbDirs()
  21. pbDownloadToFile("http://i.imgur.com/YUDYyWG.png","Graphics/Pictures/Catcher.png") unless File.exists?('Graphics/Pictures/Catcher.png')
  22. pbDownloadToFile("http://i.imgur.com/cyupQNH.png","Graphics/Pictures/Backgroundcatch.png") unless File.exists?('Graphics/Pictures/Backgroundcatch.png')
  23. pbDownloadToFile("http://i.imgur.com/Wl7457G.png","Graphics/Pictures/Ballcatch.png") unless File.exists?('Graphics/Pictures/Ballcatch.png')
  24.  
  25.  
  26. class CatchGameScene
  27.   def update
  28.     pbUpdateSpriteHash(@sprites)
  29.   end
  30.  
  31.   def pbStartScene(balls, speed, coins)
  32.     @sprites={}
  33.     @viewport=Viewport.new(0,0,Graphics.width,Graphics.height)
  34.     @viewport.z=99999
  35.     @sprites["background"]=IconSprite.new(0,0,@viewport)
  36.     @sprites["background"].setBitmap("Graphics/Pictures/backgroundcatch")
  37.     @sprites["background"].x=(
  38.         Graphics.width-@sprites["background"].bitmap.width)/2
  39.     @sprites["background"].y=(
  40.         Graphics.height-@sprites["background"].bitmap.height)/2
  41.     @sprites["player"]=IconSprite.new(0,0,@viewport)
  42.     @sprites["player"].setBitmap("Graphics/Pictures/catcher")
  43.     @sprites["overlay"]=BitmapSprite.new(
  44.         Graphics.width,Graphics.height,@viewport)
  45.     pbSetSystemFont(@sprites["overlay"].bitmap)
  46.     @balls=balls
  47.     @speed=speed
  48.     @catchcoins=coins
  49.     initializeBallsPositions
  50.     @frameCount=-1
  51.     @playerPosition=POSITIONS/2
  52.     updatePlayerPosition
  53.     @score=0
  54.     @coins=0
  55.     @vloop=0
  56.     pbDrawText
  57.     pbBGMPlay("021-Field04")
  58.     pbFadeInAndShow(@sprites) { update }
  59.   end
  60.  
  61.   def pbDrawText
  62.     overlay=@sprites["overlay"].bitmap
  63.     overlay.clear
  64.     score=_INTL("Score: {1}/{2} - Earned Coins : ${3}",@score,@balls,@coins)
  65.     baseColor=Color.new(72,72,72)
  66.     shadowColor=Color.new(160,160,160)
  67.     textPositions=[
  68.        [score,8,2,false,baseColor,shadowColor],
  69.     ]
  70.     pbDrawTextPositions(overlay,textPositions)
  71.   end
  72.  
  73.   YSTART=-40
  74.   XSTART=56
  75.   XGAIN=64
  76.  
  77.   def updatePlayerPosition
  78.     @sprites["player"].x=(
  79.         XSTART+XGAIN*@playerPosition-@sprites["player"].bitmap.width/2)
  80.     @sprites["player"].y=340-@sprites["player"].bitmap.height/2
  81.   end
  82.  
  83.   def initializeBall(position)
  84.     i=0
  85.     # This method reuse old balls for better performance
  86.     loop do
  87.       if !@sprites["ball#{i}"]
  88.         @sprites["ball#{i}"]=IconSprite.new(0,0,@viewport)
  89.         @sprites["ball#{i}"].setBitmap("Graphics/Pictures/ballcatch")
  90.         @sprites["ball#{i}"].ox=@sprites["ball#{i}"].bitmap.width/2
  91.         @sprites["ball#{i}"].oy=@sprites["ball#{i}"].bitmap.height/2
  92.         break
  93.       end  
  94.       if !@sprites["ball#{i}"].visible
  95.         @sprites["ball#{i}"].visible=true
  96.         break
  97.       end
  98.       i+=1
  99.     end
  100.     @sprites["ball#{i}"].x=XSTART+XGAIN*position
  101.     @sprites["ball#{i}"].y=YSTART
  102.   end  
  103.  
  104.   POSITIONS=8 # The number of positions or columns for the balls/player
  105.   LINEPERBALL=3.0 # The line/ball proportion
  106.   FRAMESPERLINE=12 # The number of frames until the next value of @lineArray
  107.   BALLSPEED=@speed # The speed of the ball in pixels per frame
  108.   MAXDISTANCE=1 # Max distance allowed between balls
  109.    
  110.   def initializeBallsPositions
  111.     lines=(LINEPERBALL*@balls).floor
  112.     @lineArray=[]
  113.     @lineArray[lines-1]=nil # One position for every line
  114.     loop do
  115.       while @lineArray.nitems<@balls
  116.         ballIndex = rand(lines)
  117.         @lineArray[ballIndex] = rand(POSITIONS) if !@lineArray[ballIndex]
  118.       end  
  119.       for i in 0...@lineArray.size
  120.         next if !@lineArray[i]
  121.         # Checks if the ball isn't too distant to pick.
  122.         # If is, remove from the array
  123.         checkRight(i+1,@lineArray[i]+MAXDISTANCE)
  124.         checkLeft(i+1,@lineArray[i]-MAXDISTANCE)
  125.       end
  126.       return if @lineArray.nitems==@balls
  127.     end
  128.   end  
  129.  
  130.   def checkRight(index, position)
  131.     return if (position>=POSITIONS || index>=@lineArray.size)
  132.     if (@lineArray[index] && @lineArray[index]>position)
  133.       @lineArray[index]=nil
  134.     end
  135.     checkRight(index+1,position+MAXDISTANCE)
  136.   end  
  137.  
  138.   def checkLeft(index, position)
  139.     return if (position<=0 || index>=@lineArray.size)
  140.     if (@lineArray[index] && @lineArray[index]<position)
  141.       @lineArray[index]=nil
  142.     end
  143.     checkLeft(index+1,position-MAXDISTANCE)
  144.   end  
  145.  
  146.   def applyCollisions
  147.     i=0
  148.     loop do
  149.       break if !@sprites["ball#{i}"]
  150.       if @sprites["ball#{i}"].visible
  151.         @sprites["ball#{i}"].y+=@speed
  152.         @sprites["ball#{i}"].angle+=10
  153.         ballBottomY = @sprites["ball#{i}"].y+@sprites["ball#{i}"].bitmap.height
  154.        
  155.         # Collision with player
  156.         ballPosition=(@sprites["ball#{i}"].x-XSTART+
  157.             @sprites["ball#{i}"].bitmap.width/2)/XGAIN
  158.         if ballPosition==@playerPosition
  159.           collisionStartY=-8
  160.           collisionEndY=10
  161.           # Based at target center
  162.           playerCenterY=@sprites["player"].y+@sprites["player"].bitmap.width/2
  163.           collisionStartY+=playerCenterY
  164.           collisionEndY+=playerCenterY
  165.           if(collisionStartY < ballBottomY && collisionEndY > ballBottomY)
  166.             # The ball was picked  
  167.             @sprites["ball#{i}"].visible=false
  168.             pbSEPlay('jump',100,100)
  169.             @score+=1
  170.             @vloop+=1
  171.             if @vloop==@catchcoins
  172.             @vloop=0
  173.             @coins+=1          
  174.             pbSEPlay('Voltorb Flip Point',100,100)
  175.             end
  176.             pbDrawText # Update score at screen
  177.           end
  178.         end
  179.        
  180.         # Collision with screen limit
  181.         screenLimit = Graphics.height+@sprites["ball#{i}"].bitmap.height
  182.         if(ballBottomY>screenLimit)
  183.           # The ball was out of screen
  184.           @sprites["ball#{i}"].visible=false
  185.           pbSEPlay("balldrop")
  186.         end
  187.       end  
  188.       i+=1
  189.     end
  190.   end  
  191.  
  192.   def thereBallsInGame?
  193.     i=0
  194.     loop do
  195.       return false if !@sprites["ball#{i}"]
  196.       return true if @sprites["ball#{i}"].visible
  197.       i+=1
  198.     end
  199.   end  
  200.    
  201.   def pbMain
  202.     stopBalls = false
  203.     loop do
  204.       @frameCount+=1
  205.       applyCollisions
  206.       indexNextBall=@frameCount/FRAMESPERLINE
  207.       stopBalls = indexNextBall>=@lineArray.size
  208.       if (@frameCount%FRAMESPERLINE==0 && !stopBalls &&
  209.           @lineArray[indexNextBall])
  210.         initializeBall(@lineArray[indexNextBall])
  211.       end
  212.       Graphics.update
  213.       Input.update
  214.       self.update
  215.       if stopBalls && !thereBallsInGame?
  216.         Kernel.pbMessage(_INTL("Game end!"))
  217.         break
  218.       end  
  219.       if Input.repeat?(Input::LEFT) && @playerPosition>0
  220.         @playerPosition=@playerPosition-1
  221.         updatePlayerPosition
  222.       end
  223.       if Input.repeat?(Input::RIGHT) && @playerPosition<(POSITIONS-1)
  224.         @playerPosition=@playerPosition+1
  225.         updatePlayerPosition
  226.       end
  227.     end
  228.     return @coins
  229.   end
  230.  
  231.   def pbEndScene
  232.     $game_map.autoplay
  233.     pbFadeOutAndHide(@sprites) { update }
  234.     pbDisposeSpriteHash(@sprites)
  235.     @viewport.dispose
  236.   end
  237. end
  238.  
  239. class CatchGame
  240.   def initialize(scene)
  241.     @scene=scene
  242.   end
  243.  
  244.   def pbStartScreen(balls, speed, coins)
  245.     @scene.pbStartScene(balls, speed, coins)
  246.     ret=@scene.pbMain
  247.     @scene.pbEndScene
  248.     return ret
  249.   end
  250. end
  251.  
  252. def pbCatchGame(balls, speed, coins)
  253.     Audio.bgm_play("Audio/BGM/Miror B. Theme.mp3",100,100)
  254.     Kernel.pbMessage(_INTL("\\wm\ The Game will start in 3 ..."))
  255.     Kernel.pbMessage(_INTL("\\wm\ 2 ..."))
  256.     Kernel.pbMessage(_INTL("\\wm\ 1 ..."))
  257.     Kernel.pbMessage(_INTL("\\wm\ GO ..."))
  258.   scene=CatchGameScene.new
  259.   screen=CatchGame.new(scene)
  260.   return screen.pbStartScreen(balls, speed, coins)
  261. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement