文档章节

ruby写的一个小游戏

l
 lry77
发布于 2015/09/24 16:13
字数 1318
阅读 25
收藏 0
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




© 著作权归作者所有

上一篇: SQL的操作
l
粉丝 1
博文 126
码字总数 80007
作品 0
南京
私信 提问
iOS开源项目整理

github地址 iOS开源项目 整理官方Demo应用、第三方团体项目以及个人开发者项目,阅读别人的源码也是自我提升的一种方式 开源项目 ProducthuntOSX Mac 上开源的 Product Hunt OSX 客户端软件 ...

Daniel_s
2016/03/17
247
0
闯过这 54 关,点亮你的 Git 技能树

开篇 如今,Git 大行其道,颇有一统天下之势。 如果你的技能树上 Git 和 Github 的图标还没有点亮的话,你都不好意思说你是程序员。 别说互联网企业,我接触到的许多传统企业都在从 SVN,Cle...

小哥哥
2016/01/22
115
0
15个免费游戏,将帮助您学习如何 编写代码

  当我开始学习代码时,选择有限 - 很多书(甚至不是电子书),一些非常基本的在线教程,以及大量的实验。   在线学习在过去几年中已经走了很长的路。有互动课程,大量在线教程,以及我个...

芒果web
2017/04/10
0
0
Ruby 人工智能交互游戏--Ruby Warrior

Ruby Warrior 是一个游戏,它能让 Ruby 语言和人工智能以一种很有意思的方式互动。你所扮演的是一个爬上高塔顶层获得 Ruby 宝石的战士。每到一层你都要写下 Ruby 脚本指导战士与敌人战斗,拯...

孔小菜
2015/07/10
711
0
程序员不容错过的5大有趣编程网站,你知道几个?

学编程最重要的是实践,就是写代码、看别人写的代码、再写代码。 要掌握编程这一门手艺,要经过大量的训练。 如果编程练习可以变得有趣,那你一定可以乐此不疲,长期坚持。 下面w3cschol分享...

W3Cschool
2018/04/12
0
0

没有更多内容

加载失败,请刷新页面

加载更多

ES 集群上,业务单点如何优化升级?

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! ES 基础 ES 集群 ES 集群上业务优化 一、ES 基础 ES 的安装下载,网上一大片,我这...

泥瓦匠BYSocket
18分钟前
1
0
input accept属性限制文件上传格式

上传文件的类型;具体做法如下所示: 注意:accept属性可以限制上传格式,其有兼容性如下 《1》上传.csv格式的 <input text="file" accept=".csv" /> 《2》上传.xls格式 <input text="file"......

Jack088
25分钟前
1
0
阿里开发者招聘节 | 面试题01:如何实现一个高效的单向链表逆序输出?

面试,如同玩一场饥饿游戏:既要对环境了然于胸,又要对自身心知肚明。发现一个好工作不容易,但成功应聘又会面临一系列的挑战。 为帮助开发者们提升面试技能、有机会入职阿里,云栖社区特别...

阿里云云栖社区
36分钟前
3
0
使用scp命令在多个Linux系统间进行文件复制

一,什么是scp scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令。scp命令可以在linux服务器之间复制文件和目录.scp使用ssh安全协议传输数据,具有和ssh一样的验证机制,从而安全的远...

老孟的Linux私房菜
37分钟前
0
0
CentOS7中ESL服务部署记录

在CentOS7上部署价签系统,需要安装Mysql,Redis,emqtt,jdk。 1)Mysql,参考 2)Redis,参考 3)emqtt,参考 4)jdk,参考 在jdk的下载地址中,下载rpm文件,通过ftp上传到虚拟机后,通过...

莫在全
40分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部