ruby写的一个小游戏

require "dxruby"

class Boatleft
attr_accessor :x, :y
@@image0 = Image.loadToArray("image/boatright.png", 2, 1)

  def initialize(x,y)
    @x = x
	@y = y
	@animecount = 0
	@position = 1
	@animalnum = 0
  end
  
  def start
    baseimage = Image.load('image/boatright.png')
    image = baseimage.slice(173, 0, 172, 81)
	Window.draw(@x, @y, image)
  end
  
  def update
	@x = 471
  end
  
  def update2
    @x = 200
  end
  
  def canmove
    return true if $Boat.size >= 1
	return false
  end
  
  def add(object)
    if $leftobjectgroup.include? object
 
	else
	  return false
	end
	$Boat.each do |obj|
	  if obj.name == object.name
	    return false
	  end
	end
    if $Boat.size < 2
	  $Boat << object
    end
  end

  
  def addright(object)
    if $rightobjectgroup.include? object
	
	else
	  return false
	end
	$Boat.each do |obj|
	  if obj.name == object.name
	    return false
	  end
	end
    if $Boat.size < 2
	  $Boat << object
    end
  end

  def rightbug(object)
  	# $rightobjectgroup.each do |obj|
	  # if obj.name != object.name
	    # return false
	  # end
	# end
  end
  
  def position(size=0)
    @position+=size
    if @position%2 == 0
      return "right"
    else
	  return "left"
    end
  end
  
  def hasanimals
    $Boat.each do |animal|
      @animalnum+=1
      if @animalnum %2 == 0
        return 300
	  else
	    return 250
	  end
	end
  end
  
  def hasanimals2
    $Boat.each do |animal|
      @animalnum+=1
      if @animalnum %2 == 0
        return 510
	  else
	    return 560
	  end
	end
  end
  
  def moveboat(where)
    if where == "right"
      $Boat.each do |obj|
	    $rightobjectgroup << obj
	   if obj.name == "wolf1"
	      obj.x = 630
		  obj.y = 260
	   elsif obj.name == "wolf2"
	      obj.x = 630
		  obj.y = 334
	   elsif obj.name == "wolf3"
	      obj.x = 630
		  obj.y = 410
	   elsif obj.name == "sheep1"
	      obj.x = 700
		  obj.y = 260
	   elsif obj.name == "sheep2"
	      obj.x = 700
		  obj.y = 334
	   elsif obj.name == "sheep3"
	      obj.x = 700
		  obj.y = 410
	   end
	    leftdeletews(obj)
	  end
	  $Boat = []

    else
      if where == "left"
	    $Boat.each do |obj|
	      $leftobjectgroup << obj
	   if obj.name == "wolf1"
	      obj.x = 80
		  obj.y = 260
	   elsif obj.name == "wolf2"
	      obj.x = 80
		  obj.y = 334
	   elsif obj.name == "wolf3"
	      obj.x = 80
		  obj.y = 410
	   elsif obj.name == "sheep1"
	      obj.x = 150
		  obj.y = 260
	   elsif obj.name == "sheep2"
	      obj.x = 150
		  obj.y = 334
	   elsif obj.name == "sheep3"
	      obj.x = 150
		  obj.y = 410
	   end
	      rightdeletews(obj)
	    end
	    $Boat = []
      end
    end
  end
  
  def rightdeletews(object)
    $rightobjectgroup.each_index do |index|
      if $rightobjectgroup[index] == object
        $rightobjectgroup.delete_at(index)
	    break
      end
    end
  end
  
  def leftdeletews(object)
    $leftobjectgroup.each_index do |index|
      if $leftobjectgroup[index] == object
        $leftobjectgroup.delete_at(index)
	    break
      end
    end
  end
end

class Sheep
attr_accessor :x, :y, :name
# @@xwei = 150
# @@image0 = Image.load('image/sheep.png')
  def initialize(x,y,name)
    # @x = 150
	@x = x
    @y = y
	@name = name
	@animecount = 0
  end
  def start
    # if @@xwei == 150
	  baseimage = Image.load('image/sheep.png')
      image = baseimage.slice(0, 0, 57.5, 75)
	  Window.draw(@x, @y, image)
    # end
  end
  def update
    @@xwei += 1
	if @animecount % 10 == 0
	@x += @animecount
	end
	if @x > 646 - 175

    end
    @x = 175 if @x < 175
    @x = 647 - 175 if @x > 647 - 175
    @animecount += 1
    @animecount -= 40 if @animecount >= 40
  end
  def draw
    if @x < 646 - 174
      image = @@image0.slice(0, 0, 57.5, 75)
	  Window.draw(@x, @y, image)
	end
  end
end

class Wolf
  attr_accessor :x, :y , :name
  # @@image0 = Image.load('image/wolf.png')
  def initialize(x,y,name)
    @x = x
	# @x = 220
    @y = y
	@animecount = 0
	@wolfnum = 0
	@name = name
  end
  def start
    baseimage = Image.load('image/wolf.png')
    image = baseimage.slice(0, 0, 79, 75)
	Window.draw(@x, @y, image)
  end
  
  def update
    @@xwei += 1
	if @animecount % 10 == 0
	@x += @animecount
	end
	if @x > 646 - 175

    end
    @x = 175 if @x < 79
    @x = 647 - 79 if @x > 647 - 79

    @animecount += 1
    @animecount -= 40 if @animecount >= 40
  end
  def draw
    if @x < 646 - 175
      image = @@image0.slice(0, 0, 79, 75)
	  Window.draw(@x, @y, image)
	end
  end
  def haswolf
      $Boat.each do |wolf|
	  if wolf.class.to_s.start_with? 'Wolf'
	    @wolfnum+=1
	  end
	  p @wolfnum
	  p wolf.class.to_s
	  return false if @wolfnum > 1
	  return true
	end
  end
end
@@gameover = Image.load("image/over.png")
@@image = Image.load("image/background.png")
@@win = Image.load("image/win.png")
@@description = Image.load("image/description.png")
Window.caption = "Sheep_and_Wolf_story"
Window.width = 822
Window.height = 497
def initializes
  $leftobjectgroup = []
  $rightobjectgroup = []
  $Boat = []
  $boat = Boatleft.new(200,300)
  $wolf1 = Wolf.new(80,260,"wolf1")
  $wolf2 = Wolf.new(80,334,"wolf2")
  $wolf3 = Wolf.new(80,410,"wolf3")
  $sheep1 = Sheep.new(150,260,"sheep1")
  $sheep2 = Sheep.new(150,334,"sheep2")
  $sheep3 = Sheep.new(150,410,"sheep3")
  $leftobjectgroup.push($boat)
  $leftobjectgroup.push($sheep1)
  $leftobjectgroup.push($sheep2)
  $leftobjectgroup.push($sheep3)
  $leftobjectgroup.push($wolf1)
  $leftobjectgroup.push($wolf2)
  $leftobjectgroup.push($wolf3)
end
# $sheep = Sheep.new
initializes

  def judge
    sheepnum = 0
    wolfnum = 0
    leftsheepnum = 0
    leftwolfnum = 0
    $rightobjectgroup.each do |obj|
	  if obj.class.to_s.start_with? 'Wolf'
	    wolfnum+=1
	  end
	  if obj.class.to_s.start_with? 'Sheep'
	    sheepnum+=1
	  end
    end
  	if wolfnum > sheepnum
	  if sheepnum == 0
	    
	  else
	    $canclick = false
		$re = true
		# initializes
		return Window.draw(220, 160, @@gameover)
	  end
	end
	$leftobjectgroup.each do |obj|
	  if obj.class.to_s.start_with? 'Wolf'
	    leftwolfnum+=1
	  end
	  if obj.class.to_s.start_with? 'Sheep'
	    leftsheepnum+=1
	  end
    end
  	if leftwolfnum > leftsheepnum
	  if leftsheepnum == 0
	    
	  else
	    $canclick = false
		$re = true
		# 
	    return Window.draw(220, 160, @@gameover)
	  end
	end
	if sheepnum == 3 && sheepnum > wolfnum
	  $canclick = false
	  return Window.draw(220, 160, @@win)
	  # @continue = false
	end
  end
# ObjectGroup.push($$boat)

description = true
$canclick = true
$re = false
Window.loop do
  if Input.mousePush?(M_LBUTTON)
    #retry
    if $re == true
      if Input.mousePosX > 270 && Input.mousePosX < 340 && Input.mousePosY > 350 && Input.mousePosY < 420
	    initializes
		$canclick = true
		$re = false
	  end
	  if Input.mousePosX > 470 && Input.mousePosX < 540 && Input.mousePosY > 350 && Input.mousePosY < 420
	    break
	  end
	end
	#game description
    if $canclick == true
    if Input.mousePosX > 200 && Input.mousePosX < 350 && Input.mousePosY > 330 && Input.mousePosY < 385 && description == true
	  description = false
	end
    if Input.mousePosX > 470 && Input.mousePosX < 650 && Input.mousePosY > 330 && Input.mousePosY < 385 && description == true
	  break
	end
	#game start
	if description == false
    if Input.mousePosX > 80 && Input.mousePosX < 155 && Input.mousePosY > 260 && Input.mousePosY < 335 && $boat.position == "left"
      if $boat.add($wolf1)
	  $wolf1.x = 220
	  $wolf1.y = 260
	  $wolf1.x = $boat.hasanimals
	  end
	end
	if Input.mousePosX > 80 && Input.mousePosX < 155 && Input.mousePosY > 334 && Input.mousePosY < 409 && $boat.position == "left"
	  if $boat.add($wolf2)
		$wolf2.x = 220
	    $wolf2.y = 260
	    $wolf2.x = $boat.hasanimals
	  end
	end
	if Input.mousePosX > 80 && Input.mousePosX < 155 && Input.mousePosY > 410 && Input.mousePosY < 485 && $boat.position == "left"
	  if $boat.add($wolf3)
		$wolf3.x = 220
	    $wolf3.y = 260
		$wolf3.x = $boat.hasanimals
	  end
	end
	if Input.mousePosX > 150 && Input.mousePosX < 208 && Input.mousePosY > 260 && Input.mousePosY < 335 && $boat.position == "left"
	  if $boat.add($sheep1)
	    $sheep1.x = 300
		$sheep1.y = 260
		$sheep1.x = $boat.hasanimals
	  end
	end
	if Input.mousePosX > 150 && Input.mousePosX < 208 && Input.mousePosY > 334 && Input.mousePosY < 409 && $boat.position == "left"
	  if $boat.add($sheep2)
	    $sheep2.x = 300
		$sheep2.y = 260
		$sheep2.x = $boat.hasanimals
	  end
	end
	if Input.mousePosX > 150 && Input.mousePosX < 208 && Input.mousePosY > 410 && Input.mousePosY < 485 && $boat.position == "left"
	  if $boat.add($sheep3)
	    $sheep3.x = 300
		$sheep3.y = 260
		$sheep3.x = $boat.hasanimals
	  end
	end
	if Input.mousePosX > 224 && Input.mousePosX < 370 && Input.mousePosY > 260 && Input.mousePosY < 335 && $boat.position == "left"
	  $Boat.each do |obj|
	   if obj.name == "wolf1"
	      obj.x = 80
		  obj.y = 260
	   elsif obj.name == "wolf2"
	      obj.x = 80
		  obj.y = 334
	   elsif obj.name == "wolf3"
	      obj.x = 80
		  obj.y = 410
	   elsif obj.name == "sheep1"
	      obj.x = 150
		  obj.y = 260
	   elsif obj.name == "sheep2"
	      obj.x = 150
		  obj.y = 334
	   elsif obj.name == "sheep3"
	      obj.x = 150
		  obj.y = 410
	   end
	   $Boat.each_index do |index|
	     $Boat.delete_at(index)
	   end
	  end
	end

	# move $boat
	if Input.mousePosX > 224 && Input.mousePosX < 370 && Input.mousePosY > 336 && Input.mousePosY < 380 && $boat.canmove && $boat.position == "left"
	    $boat.moveboat($boat.position(1))
	    $boat.update
	end
	# right
    if Input.mousePosX > 630 && Input.mousePosX < 700 && Input.mousePosY > 260 && Input.mousePosY < 335 && $boat.position == "right"
      if $boat.addright($wolf1)
	  $wolf1.x = 700
	  $wolf1.y = 260
	  $wolf1.x = $boat.hasanimals2
	  end
	end
	if Input.mousePosX > 630 && Input.mousePosX < 700 && Input.mousePosY > 334 && Input.mousePosY < 409 && $boat.position == "right"
	  if $boat.addright($wolf2)
		$wolf2.x = 700
	    $wolf2.y = 260
	    $wolf2.x = $boat.hasanimals2
	  end
	end
	if Input.mousePosX > 630 && Input.mousePosX < 700 && Input.mousePosY > 410 && Input.mousePosY < 485 && $boat.position == "right"
	  if $boat.addright($wolf3)
		$wolf3.x = 700
	    $wolf3.y = 260
		$wolf3.x = $boat.hasanimals2
	  end
	end
	if Input.mousePosX > 700 && Input.mousePosX < 750 && Input.mousePosY > 260 && Input.mousePosY < 335 && $boat.position == "right"
	  if $boat.addright($sheep1)
	    $sheep1.x = 300
		$sheep1.y = 260
		$sheep1.x = $boat.hasanimals2
	  end
	end
	if Input.mousePosX > 700 && Input.mousePosX < 750 && Input.mousePosY > 334 && Input.mousePosY < 409 && $boat.position == "right"
	  if $boat.addright($sheep2)
	    $sheep2.x = 300
		$sheep2.y = 260
		$sheep2.x = $boat.hasanimals2
	  end
	end
	if Input.mousePosX > 700 && Input.mousePosX < 750 && Input.mousePosY > 410 && Input.mousePosY < 485 && $boat.position == "right"
	  if $boat.addright($sheep3)
	    $sheep3.x = 300
		$sheep3.y = 260
		$sheep3.x = $boat.hasanimals2
	  end
	end
	
	if Input.mousePosX > 510 && Input.mousePosX < 610 && Input.mousePosY > 260 && Input.mousePosY < 335 && $boat.position == "right"
	  $Boat.each do |obj|
	   if obj.name == "wolf1"
	      obj.x = 630
		  obj.y = 260
	   elsif obj.name == "wolf2"
	      obj.x = 630
		  obj.y = 334
	   elsif obj.name == "wolf3"
	      obj.x = 630
		  obj.y = 410
	   elsif obj.name == "sheep1"
	      obj.x = 700
		  obj.y = 260
	   elsif obj.name == "sheep2"
	      obj.x = 700
		  obj.y = 334
	   elsif obj.name == "sheep3"
	      obj.x = 700
		  obj.y = 410
	   end
	   $Boat.each_index do |index|
	     $Boat.delete_at(index)
	   end
	  end
	end
	  if Input.mousePosX > 480 && Input.mousePosX < 610 && Input.mousePosY > 336 && Input.mousePosY < 380 && $boat.canmove && $boat.position == "right"
	    $boat.moveboat($boat.position(1))
	    $boat.update2
	  end
	end
  end
  end
  
  Window.draw(0, 0, @@image)
  $leftobjectgroup.each do |obj|
	obj.start
  end
  $rightobjectgroup.each do |obj|
	obj.start
  end
  
  if description == true
    Window.draw(110, 85, @@description)
  end
  judge
end




转载于:https://my.oschina.net/phoebus789/blog/510730

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值