【RGSS】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
  ...
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
  ...
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
  ...
end

generateTarget

def eatCheck
  ...
end

def bounderyCheck
  ...
end

def bodyMove
  ...
end

def move
  ...
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
  ...
end

generateTarget

def eatCheck
  ...
end

def bounderyCheck
  ...
end

def bodyMove
  ...
end

def move
  ...
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
  ...
end

initGame

def generateTarget
  ...
end

generateTarget

def eatCheck
  ...
end

def bounderyCheck
  ...
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
  ...
end

def move
  ...
end

def update
  ...
end

def gameOver
  ...
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RPGXP脚本学习大全101: 显示文章 102: 显示选择项 103: 数值输入的处理 104: 更改文章选项 105: 按钮输入的处理 106: 等待 108: 注释 111: 条件分歧 112: 循环 113: 中断循环 115: 中断事件处理 116: 暂时消除事件 117: 公共事件 118: 标签 119: 标签跳转 121: 开关操作 122: 变量操作 123: 独立开关操作 124: 计时器操作 125: 增减金钱 126: 增减物品 127: 增减武器 128: 增减防具 129: 替换队员 131: 更改窗口外观 132: 更改战斗 BGM 133: 更改战斗结束 ME 134: 更改禁止存档 135: 更改禁止菜单 136: 更改禁止遇敌 201: 场所移动 202: 设置事件位置 203: 画面卷动 204: 更改地图设置 205: 更改雾的色调 206: 更改雾的不透明度 207: 显示动画 208: 更改透明状态 209: 设置移动路线 210: 等待移动结束 221: 准备渐变 222: 执行渐变 223: 更改画面色调 224: 画面闪烁 225: 画面震动 231: 显示图片 232: 移动图片 233: 旋转图片 234: 更改图片色调 235: 图片消失 236: 天气设置 241: 演奏 BGM 242: 淡出 BGM 245: 演奏 BGS 246: 淡出 BGS 247: 记忆 BGM / BGS 248: 还原 BGM / BGS 249: 演奏 ME 250: 演奏 SE 251: 停止 SE 301: 战斗处理 302: 商店处理 303: 名称输入处理 311: 增减 HP 312: 增减 SP 313: 更改状态 314: 完全回复 315: 增减 EXP 316: 增减等级 317: 增减能力值 318: 增减特技 319: 变更装备 320: 更改角色姓名 321: 更改角色职业 322: 更改角色图形 331: 增减敌人 HP 332: 增减敌人 SP 333: 更改敌人状态 334: 敌人出现 335: 敌人变身 336: 敌人完全回复 337: 显示动画 338: 伤害处理 339: 强制行动 340: 战斗中断 351: 呼叫菜单画面 352: 呼叫存档画面 353: 游戏结束 354: 返回标题画面 355: 脚本
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值