Advertisement
khanhdu

Game Bắn Súng

Jun 13th, 2017
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 25.57 KB | None | 0 0
  1. #------------------------------------------------------------------------------#
  2. #  Galv's Invader Mini Game
  3. #------------------------------------------------------------------------------#
  4. #  For: RPGMAKER VX ACE
  5. #  Version 1.1
  6. #  Thanks Ocedic for add-on ideas
  7. #------------------------------------------------------------------------------#
  8. #  2013-03-20 - Version 1.1 - added enemy types, new gun type, new powerup
  9. #  2013-03-18 - Version 1.0 - release
  10. #------------------------------------------------------------------------------#
  11. #  A simple shooter mini game that can be played within Ace with a script call.
  12. #
  13. #  The game is designed to be simple. Enemies are random, they shoot random
  14. #  lazors at you. Powerups are random. Slowly during the course of the game
  15. #  the enemies spawn faster and faster until eventually the player will be
  16. #  overrun completely and die. A set time through the game, a reset powerup will
  17. #  spawn. If the player manages to get it, they get another chance at more
  18. #  points as it sets the spawn speed back to the start.
  19. #  Variables are used for current score and high score which you can use in
  20. #  your game to give your player bonuses for doing well or not.
  21. #  
  22. #  You'll need the demo for all the graphics (but feel free to create your own)
  23. #
  24. #  NOTE: This is intended to be simple for now. Please don't ask for add-ons
  25. #  or improvements. I might improve it later but for now, it is what it is.
  26. #------------------------------------------------------------------------------#
  27.  
  28. #------------------------------------------------------------------------------#
  29. #  CONTROLS
  30. #------------------------------------------------------------------------------#
  31. #  Left and Right - moves left and right
  32. #  Space/Z/Enter  - fire your lazor
  33. #  ESC/X          - Leave the mini game
  34. #  A              - Use held item (currently only nuke)
  35. #------------------------------------------------------------------------------#
  36. #  POWERUPS
  37. #------------------------------------------------------------------------------#
  38. #  Heath  - restores shields completely. If shields full, adds to an bonus bar
  39. #  Zzzz   - resets enemy spawn rate to beginning (spawned only at a set time)
  40. #  Nuke   - Can only hold one nuke at a time. Press A to destroy all enemies.
  41. #
  42. #  Lazors - Get lazors or increases number of lazors fired
  43. #  Lazorball - Get lazorballs or increases number of lazorballs fired
  44. #
  45. #  NOTE: Obtaining weapons equips them or improves them if already equipped
  46. #  NOTE2: Yes, I do know it's not spelled "lazor"
  47. #------------------------------------------------------------------------------#
  48.  
  49. #------------------------------------------------------------------------------#
  50. #  SCRIPT CALL:
  51. #------------------------------------------------------------------------------#
  52. #  SceneManager.call(Scene_Invaders)       # Starts the minigame.
  53. #------------------------------------------------------------------------------#
  54.  
  55. ($imported ||= {})["Galv_Invaders"] = true
  56. module Galv_SI
  57.  
  58. #------------------------------------------------------------------------------#
  59. #  SCRIPT SETTINGS
  60. #------------------------------------------------------------------------------#
  61.  
  62.   #----------------#
  63.   # PLAYER OPTIONS #
  64.   #----------------#
  65.  
  66.   PLAYER_SHIELDS = 10  # Hits player can take before game over
  67.  
  68.   MAX_SHOTS = 10    # Maxium number of shots player can have on screen at a time
  69.  
  70.   SE = ["Attack2",80,150]  # "SE_Name",volume,pitch - SE for player laser
  71.   SE1 = ["Heal5",80,160]  # "SE_Name",volume,pitch - SE for player lazorball
  72.   NSE = ["Explosion3",120,100]  # "SE_Name",volume,pitch - SE for nuke
  73.  
  74.   PSE = ["Damage5",90,150]  # "SE_Name",volume,pitch - SE when player damaged
  75.   KSE = ["Explosion7",100,100]  # "SE_Name",volume,pitch - SE when destroyed
  76.  
  77.   BSE = ["Down4",120,150]  # "SE_Name",volume,pitch - SE when bonus shield gain
  78.   PUSE = ["Item1",80,150]  # "SE_Name",volume,pitch - SE when get powerup
  79.  
  80.   SHIP_SPEED = 5        # Speed at which player can move ship left and right
  81.   LAZOR_SPEED = 5       # Speed the lazor fires
  82.  
  83.  
  84.   #---------------#
  85.   # ENEMY OPTIONS #
  86.   #---------------#
  87.  
  88.   SPAWN_SPEED = 100    # Lower means faster spawn. Higher is slower spawn.
  89.  
  90.   LEVEL2 = 30          # Seconds until level 2 ships can start spawning
  91.   LEVEL3 = 100         # Seconds until level 3 ships can start spawning
  92.  
  93.   LEVEL1_HP = 1        # Level 1 enemies have this much hp
  94.   LEVEL2_HP = 4        # Level 2 enemies have this much hp
  95.   LEVEL3_HP = 12       # Level 3 enemies have this much hp
  96.  
  97.   ELAZOR_SPEED = 5     # Enemy lazor speed
  98.  
  99.   ASE = ["Attack2",80,110]  # "SE_Name",volume,pitch - SE for enemy shooting
  100.   DSE = ["Fire3",90,150]  # "SE_Name",volume,pitch - SE for enemy dying
  101.  
  102.  
  103.   #---------------#
  104.   # OTHER OPTIONS #
  105.   #---------------#
  106.  
  107.   SCORE_VAR = 1         # Variable id to keep track of score
  108.   HIGH_SCORE_VAR = 2    # Variable id to keep track of highest score
  109.  
  110.   RESET_PUP = 130       # Seconds between reset powerup spawns. If the
  111.                         # player manages to get this, the enemy spawn rate is
  112.                         # reset and has a chance to earn more points!
  113.  
  114.   SOUND_TIMER = 5       # Prevent enemy lazor sound spam by increasing this
  115.  
  116.   DESTROY_PUPS = false      # Can destroy powerups true or false
  117.  
  118.   LAZOR_DAMAGE = 1      # Damage done when lazors hit
  119.   COLLIDE_DAMAGE = 4    # Damage done when collide with enemy
  120.  
  121.   BGM_LIST = [ # don't touch
  122.   # List of BGM's to random.  ["BGM_Name",volume,pitch]
  123.  
  124.         ["Battle6",100,110],
  125.         ["Battle5",100,110],
  126.        
  127.         # ["Battle5",100,110],   # Add more as required
  128.         # ["Battle5",100,110],   # Add more as required
  129.         # ["Battle5",100,110],   # Add more as required
  130.        
  131.   ] # don't touch
  132.  
  133.  
  134. #------------------------------------------------------------------------------#
  135. #  END OF SCRIPT SETTINGS
  136. #------------------------------------------------------------------------------#
  137. end
  138.  
  139.  
  140. #-------------------------------------------------------------------------------
  141. #  CACHE
  142. #-------------------------------------------------------------------------------
  143.  
  144. module Cache
  145.   def self.space(filename)
  146.     load_bitmap("Graphics/Invaders/", filename)
  147.   end
  148. end # module Cache
  149.  
  150. #-------------------------------------------------------------------------------
  151. #  SCENE
  152. #-------------------------------------------------------------------------------
  153.  
  154. class Scene_Invaders < Scene_Base
  155.   def start
  156.     $game_system.save_bgm
  157.     super
  158.     SceneManager.clear
  159.     Graphics.freeze
  160.     initialize_game
  161.   end
  162.  
  163.   def initialize_game
  164.     play_bgm
  165.     init_variables
  166.     create_backdrop
  167.     create_sprites
  168.     create_stats
  169.   end
  170.  
  171.   def play_bgm
  172.     m = Galv_SI::BGM_LIST[rand(Galv_SI::BGM_LIST.count)]
  173.     RPG::BGM.new(m[0],m[1],m[2]).play
  174.   end
  175.  
  176.   def init_variables
  177.     @nukeall = false
  178.     @sound_timer = Galv_SI::SOUND_TIMER
  179.     @enemy_wave = 1
  180.     @guns = 1
  181.     @gun_type = 0
  182.     @bonus_shields = 0
  183.     @reset_pup = Galv_SI::RESET_PUP * 60
  184.     @player_shields = Galv_SI::PLAYER_SHIELDS
  185.     $game_variables[Galv_SI::SCORE_VAR] = 0
  186.     @plazors = []
  187.     @elazors = []
  188.     @enemies = []
  189.     @explodes = []
  190.     @pups = []
  191.     @spawn_timer = rand(Galv_SI::SPAWN_SPEED)
  192.     @ticker = 100
  193.     @game_time = 0
  194.     @alien_count = 0
  195.     @pups_count = 0
  196.     @difficulty = 0.to_f
  197.     @dead = false
  198.   end
  199.  
  200.   def create_backdrop
  201.     @backdrop = Plane.new
  202.     @backdrop.bitmap = Cache.space("backdrop")
  203.     @backdrop.z = -1
  204.     @flash = Sprite.new
  205.     @flash.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  206.     @flash.bitmap.fill_rect(@flash.bitmap.rect,Color.new(255,255,255))
  207.     @flash.z = 2000
  208.     @flash.opacity = 0
  209.   end
  210.  
  211.   def create_sprites
  212.     @player = Sprite_Player.new(@viewport1)
  213.     @item_held = Sprite.new
  214.     @item_held.x = Graphics.width / 4 + 40
  215.     @item_held.y = 15
  216.     @item_held.z = 100
  217.   end
  218.  
  219.   def draw_item_held
  220.     @item_held.bitmap.dispose if @item_held.bitmap
  221.     @item_held.opacity = 255
  222.     return @item_held.opacity = 0 if @item.nil?
  223.     @item_held.bitmap = Cache.space("item_" + @item.to_s)
  224.   end
  225.  
  226.   def create_stats
  227.     @score_window = Window_InvaderScore.new
  228.     @score_window
  229.   end
  230.  
  231.   def play_time
  232.     @game_time / 60
  233.   end
  234.  
  235.   def update
  236.     update_flash
  237.     @game_time += 1
  238.     @reset_pup -= 1
  239.     super
  240.     update_backdrop
  241.     update_player
  242.     update_splosions
  243.     update_plazors
  244.     update_elazors
  245.     update_enemies
  246.     update_pups
  247.     if !@dead
  248.       update_spawn
  249.     else
  250.       update_game_over
  251.     end
  252.   end
  253.  
  254.   def update_flash
  255.     @flash.opacity -= 3 if @flash.opacity > 0
  256.   end
  257.  
  258.   def update_backdrop
  259.     @backdrop.oy -= 1
  260.   end
  261.  
  262.   def update_spawn
  263.     if @spawn_timer <= 0
  264.       t = alien_type
  265.       if rand(alien_type).to_i == alien_type
  266.         @enemies << Sprite_Alien.new(@viewport1,@alien_count,t)
  267.         @alien_count += 1
  268.       else
  269.         @enemies << Sprite_Alien.new(@viewport1,@alien_count,0)
  270.         @alien_count += 1
  271.       end
  272.         @spawn_timer = 50 + rand(Galv_SI::SPAWN_SPEED) / 2 - @difficulty
  273.     end
  274.     @ticker -= 1
  275.     if @ticker <= 0
  276.       @difficulty += 1
  277.       @ticker = 100
  278.     end
  279.     @spawn_timer -= 1
  280.   end
  281.  
  282.   def alien_type
  283.     r = rand(play_time)
  284.     if r < Galv_SI::LEVEL2
  285.       return 0
  286.     elsif r < Galv_SI::LEVEL3
  287.       return 1
  288.     else
  289.       return 2
  290.     end
  291.   end
  292.  
  293.   def update_player
  294.     @player.update
  295.     update_player_actions
  296.   end
  297.  
  298.   def init_game_over
  299.     RPG::BGM.fade(10)
  300.     @game_over = Sprite.new
  301.     @game_over.bitmap = Cache.space("game-over")
  302.     @game_over.opacity = 0
  303.     @game_over.z = 500
  304.   end
  305.  
  306.   def update_game_over
  307.     @game_over.opacity += 3
  308.     if @game_over.opacity >= 255 && Input.trigger?(:C)
  309.       dispose_graphics
  310.       initialize_game
  311.     end
  312.   end
  313.  
  314.   def update_player_actions
  315.     if Input.trigger?(:C) && !@dead
  316.       return if Galv_SI::MAX_SHOTS * @guns <= @plazors.count
  317.       player_shoot
  318.     end
  319.     if Input.trigger?(:B)
  320.       SceneManager.goto(Scene_Map)
  321.     end
  322.     if Input.trigger?(:X) && @item && !@dead
  323.       @nukeall = true
  324.       RPG::SE.new(Galv_SI::NSE[0],Galv_SI::NSE[1],Galv_SI::NSE[2]).play
  325.       @difficulty *= 0.75
  326.       @flash.opacity = 225
  327.       @item = nil
  328.       draw_item_held
  329.     end
  330.   end
  331.  
  332.   def player_shoot
  333.     case @gun_type
  334.     when 0  # Normal Lazers
  335.       RPG::SE.new(Galv_SI::SE[0],Galv_SI::SE[1],Galv_SI::SE[2]).play
  336.       case @guns
  337.       when 1
  338.         @plazors << Sprite_Lazor.new(@viewport1,@player.x,@player.y)
  339.       when 2
  340.         2.times { |i|
  341.           @plazors << Sprite_Lazor.new(@viewport1,@player.x - 20 + i * 40,@player.y)
  342.         }
  343.       when 3
  344.         3.times { |i|
  345.           @plazors << Sprite_Lazor.new(@viewport1,@player.x - 20 + i * 20,@player.y)
  346.         }
  347.       when 4
  348.         4.times { |i|
  349.           @plazors << Sprite_Lazor.new(@viewport1,@player.x - 30 + i * 20,@player.y)
  350.         }
  351.       when 5
  352.         5.times { |i|
  353.           @plazors << Sprite_Lazor.new(@viewport1,@player.x - 30 + i * 15,@player.y)
  354.         }
  355.       end
  356.     when 1  # Lazer Ball
  357.       RPG::SE.new(Galv_SI::SE1[0],Galv_SI::SE1[1],Galv_SI::SE1[2]).play
  358.       case @guns
  359.       when 1
  360.         @plazors << Sprite_Lazor.new(@viewport1,@player.x,@player.y,1,0)
  361.       when 2
  362.         2.times { |i|
  363.           @plazors << Sprite_Lazor.new(@viewport1,@player.x,@player.y,1,1 + i)
  364.         }
  365.       when 3,4,5
  366.         @guns.times { |i|
  367.           @plazors << Sprite_Lazor.new(@viewport1,@player.x,@player.y,1,i)
  368.         }
  369.       end
  370.     end
  371.   end
  372.  
  373.  
  374.   def update_plazors
  375.     @plazors.each_with_index { |lazor,i|
  376.       lazor.update
  377.       if lazor.y < -10
  378.         lazor.dispose
  379.         @plazors.delete_at(i)
  380.       end
  381.     }
  382.   end
  383.  
  384.   def update_elazors
  385.     @elazors.each_with_index { |lazor,i|
  386.     next if !lazor
  387.       lazor.update
  388.       if lazor.y > Graphics.height
  389.         lazor.dispose
  390.         @elazors[i] = false
  391.       elsif lazor.y > (Graphics.height - @player.height) && player_hit?(lazor.x,lazor.y)
  392.         damage_player(Galv_SI::LAZOR_DAMAGE)
  393.         lazor.dispose
  394.         @elazors[i] = false
  395.       end
  396.     }
  397.   end
  398.  
  399.   def update_enemies
  400.     @sound_timer += 1
  401.     @enemies.each_with_index { |enemy,i|
  402.       next if enemy.nil?
  403.       enemy.update
  404.       if enemy_hit?(enemy) || @nukeall
  405.         enemy.hp -= @nukeall ? enemy.mhp : 1
  406.         if enemy.hp <= 0
  407.           destroy_enemy(enemy.mhp)
  408.           @explodes << Sprite_Splosion.new(@viewport1,enemy.x,enemy.y)
  409.           enemy.dispose
  410.           @enemies[i] = nil
  411.         else
  412.           RPG::SE.new(Galv_SI::PSE[0],Galv_SI::PSE[1],Galv_SI::PSE[2]).play
  413.           enemy.flash(Color.new(255,155,155),20)
  414.         end
  415.       elsif enemy.y > (Graphics.height - @player.height) && player_hit?(enemy.x,enemy.y)
  416.         destroy_enemy(enemy.mhp)
  417.         @explodes << Sprite_Splosion.new(@viewport1,enemy.x,enemy.y)
  418.         enemy.dispose
  419.         @enemies[i] = nil
  420.         damage_player(Galv_SI::COLLIDE_DAMAGE)
  421.       elsif rand(1000) > (995 - @difficulty)
  422.         if @elazors[i].nil?
  423.           if @sound_timer >= Galv_SI::SOUND_TIMER
  424.             RPG::SE.new(Galv_SI::ASE[0],Galv_SI::ASE[1],Galv_SI::ASE[2]).play
  425.             @sound_timer = 0
  426.           end
  427.           @elazors[i] = Sprite_ELazor.new(@viewport1,enemy.x,enemy.y)
  428.         end
  429.       end
  430.     }
  431.     @nukeall = false
  432.   end
  433.  
  434.   def update_pups
  435.     if @reset_pup <= 0
  436.       @pups << Sprite_Powerup.new(@viewport1,@pups_count,999)
  437.       @reset_pup = (Galv_SI::RESET_PUP + @enemy_wave) * 60
  438.     end
  439.     if rand(1000) > (998) && !@dead
  440.       @pups << Sprite_Powerup.new(@viewport1,@pups_count,rand(4))
  441.       @pups_count += 1
  442.     end
  443.     @pups.each_with_index { |pup,i|
  444.       next if pup.nil?
  445.       pup.update
  446.       if enemy_hit?(pup,false) && Galv_SI::DESTROY_PUPS
  447.         RPG::SE.new(Galv_SI::DSE[0],Galv_SI::DSE[1],Galv_SI::DSE[2]).play
  448.         @explodes << Sprite_Splosion.new(@viewport1,pup.x,pup.y)
  449.         pup.dispose
  450.         @pups[i] = nil
  451.       elsif pup.y > (Graphics.height - @player.height) && player_hit?(pup.x,pup.y)
  452.         do_powerup(pup.type)
  453.         pup.dispose
  454.         @pups[i] = nil
  455.       end
  456.     }
  457.   end
  458.  
  459.   def do_powerup(type)
  460.     @player.flash(Color.new(155,255,155),20)
  461.     case type
  462.     when 0 # Shield Restore
  463.       RPG::SE.new(Galv_SI::BSE[0],Galv_SI::BSE[1],Galv_SI::BSE[2]).play
  464.       if @player_shields == Galv_SI::PLAYER_SHIELDS
  465.         if @bonus_shields < Galv_SI::PLAYER_SHIELDS
  466.           @bonus_shields += 1
  467.         end
  468.       else
  469.         @player_shields = Galv_SI::PLAYER_SHIELDS
  470.       end
  471.       @score_window.refresh(@player_shields.to_f,@bonus_shields)
  472.     when 1 # Gun Type: 0 (Normal Lazor)
  473.       RPG::SE.new(Galv_SI::PUSE[0],Galv_SI::PUSE[1],Galv_SI::PUSE[2]).play
  474.       if @gun_type != 0
  475.         @gun_type = 0
  476.       elsif @guns < 5
  477.         @guns += 1
  478.       end
  479.     when 2 # Gun Type: 1 (Lazor Ball)
  480.       RPG::SE.new(Galv_SI::PUSE[0],Galv_SI::PUSE[1],Galv_SI::PUSE[2]).play
  481.       if @gun_type != 1
  482.         @gun_type = 1
  483.       elsif @guns < 5
  484.         @guns += 1
  485.       end
  486.     when 3
  487.       RPG::SE.new(Galv_SI::PUSE[0],Galv_SI::PUSE[1],Galv_SI::PUSE[2]).play
  488.       @item = "nuke"
  489.       draw_item_held
  490.     when 999 # Reset
  491.       RPG::SE.new(Galv_SI::PUSE[0],Galv_SI::PUSE[1],Galv_SI::PUSE[2]).play
  492.       @difficulty = 0
  493.       @enemy_wave += 1
  494.     end
  495.   end
  496.  
  497.   def damage_player(amount)
  498.     RPG::SE.new(Galv_SI::PSE[0],Galv_SI::PSE[1],Galv_SI::PSE[2]).play
  499.     @player.flash(Color.new(255,155,155),20)
  500.     if @bonus_shields > 0
  501.       @bonus_shields = [@bonus_shields - amount,0].max
  502.     else
  503.       @player_shields -= amount
  504.     end
  505.     @score_window.refresh(@player_shields.to_f,@bonus_shields)
  506.     destroy_player if @player_shields <= 0
  507.   end
  508.  
  509.   def destroy_enemy(score)
  510.     RPG::SE.new(Galv_SI::DSE[0],Galv_SI::DSE[1],Galv_SI::DSE[2]).play
  511.     $game_variables[Galv_SI::SCORE_VAR] += score
  512.     if $game_variables[Galv_SI::SCORE_VAR] > $game_variables[Galv_SI::HIGH_SCORE_VAR]
  513.       $game_variables[Galv_SI::HIGH_SCORE_VAR] += score
  514.     end
  515.     @score_window.refresh(@player_shields.to_f,@bonus_shields)
  516.   end
  517.  
  518.   def destroy_player
  519.     @player_shields = 0
  520.     @explodes << Sprite_Splosion.new(@viewport1,@player.x,@player.y,2)
  521.     @player.opacity = 0
  522.     @player.x = -100
  523.     RPG::SE.new(Galv_SI::KSE[0],Galv_SI::KSE[1],Galv_SI::KSE[2]).play
  524.     @score_window.refresh(@player_shields.to_f,@bonus_shields)
  525.     init_game_over
  526.     @dead = true
  527.   end
  528.  
  529.   def update_splosions
  530.     @explodes.each_with_index { |ex,i|
  531.       ex.update
  532.       if ex.finished?
  533.         ex.dispose
  534.         @explodes.delete_at(i)
  535.       end
  536.     }
  537.   end
  538.  
  539.   def player_hit?(x,y)
  540.      if x.between?(@player.x - player_width / 6, @player.x + player_width / 6) &&
  541.         y.between?(@player.y - player_height, @player.y)
  542.       return true
  543.     end
  544.     return false
  545.   end
  546.  
  547.   def enemy_hit?(enemy, kill = true)
  548.     @plazors.each_with_index { |lazor,i|
  549.       if lazor.x.between?(enemy.x - enemy.width / 2, enemy.x + enemy.width / 2) &&
  550.         lazor.y.between?(enemy.y - enemy.height / 2, enemy.y + enemy.height / 2)
  551.       if kill
  552.         lazor.dispose
  553.         @plazors.delete_at(i)
  554.       end
  555.       return true
  556.       end
  557.     }
  558.     false
  559.   end
  560.  
  561.   def player_width
  562.     @player.bitmap.width
  563.   end
  564.   def player_height
  565.     @player.bitmap.height
  566.   end
  567.  
  568.   def terminate
  569.     super
  570.     SceneManager.snapshot_for_background
  571.     dispose_graphics
  572.     $game_system.replay_bgm
  573.   end
  574.  
  575.   def dispose_graphics
  576.     @item_held.bitmap.dispose if @item_held.bitmap
  577.     @plazors.each { |object| object.dispose if object }
  578.     @elazors.each { |object| object.dispose if object }
  579.     @enemies.each { |object| object.dispose if object }
  580.     @explodes.each { |object| object.dispose if object }
  581.     @pups.each { |object| object.dispose if object }
  582.     @backdrop.bitmap.dispose
  583.     @backdrop.dispose
  584.     @player.bitmap.dispose
  585.     @player.dispose
  586.     if @game_over
  587.       @game_over.bitmap.dispose
  588.       @game_over.dispose
  589.     end
  590.     @score_window.dispose
  591.   end
  592. end # Scene_Invaders < Scene_Base
  593.  
  594. #-------------------------------------------------------------------------------
  595. #  PLAYER SPRITE
  596. #-------------------------------------------------------------------------------
  597.  
  598. class Sprite_Player < Sprite
  599.   def initialize(viewport)
  600.     super(viewport)
  601.     init_position
  602.   end
  603.  
  604.   def init_position
  605.     setup_player_image
  606.   end
  607.  
  608.   def dispose
  609.     super
  610.   end
  611.  
  612.   def update
  613.     super
  614.     update_src_rect
  615.     update_position
  616.   end
  617.  
  618.   def setup_player_image
  619.     @cell = 1
  620.     self.bitmap = Cache.space("player")
  621.     @cw = bitmap.width / 3
  622.     self.src_rect.set(@cell * @cw, 0, @cw, height)
  623.     self.ox = @cw / 2
  624.     self.oy = height
  625.     self.x = Graphics.width / 2
  626.     self.y = Graphics.height - height / 4
  627.   end
  628.  
  629.   def width
  630.     self.bitmap.width / 3
  631.   end
  632.   def height
  633.     self.bitmap.height
  634.   end
  635.  
  636.   def update_src_rect
  637.     @cell = 1 if @cell > 3
  638.     sx = @cell * @cw
  639.     self.src_rect.set(sx, 0, @cw, height)
  640.   end
  641.  
  642.   def update_position
  643.     if Input.press?(:LEFT) && !Input.press?(:RIGHT)
  644.       @cell = 0
  645.       self.x -= Galv_SI::SHIP_SPEED if self.x > width / 2
  646.     elsif Input.press?(:RIGHT) && !Input.press?(:LEFT)
  647.       @cell = 2
  648.       self.x += Galv_SI::SHIP_SPEED if self.x < Graphics.width - width / 2
  649.     else
  650.       @cell = 1
  651.     end
  652.   end
  653. end # Sprite_Player < Sprite
  654.  
  655.  
  656. #-------------------------------------------------------------------------------
  657. #  LAZOR SPRITES
  658. #-------------------------------------------------------------------------------
  659.  
  660. class Sprite_Lazor < Sprite
  661.   def initialize(viewport,x,y,type = 0,dir = 0)
  662.     super(viewport)
  663.     self.x = x
  664.     self.y = y - 20
  665.     @type = type
  666.     @dir = dir
  667.     setup_lazor_image
  668.   end
  669.  
  670.   def dispose
  671.     super
  672.   end
  673.  
  674.   def update
  675.     super
  676.     update_position
  677.   end
  678.  
  679.   def setup_lazor_image
  680.     case @type
  681.     when 0
  682.       self.bitmap = Cache.space("lazor")
  683.     when 1
  684.       self.bitmap = Cache.space("lazor_ball")
  685.     end  
  686.     self.ox = bitmap.width / 2
  687.     self.oy = bitmap.height / 2
  688.   end
  689.  
  690.   def update_position
  691.     self.y -= Galv_SI::LAZOR_SPEED    
  692.     case @dir
  693.     when 1
  694.       self.x -= Galv_SI::LAZOR_SPEED.to_f / 4
  695.     when 2
  696.       self.x += Galv_SI::LAZOR_SPEED.to_f / 4 + 1
  697.     when 3
  698.       self.x -= Galv_SI::LAZOR_SPEED
  699.     when 4
  700.       self.x += Galv_SI::LAZOR_SPEED
  701.     end
  702.   end
  703. end # Sprite_Lazor < Sprite
  704.  
  705.  
  706. class Sprite_ELazor < Sprite
  707.   def initialize(viewport,x,y)
  708.     super(viewport)
  709.     self.x = x
  710.     self.y = y - 20
  711.     setup_lazor_image
  712.   end
  713.  
  714.   def dispose
  715.     super
  716.   end
  717.  
  718.   def update
  719.     super
  720.     update_position
  721.   end
  722.  
  723.   def setup_lazor_image
  724.     self.bitmap = Cache.space("elazor")
  725.     self.ox = bitmap.width / 2
  726.     self.oy = bitmap.height / 2
  727.   end
  728.  
  729.   def update_position
  730.     self.y += Galv_SI::ELAZOR_SPEED
  731.   end
  732. end # Sprite_ELazor < Sprite
  733.  
  734. #-------------------------------------------------------------------------------
  735. #  ALIEN SPRITES
  736. #-------------------------------------------------------------------------------
  737.  
  738. class Sprite_Alien < Sprite
  739.   attr_accessor :hp
  740.   attr_reader :mhp
  741.  
  742.   def initialize(viewport,id,type = 0)
  743.     super(viewport)
  744.     @type = type
  745.     @id = id
  746.     @move = true   # true is right, false is left
  747.     @speed = rand(2) + 1
  748.     @ticker = 0
  749.     setup_enemy
  750.     init_position
  751.   end
  752.  
  753.   def init_position
  754.     self.x = rand(Graphics.width)
  755.     self.y = -10
  756.   end
  757.  
  758.   def dispose
  759.     super
  760.   end
  761.  
  762.   def update
  763.     super
  764.     update_move
  765.   end
  766.  
  767.   def update_move
  768.     case @move
  769.     when true  # Right
  770.       self.x += 1 * (@ticker * 0.06) if self.x <= Graphics.width
  771.     when false # Left
  772.       self.x -= 1 * (@ticker * 0.06) if self.x > 0
  773.     end
  774.     @ticker -= 1
  775.     self.y += @speed
  776.     if @ticker <= 0
  777.       @move = self.x < Graphics.width / 2 ? true : false
  778.       @ticker = rand(90)
  779.     end
  780.   end
  781.  
  782.   def setup_enemy
  783.     self.bitmap = Cache.space("alien" + @type.to_s)
  784.     self.ox = bitmap.width / 2
  785.     self.oy = bitmap.height / 2
  786.    
  787.     case @type
  788.     when 0
  789.       @hp = Galv_SI::LEVEL1_HP
  790.     when 1
  791.       @hp = Galv_SI::LEVEL2_HP
  792.     when 2
  793.       @hp = Galv_SI::LEVEL3_HP
  794.       @speed = 1
  795.     end
  796.       @mhp = @hp
  797.   end
  798.  
  799.   def width
  800.     self.bitmap.width
  801.   end
  802.   def height
  803.     self.bitmap.height
  804.   end
  805. end # Sprite_Alien < Sprite
  806.  
  807. #-------------------------------------------------------------------------------
  808. #  EXPLOSION SPRITES
  809. #-------------------------------------------------------------------------------
  810.  
  811. class Sprite_Splosion < Sprite
  812.   def initialize(viewport,x,y,zoom = 1)
  813.     super(viewport)
  814.     self.x = x
  815.     self.y = y
  816.     @timer = 10
  817.     setup_explosion_image(zoom)
  818.   end
  819.  
  820.   def dispose
  821.     super
  822.   end
  823.  
  824.   def update
  825.     super
  826.     wait_for_timer
  827.   end
  828.  
  829.   def setup_explosion_image(zoom)
  830.     self.bitmap = Cache.space("explode")
  831.     self.ox = bitmap.width / 2
  832.     self.oy = bitmap.height / 2
  833.     self.zoom_x = zoom
  834.     self.zoom_y = zoom
  835.   end
  836.  
  837.   def wait_for_timer
  838.     @finished = true if @timer <= 0
  839.     @timer -= 1
  840.   end
  841.  
  842.   def finished?
  843.     @finished
  844.   end
  845. end # Sprite_Splosion < Sprite
  846.  
  847.  
  848. #-------------------------------------------------------------------------------
  849. #  POWERUP SPRITES
  850. #-------------------------------------------------------------------------------
  851.  
  852. class Sprite_Powerup < Sprite
  853.   attr_reader :type
  854.  
  855.   def initialize(viewport,id,type)
  856.     super(viewport)
  857.     @id = id
  858.     @type = type            # 0 heal   1 lazor  2 lazerball  3 nuke   999 reset
  859.     @speed = rand(1) + 1
  860.     setup_image(type)
  861.     init_position
  862.   end
  863.  
  864.   def init_position
  865.     self.x = rand(Graphics.width)
  866.     self.y = -10
  867.   end
  868.  
  869.   def dispose
  870.     super
  871.   end
  872.  
  873.   def update
  874.     super
  875.     update_move
  876.   end
  877.  
  878.   def update_move
  879.     self.y += @speed
  880.   end
  881.  
  882.   def setup_image(type)
  883.     self.bitmap = Cache.space("powerup" + type.to_s)
  884.     self.ox = bitmap.width / 2
  885.     self.oy = bitmap.height / 2
  886.   end
  887.  
  888.   def width
  889.     self.bitmap.width
  890.   end
  891.   def height
  892.     self.bitmap.height
  893.   end
  894. end # Sprite_Powerup < Sprite
  895.  
  896. #-------------------------------------------------------------------------------
  897. #  SCORE WINDOW
  898. #-------------------------------------------------------------------------------
  899.  
  900. class Window_InvaderScore < Window_Base
  901.   def initialize
  902.     super(0, 0, Graphics.width, Graphics.height)
  903.     self.opacity = 0
  904.     refresh
  905.   end
  906.  
  907.   def refresh(shields = Galv_SI::PLAYER_SHIELDS.to_f,bonus_shields = 0)
  908.     contents.clear
  909.     draw_score("Điểm : ", score, 4, 0, contents.width - 8, 2)
  910.     draw_score("Tốt: ", high_score, -(Graphics.width / 2) + 70, 0, contents.width - 8, 2)
  911.     draw_shields(shields, 4, 0)
  912.     draw_bonus_shields(bonus_shields)
  913.   end
  914.  
  915.   def draw_shields(shields, x, y, width = Graphics.width / 4)
  916.     draw_gauge(x, y, width, shields / Galv_SI::PLAYER_SHIELDS.to_f, text_color(1),
  917.     text_color(4))
  918.   end
  919.  
  920.   def draw_bonus_shields(x,width = Graphics.width / 4)
  921.     w = width * x / Galv_SI::PLAYER_SHIELDS.to_f
  922.     rect = Rect.new(4, 0, w, 12)
  923.     contents.fill_rect(rect, text_color(1))
  924.   end
  925.  
  926.   def score
  927.     $game_variables[Galv_SI::SCORE_VAR]
  928.   end
  929.   def high_score
  930.     $game_variables[Galv_SI::HIGH_SCORE_VAR]
  931.   end
  932.  
  933.   def draw_score(value, unit, x, y, width, align)
  934.     cx = text_size(unit).width
  935.     draw_text(x, y, width - cx - 2, line_height, value, align)
  936.     draw_text(x, y, width, line_height, unit, align)
  937.   end
  938.  
  939.   def open
  940.     refresh
  941.     super
  942.   end
  943. end # Window_InvaderScore < Window_Base
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement