RPG-Maker脚本探索:用XP做贪吃蛇

Show A Picture

First of all, let's try to show a circle in the game.

We can learn these code from the Help document.

devil = Sprite.new
devil.bitmap = Bitmap.new("Graphics/Battlers/075-Devil01")
devil.ox = devil.bitmap.width / 2
devil.oy = devil.bitmap.height / 2
devil.x = 320
devil.y = 240

loop do
  Graphics.update
end

This can simply display a picture of the devil.

A Moving Picture

And now, let's try to make this picture move with the time.

devil = Sprite.new
devil.bitmap = Bitmap.new("Graphics/Battlers/075-Devil01")
devil.ox = devil.bitmap.width / 2
devil.oy = devil.bitmap.height / 2
devil.x = 320
devil.y = 240

loop do
  Graphics.update
  devil.x += 1
end

This program can make the picture move on the x axis to the right side.

We should notice that the refresh rate of RPG Maker XP is 20 fps.

Then, we should restrict the devil in the window, or it may move to the outside.

The size of the visiable area is 640*480, and the restraint of x is 640 minus half of the width of the picture.

devil = Sprite.new
devil.bitmap = Bitmap.new("Graphics/Battlers/075-Devil01")
devil.ox = devil.bitmap.width / 2
devil.oy = devil.bitmap.height / 2
devil.x = 320
devil.y = 240

loop do
  Graphics.update
  if devil.x < 640 - devil.bitmap.width/2
    devil.x += 1
  end
end

I found a picture of a circle on the internet.

Then resize the picture and complete the constraints of the other three sides.

$snake = Sprite.new
$snake.bitmap = Bitmap.new("Graphics/Pictures/Circle")
$snake.zoom_x = 0.1
$snake.zoom_y = 0.1
$snake.ox = $snake.bitmap.width / 2
$snake.oy = $snake.bitmap.height / 2
$snake.x = 320
$snake.y = 240
$direction = 1

def bounderyCheck
  if $snake.x >= 640 - $snake.bitmap.width/2 * $snake.zoom_x
    return false
  end
  if $snake.x <= $snake.bitmap.width/2 * $snake.zoom_x
    return false
  end
  if $snake.y >= 480 - $snake.bitmap.height/2 * $snake.zoom_y
    return false
  end
  if $snake.y <= $snake.bitmap.height/2 * $snake.zoom_y
    return false
  end
  return true
end

def move
  if $direction == 0
    return
  end
  if $direction == 1
    $snake.x += 1
    if !bounderyCheck
      $snake.x -= 1
    end
  end
  if $direction == -1
    $snake.x -= 1
    if !bounderyCheck
      $snake.x += 1
    end
  end
  if $direction == 2
    $snake.y += 1
    if !bounderyCheck
      $snake.y -= 1
    end
  end
  if $direction == -2
    $snake.y -= 1
    if !bounderyCheck
      $snake.y += 1
    end
  end
end

loop do
  Graphics.update
  move
end

Control it!

And now, make the program interactive.

$snake = Sprite.new
$snake.bitmap = Bitmap.new("Graphics/Pictures/Circle")
$snake.zoom_x = 0.1
$snake.zoom_y = 0.1
$snake.ox = $snake.bitmap.width / 2
$snake.oy = $snake.bitmap.height / 2
$snake.x = 320
$snake.y = 240
$direction = 1

def bounderyCheck
  if $snake.x >= 640 - $snake.bitmap.width/2 * $snake.zoom_x
    return false
  end
  if $snake.x <= $snake.bitmap.width/2 * $snake.zoom_x
    return false
  end
  if $snake.y >= 480 - $snake.bitmap.height/2 * $snake.zoom_y
    return false
  end
  if $snake.y <= $snake.bitmap.height/2 * $snake.zoom_y
    return false
  end
  return true
end

def move
  if $direction == 0
    return
  end
  if $direction == 1
    $snake.x += 1
    if !bounderyCheck
      $snake.x -= 1
    end
  end
  if $direction == -1
    $snake.x -= 1
    if !bounderyCheck
      $snake.x += 1
    end
  end
  if $direction == 2
    $snake.y += 1
    if !bounderyCheck
      $snake.y -= 1
    end
  end
  if $direction == -2
    $snake.y -= 1
    if !bounderyCheck
      $snake.y += 1
    end
  end
end

loop do
  Graphics.update
  Input.update
  if Input.trigger?(Input::RIGHT)
    $direction = 1
  end
  if Input.trigger?(Input::LEFT)
    $direction = -1
  end
  if Input.trigger?(Input::DOWN)
    $direction = 2
  end
  if Input.trigger?(Input::UP)
    $direction = -2
  end
  move
end

A snake!

The following code has bought out a contorllable snake.

$snake = Sprite.new
$snake.bitmap = Bitmap.new("Graphics/Pictures/Circle")
$snake.zoom_x = 0.1
$snake.zoom_y = 0.1
$snake.ox = $snake.bitmap.width / 2
$snake.oy = $snake.bitmap.height / 2
$snake.x = 10 * $snake.bitmap.width * $snake.zoom_x
$snake.y = 8 * $snake.bitmap.height * $snake.zoom_y
$snake_body = [$snake]
$snake_length = 5
$step = $snake.bitmap.width * $snake.zoom_x

for i in 1..$snake_length
  $snake_body.push(Sprite.new)
  $snake_body[i].bitmap = Bitmap.new("Graphics/Pictures/Circle")
  $snake_body[i].zoom_x = 0.1
  $snake_body[i].zoom_y = 0.1
  $snake_body[i].ox = $snake.bitmap.width / 2
  $snake_body[i].oy = $snake.bitmap.height / 2
  $snake_body[i].x = $snake_body[i-1].x - $snake.bitmap.width * $snake.zoom_x
  $snake_body[i].y = $snake.y
end

$direction = 0
$timer = 0
$time_window = 5

def bounderyCheck
  if $snake.x >= 640 - $snake.bitmap.width/2 * $snake.zoom_x
    return false
  end
  if $snake.x <= $snake.bitmap.width/2 * $snake.zoom_x
    return false
  end
  if $snake.y >= 480 - $snake.bitmap.height/2 * $snake.zoom_y
    return false
  end
  if $snake.y <= $snake.bitmap.height/2 * $snake.zoom_y
    return false
  end
  return true
end

def bodyMove
  for i in 1..$snake_length
    $snake_body[$snake_length+1-i].x = $snake_body[$snake_length-i].x
    $snake_body[$snake_length+1-i].y = $snake_body[$snake_length-i].y
  end
end

def move
  if $direction == 0
    return
  end
  if $direction == 1
    $snake.x += $step
    if !bounderyCheck
      $snake.x -= $step
      $direction = 0
      return
    end
    $snake.x -= $step
    bodyMove
    $snake.x += $step
  end
  if $direction == -1
    $snake.x -= $step
    if !bounderyCheck
      $snake.x += $step
      $direction = 0
      return
    end
    $snake.x += $step
    bodyMove
    $snake.x -= $step
  end
  if $direction == 2
    $snake.y += $step
    if !bounderyCheck
      $snake.y -= $step
      $direction = 0
      return
    end
    $snake.y -= $step
    bodyMove
    $snake.y += $step
  end
  if $direction == -2
    $snake.y -= $step
    if !bounderyCheck
      $snake.y += $step
      $direction = 0
      return
    end
    $snake.y += $step
    bodyMove
    $snake.y -= $step
  end
end

loop do
  Graphics.update
  Input.update
  if Input.trigger?(Input::RIGHT)
    if $direction != -1
      $direction = 1
    end
  end
  if Input.trigger?(Input::LEFT)
    if $direction != 1
      $direction = -1
    end
  end
  if Input.trigger?(Input::DOWN)
    if $direction != -2
      $direction = 2
    end
  end
  if Input.trigger?(Input::UP)
    if $direction != 2
      $direction = -2
    end
  end
  if $timer == 0
    move
  end
  if $timer != $time_window
    $timer += 1
  else
    $timer = 0
  end
end

 Target

When there is no target, we should generate one.

$snake = Sprite.new
$snake.bitmap = Bitmap.new("Graphics/Pictures/Circle")
$snake.zoom_x = 0.1
$snake.zoom_y = 0.1
$snake.ox = $snake.bitmap.width / 2
$snake.oy = $snake.bitmap.height / 2
$snake.x = 10 * $snake.bitmap.width * $snake.zoom_x + $snake.ox * $snake.zoom_x
$snake.y = 8 * $snake.bitmap.height * $snake.zoom_y + $snake.oy * $snake.zoom_y
$snake_body = [$snake]
$snake_length = 5
$step = $snake.bitmap.width * $snake.zoom_x

$target = Sprite.new
$target.bitmap = Bitmap.new("Graphics/Pictures/Circle")
$target.zoom_x = 0.1
$target.zoom_y = 0.1
$target.ox = $snake.bitmap.width / 2
$target.oy = $snake.bitmap.height / 2
$target.x = -1 * $snake.bitmap.width * $snake.zoom_x + $snake.ox
$target.y = -1 * $snake.bitmap.height * $snake.zoom_y + $snake.oy

for i in 1..$snake_length
  $snake_body.push(Sprite.new)
  $snake_body[i].bitmap = Bitmap.new("Graphics/Pictures/Circle")
  $snake_body[i].zoom_x = 0.1
  $snake_body[i].zoom_y = 0.1
  $snake_body[i].ox = $snake.ox
  $snake_body[i].oy = $snake.ox
  $snake_body[i].x = $snake_body[i-1].x - $snake.bitmap.width * $snake.zoom_x
  $snake_body[i].y = $snake.y
end

$direction = 0
$timer = 0
$time_window = 5

def generateTarget
  rx = rand(640/$snake.bitmap.width/$snake.zoom_x).to_i * $snake.bitmap.width * $snake.zoom_x + $snake.ox * $snake.zoom_x
  ry = rand(480/$snake.bitmap.height/$snake.zoom_y).to_i * $snake.bitmap.height * $snake.zoom_y + $snake.oy * $snake.zoom_y
  $target.x = rx
  $target.y = ry
end

generateTarget

def eatCheck
  if ($target.x - $snake.x).abs < $snake.bitmap.width * $snake.zoom_x and ($target.y - $snake.y).abs < $snake.bitmap.height * $snake.zoom_y
    generateTarget
  end
end

def bounderyCheck
  if $snake.x >= 640
    return false
  end
  if $snake.x <= 0
    return false
  end
  if $snake.y >= 480
    return false
  end
  if $snake.y <= 0
    return false
  end
  return true
end

def bodyMove
  for i in 1..$snake_length
    $snake_body[$snake_length+1-i].x = $snake_body[$snake_length-i].x
    $snake_body[$snake_length+1-i].y = $snake_body[$snake_length-i].y
  end
end

def move
  if $direction == 0
    return
  end
  if $direction == 1
    $snake.x += $step
    if !bounderyCheck
      $snake.x -= $step
      $direction = 0
      return
    end
    $snake.x -= $step
    bodyMove
    $snake.x += $step
  end
  if $direction == -1
    $snake.x -= $step
    if !bounderyCheck
      $snake.x += $step
      $direction = 0
      return
    end
    $snake.x += $step
    bodyMove
    $snake.x -= $step
  end
  if $direction == 2
    $snake.y += $step
    if !bounderyCheck
      $snake.y -= $step
      $direction = 0
      return
    end
    $snake.y -= $step
    bodyMove
    $snake.y += $step
  end
  if $direction == -2
    $snake.y -= $step
    if !bounderyCheck
      $snake.y += $step
      $direction = 0
      return
    end
    $snake.y += $step
    bodyMove
    $snake.y -= $step
  end
end

loop do
  Graphics.update
  Input.update
  if Input.trigger?(Input::RIGHT)
    if $direction != -1
      $direction = 1
    end
  end
  if Input.trigger?(Input::LEFT)
    if $direction != 1
      $direction = -1
    end
  end
  if Input.trigger?(Input::DOWN)
    if $direction != -2
      $direction = 2
    end
  end
  if Input.trigger?(Input::UP)
    if $direction != 2
      $direction = -2
    end
  end
  if $timer == 0
    move
    eatCheck
  end
  if $timer != $time_window
    $timer += 1
  else
    $timer = 0
  end
end

Score and Pause

Then we try to display the score and make it can be paused by user.

$snake = Sprite.new
$snake.bitmap = Bitmap.new("Graphics/Pictures/Circle")
$snake.bitmap.hue_change(90)
$snake.zoom_x = 0.1
$snake.zoom_y = 0.1
$snake.ox = $snake.bitmap.width / 2
$snake.oy = $snake.bitmap.height / 2
$snake.x = 10 * $snake.bitmap.width * $snake.zoom_x + $snake.ox * $snake.zoom_x
$snake.y = 8 * $snake.bitmap.height * $snake.zoom_y + $snake.oy * $snake.zoom_y
$snake_body = [$snake]
$snake_length = 5
$step = $snake.bitmap.width * $snake.zoom_x

$target = Sprite.new
$target.bitmap = Bitmap.new("Graphics/Pictures/Circle")
$target.zoom_x = 0.1
$target.zoom_y = 0.1
$target.ox = $snake.bitmap.width / 2
$target.oy = $snake.bitmap.height / 2
$target.x = -1 * $snake.bitmap.width * $snake.zoom_x + $snake.ox
$target.y = -1 * $snake.bitmap.height * $snake.zoom_y + $snake.oy

$score = 0
$score_text = Sprite.new
$score_text.bitmap = Bitmap.new(640, 30)
$score_text.bitmap.draw_text(0, 0, 640, 30, "Score:"+$score.to_s)
$state_text = Sprite.new
$state_text.y = 30
$state_text.bitmap = Bitmap.new(640, 30)
$state_text.bitmap.draw_text(0, 0, 640, 30, "Pause")

for i in 1..$snake_length
  $snake_body.push(Sprite.new)
  $snake_body[i].bitmap = Bitmap.new("Graphics/Pictures/Circle")
  $snake_body[i].zoom_x = 0.1
  $snake_body[i].zoom_y = 0.1
  $snake_body[i].ox = $snake.ox
  $snake_body[i].oy = $snake.ox
  $snake_body[i].x = $snake_body[i-1].x - $snake.bitmap.width * $snake.zoom_x
  $snake_body[i].y = $snake.y
end

$direction = 0
$timer = 0
$time_window = 5
$game_state = 0

def generateTarget
  rx = rand(640/$snake.bitmap.width/$snake.zoom_x).to_i * $snake.bitmap.width * $snake.zoom_x + $snake.ox * $snake.zoom_x
  ry = rand(480/$snake.bitmap.height/$snake.zoom_y).to_i * $snake.bitmap.height * $snake.zoom_y + $snake.oy * $snake.zoom_y
  $target.x = rx
  $target.y = ry
end

generateTarget

def eatCheck
  if ($target.x - $snake.x).abs < $snake.bitmap.width * $snake.zoom_x and ($target.y - $snake.y).abs < $snake.bitmap.height * $snake.zoom_y
    generateTarget
    $score += 1
    $score_text.bitmap.dispose
    $score_text.bitmap = Bitmap.new(640, 30)
    $score_text.bitmap.draw_text(0, 0, 640, 30, "Score:"+$score.to_s)
  end
end

def bounderyCheck
  if $snake.x >= 640
    return false
  end
  if $snake.x <= 0
    return false
  end
  if $snake.y >= 480
    return false
  end
  if $snake.y <= 0
    return false
  end
  return true
end

def bodyMove
  for i in 1..$snake_length
    $snake_body[$snake_length+1-i].x = $snake_body[$snake_length-i].x
    $snake_body[$snake_length+1-i].y = $snake_body[$snake_length-i].y
  end
end

def move
  if $direction == 0
    return
  end
  if $direction == 1
    $snake.x += $step
    if !bounderyCheck
      $snake.x -= $step
      $direction = 0
      return
    end
    $snake.x -= $step
    bodyMove
    $snake.x += $step
  end
  if $direction == -1
    $snake.x -= $step
    if !bounderyCheck
      $snake.x += $step
      $direction = 0
      return
    end
    $snake.x += $step
    bodyMove
    $snake.x -= $step
  end
  if $direction == 2
    $snake.y += $step
    if !bounderyCheck
      $snake.y -= $step
      $direction = 0
      return
    end
    $snake.y -= $step
    bodyMove
    $snake.y += $step
  end
  if $direction == -2
    $snake.y -= $step
    if !bounderyCheck
      $snake.y += $step
      $direction = 0
      return
    end
    $snake.y += $step
    bodyMove
    $snake.y -= $step
  end
end

loop do
  Graphics.update
  Input.update
  if Input.trigger?(Input::C)
    if $game_state == 0
      $game_state = 1
      $state_text.bitmap.dispose
    elsif $game_state == 1
      $game_state = 0
      $state_text.bitmap = Bitmap.new(640, 30)
      $state_text.bitmap.draw_text(0, 0, 640, 30, "Pause")
    end
  end
  if $game_state == 1
    if Input.trigger?(Input::RIGHT)
      if $direction != -1
        $direction = 1
      end
    end
    if Input.trigger?(Input::LEFT)
      if $direction != 1
        $direction = -1
      end
    end
    if Input.trigger?(Input::DOWN)
      if $direction != -2
        $direction = 2
      end
    end
    if Input.trigger?(Input::UP)
      if $direction != 2
        $direction = -2
      end
    end
    if $timer == 0
      move
      eatCheck
    end
    if $timer != $time_window
      $timer += 1
    else
      $timer = 0
    end
  end
end

Game Over

def initGame
  $snake = Sprite.new
  $snake.bitmap = Bitmap.new("Graphics/Pictures/Circle")
  $snake.bitmap.hue_change(90)
  $snake.zoom_x = 0.1
  $snake.zoom_y = 0.1
  $snake.ox = $snake.bitmap.width / 2
  $snake.oy = $snake.bitmap.height / 2
  $snake.x = 10 * $snake.bitmap.width * $snake.zoom_x + $snake.ox * $snake.zoom_x
  $snake.y = 8 * $snake.bitmap.height * $snake.zoom_y + $snake.oy * $snake.zoom_y
  $snake_body = [$snake]
  $snake_length = 5
  $step = $snake.bitmap.width * $snake.zoom_x
  
  $target = Sprite.new
  $target.bitmap = Bitmap.new("Graphics/Pictures/Circle")
  $target.zoom_x = 0.1
  $target.zoom_y = 0.1
  $target.ox = $snake.bitmap.width / 2
  $target.oy = $snake.bitmap.height / 2
  $target.x = -1 * $snake.bitmap.width * $snake.zoom_x + $snake.ox
  $target.y = -1 * $snake.bitmap.height * $snake.zoom_y + $snake.oy
  
  $score = 0
  $score_text = Sprite.new
  $score_text.bitmap = Bitmap.new(640, 30)
  $score_text.bitmap.draw_text(0, 0, 640, 30, "Score:"+$score.to_s)
  $state_text = Sprite.new
  $state_text.y = 30
  $state_text.bitmap = Bitmap.new(640, 30)
  $state_text.bitmap.draw_text(0, 0, 640, 30, "Pause(Press C to continue)")
  
  for i in 1..$snake_length
    $snake_body.push(Sprite.new)
    $snake_body[i].bitmap = Bitmap.new("Graphics/Pictures/Circle")
    $snake_body[i].zoom_x = 0.1
    $snake_body[i].zoom_y = 0.1
    $snake_body[i].ox = $snake.ox
    $snake_body[i].oy = $snake.ox
    $snake_body[i].x = $snake_body[i-1].x - $snake.bitmap.width * $snake.zoom_x
    $snake_body[i].y = $snake.y
  end
  
  $direction = 1
  $timer = 0
  $time_window = 5
  $game_state = 0
end

initGame

def generateTarget
  rx = rand(640/$snake.bitmap.width/$snake.zoom_x).to_i * $snake.bitmap.width * $snake.zoom_x + $snake.ox * $snake.zoom_x
  ry = rand(480/$snake.bitmap.height/$snake.zoom_y).to_i * $snake.bitmap.height * $snake.zoom_y + $snake.oy * $snake.zoom_y
  $target.x = rx
  $target.y = ry
end

generateTarget

def eatCheck
  if ($target.x - $snake.x).abs < $snake.bitmap.width * $snake.zoom_x and ($target.y - $snake.y).abs < $snake.bitmap.height * $snake.zoom_y
    generateTarget
    $score += 1
    $score_text.bitmap.dispose
    $score_text.bitmap = Bitmap.new(640, 30)
    $score_text.bitmap.draw_text(0, 0, 640, 30, "Score:"+$score.to_s)
    $snake_body.push(Sprite.new)
    $snake_body[$snake_length].bitmap = Bitmap.new("Graphics/Pictures/Circle")
    $snake_body[$snake_length].zoom_x = 0.1
    $snake_body[$snake_length].zoom_y = 0.1
    $snake_body[$snake_length].ox = $snake.ox
    $snake_body[$snake_length].oy = $snake.ox
    $snake_body[$snake_length].x = $snake_body[$snake_length-1].x
    $snake_body[$snake_length].y = $snake_body[$snake_length-1].y
    $snake_length += 1
  end
end

def bounderyCheck
  if $snake.x >= 640
    return false
  end
  if $snake.x <= 0
    return false
  end
  if $snake.y >= 480
    return false
  end
  if $snake.y <= 0
    return false
  end
  return true
end

def bodyMove
  for i in 1..$snake_length
    $snake_body[$snake_length+1-i].x = $snake_body[$snake_length-i].x
    $snake_body[$snake_length+1-i].y = $snake_body[$snake_length-i].y
  end
end

def move
  if $direction == 0
    return
  end
  if $direction == 1
    $snake.x += $step
    if !bounderyCheck
      $snake.x -= $step
      $direction = 0
      gameOver
      return
    end
    $snake.x -= $step
    bodyMove
    $snake.x += $step
  end
  if $direction == -1
    $snake.x -= $step
    if !bounderyCheck
      $snake.x += $step
      $direction = 0
      gameOver
      return
    end
    $snake.x += $step
    bodyMove
    $snake.x -= $step
  end
  if $direction == 2
    $snake.y += $step
    if !bounderyCheck
      $snake.y -= $step
      $direction = 0
      gameOver
      return
    end
    $snake.y -= $step
    bodyMove
    $snake.y += $step
  end
  if $direction == -2
    $snake.y -= $step
    if !bounderyCheck
      $snake.y += $step
      $direction = 0
      gameOver
      return
    end
    $snake.y += $step
    bodyMove
    $snake.y -= $step
  end
end

def update
  if Input.trigger?(Input::C)
    if $game_state == 0
      $game_state = 1
      $state_text.bitmap.dispose
    elsif $game_state == 1
      $game_state = 0
      $state_text.bitmap = Bitmap.new(640, 30)
      $state_text.bitmap.draw_text(0, 0, 640, 30, "Pause(Press C to continue)")
    end
  end
  if $game_state == 1
    if Input.trigger?(Input::RIGHT)
      if $direction != -1
        $direction = 1
      end
    end
    if Input.trigger?(Input::LEFT)
      if $direction != 1
        $direction = -1
      end
    end
    if Input.trigger?(Input::DOWN)
      if $direction != -2
        $direction = 2
      end
    end
    if Input.trigger?(Input::UP)
      if $direction != 2
        $direction = -2
      end
    end
    if $timer == 0
      eatCheck
      move
    end
    if $timer != $time_window
      $timer += 1
    else
      $timer = 0
    end
  end
end

def gameOver
  $state_text.dispose
  $score_text.dispose
  for i in $snake_body
    i.dispose
  end
  $target.dispose
  initGame
end

loop do
  Graphics.update
  Input.update
  update
end

Self collision

def initGame
  $snake = Sprite.new
  $snake.bitmap = Bitmap.new("Graphics/Pictures/Circle")
  $snake.bitmap.hue_change(90)
  $snake.zoom_x = 0.1
  $snake.zoom_y = 0.1
  $snake.ox = $snake.bitmap.width / 2
  $snake.oy = $snake.bitmap.height / 2
  $snake.x = 10 * $snake.bitmap.width * $snake.zoom_x + $snake.ox * $snake.zoom_x
  $snake.y = 8 * $snake.bitmap.height * $snake.zoom_y + $snake.oy * $snake.zoom_y
  $snake_body = [$snake]
  $snake_length = 5
  $step = $snake.bitmap.width * $snake.zoom_x
  
  $target = Sprite.new
  $target.bitmap = Bitmap.new("Graphics/Pictures/Circle")
  $target.zoom_x = 0.1
  $target.zoom_y = 0.1
  $target.ox = $snake.bitmap.width / 2
  $target.oy = $snake.bitmap.height / 2
  $target.x = -1 * $snake.bitmap.width * $snake.zoom_x + $snake.ox
  $target.y = -1 * $snake.bitmap.height * $snake.zoom_y + $snake.oy
  
  $score = 0
  $score_text = Sprite.new
  $score_text.bitmap = Bitmap.new(640, 30)
  $score_text.bitmap.draw_text(0, 0, 640, 30, "Score:"+$score.to_s)
  $state_text = Sprite.new
  $state_text.y = 30
  $state_text.bitmap = Bitmap.new(640, 30)
  $state_text.bitmap.draw_text(0, 0, 640, 30, "Pause(Press C to continue)")
  
  for i in 1..$snake_length
    $snake_body.push(Sprite.new)
    $snake_body[i].bitmap = Bitmap.new("Graphics/Pictures/Circle")
    $snake_body[i].zoom_x = 0.1
    $snake_body[i].zoom_y = 0.1
    $snake_body[i].ox = $snake.ox
    $snake_body[i].oy = $snake.ox
    $snake_body[i].x = $snake_body[i-1].x - $snake.bitmap.width * $snake.zoom_x
    $snake_body[i].y = $snake.y
  end
  
  $direction = 1
  $timer = 0
  $time_window = 5
  $game_state = 0
end

initGame

def generateTarget
  rx = rand(640/$snake.bitmap.width/$snake.zoom_x).to_i * $snake.bitmap.width * $snake.zoom_x + $snake.ox * $snake.zoom_x
  ry = rand(480/$snake.bitmap.height/$snake.zoom_y).to_i * $snake.bitmap.height * $snake.zoom_y + $snake.oy * $snake.zoom_y
  $target.x = rx
  $target.y = ry
end

generateTarget

def eatCheck
  if ($target.x - $snake.x).abs < $snake.bitmap.width * $snake.zoom_x and ($target.y - $snake.y).abs < $snake.bitmap.height * $snake.zoom_y
    generateTarget
    $score += 1
    $score_text.bitmap.dispose
    $score_text.bitmap = Bitmap.new(640, 30)
    $score_text.bitmap.draw_text(0, 0, 640, 30, "Score:"+$score.to_s)
    $snake_body.push(Sprite.new)
    $snake_body[$snake_length].bitmap = Bitmap.new("Graphics/Pictures/Circle")
    $snake_body[$snake_length].zoom_x = 0.1
    $snake_body[$snake_length].zoom_y = 0.1
    $snake_body[$snake_length].ox = $snake.ox
    $snake_body[$snake_length].oy = $snake.ox
    $snake_body[$snake_length].x = $snake_body[$snake_length-1].x
    $snake_body[$snake_length].y = $snake_body[$snake_length-1].y
    $snake_length += 1
  end
end

def bounderyCheck
  if $snake.x >= 640
    return false
  end
  if $snake.x <= 0
    return false
  end
  if $snake.y >= 480
    return false
  end
  if $snake.y <= 0
    return false
  end
  return true
end

def selfCollisionCheck
  for i in 1..$snake_length
    if ($snake_body[i].x-$snake.x).abs < $snake.ox*$snake.zoom_x and ($snake_body[i].y-$snake.y).abs < $snake.oy*$snake.zoom_y
      gameOver
      break
    end
  end
end

def bodyMove
  for i in 1..$snake_length
    $snake_body[$snake_length+1-i].x = $snake_body[$snake_length-i].x
    $snake_body[$snake_length+1-i].y = $snake_body[$snake_length-i].y
  end
end

def move
  if $direction == 0
    return
  end
  if $direction == 1
    $snake.x += $step
    if (!bounderyCheck)
      $snake.x -= $step
      $direction = 0
      gameOver
      return
    end
    $snake.x -= $step
    bodyMove
    $snake.x += $step
  end
  if $direction == -1
    $snake.x -= $step
    if (!bounderyCheck)
      $snake.x += $step
      $direction = 0
      gameOver
      return
    end
    $snake.x += $step
    bodyMove
    $snake.x -= $step
  end
  if $direction == 2
    $snake.y += $step
    if (!bounderyCheck)
      $snake.y -= $step
      $direction = 0
      gameOver
      return
    end
    $snake.y -= $step
    bodyMove
    $snake.y += $step
  end
  if $direction == -2
    $snake.y -= $step
    if (!bounderyCheck)
      $snake.y += $step
      $direction = 0
      gameOver
      return
    end
    $snake.y += $step
    bodyMove
    $snake.y -= $step
  end
end

def update
  if Input.trigger?(Input::C)
    if $game_state == 0
      $game_state = 1
      $state_text.bitmap.dispose
    elsif $game_state == 1
      $game_state = 0
      $state_text.bitmap = Bitmap.new(640, 30)
      $state_text.bitmap.draw_text(0, 0, 640, 30, "Pause(Press C to continue)")
    end
  end
  if $game_state == 1
    if Input.trigger?(Input::RIGHT)
      if $direction != -1
        $direction = 1
      end
    end
    if Input.trigger?(Input::LEFT)
      if $direction != 1
        $direction = -1
      end
    end
    if Input.trigger?(Input::DOWN)
      if $direction != -2
        $direction = 2
      end
    end
    if Input.trigger?(Input::UP)
      if $direction != 2
        $direction = -2
      end
    end
    if $timer == 0
      move
      selfCollisionCheck
      eatCheck
    end
    if $timer != $time_window
      $timer += 1
    else
      $timer = 0
    end
  end
end

def gameOver
  $state_text.dispose
  $score_text.dispose
  for i in $snake_body
    i.dispose
  end
  $target.dispose
  initGame
end

loop do
  Graphics.update
  Input.update
  update
end

A simple Retro Snaker has been finished!

Demo(Nutstore Account Needed):https://www.jianguoyun.com/p/DYfPnjEQxL7-BxiDy7wC

Github:https://github.com/Karma-Tiumo/RGSS-Explore/tree/master/RetroSnaker

猜你喜欢

转载自www.cnblogs.com/tiumo/p/11405007.html