赞 | 0 |
VIP | 0 |
好人卡 | 0 |
积分 | 1 |
经验 | 5414 |
最后登录 | 2019-7-22 |
在线时间 | 100 小时 |
Lv1.梦旅人
- 梦石
- 0
- 星屑
- 55
- 在线时间
- 100 小时
- 注册时间
- 2010-7-30
- 帖子
- 232
|
#============================================================================== # # ●贪吃蛇v1.0 # by 猫哥哥 #============================================================================== class Tanchishe def initialize #生成棋盘 @size = 18 @world = [] for i in 1..@size for j in 1..@size @world.push([i,j]) end end @ox = 60 #原点 @oy = 40 draw_line((@ox/20)-1 ,(@oy/20)-1) @worm = {} @food = {} @balls = [] @key = 6 draw_worm(4,8) draw_worm(5,8) draw_worm(6,8) draw_worm(7,8) draw_worm(8,8) draw_worm(9,8) draw_food @speed = 5 #值越大,速度越慢 $FENSHU_WORM = 0 #分数 @score2 = Sprite.new @score2.bitmap = Bitmap.new(200,20) @score2.x = @ox +350 @score2.y = @oy +330 @score2.bitmap.draw_text(0, 0,200,20,"得分",1) @score = Sprite.new @score.bitmap = Bitmap.new(200,40) @score.x = @ox +350 @score.y = @oy +350 score_refresh #刷新分数 @step = 0 end def main loop do Graphics.update Input.update update if @step == 1 move end end end def score_refresh @score.bitmap.clear @score.bitmap.draw_text(0, 20,200,20,$FENSHU_WORM.to_s,2 ) end def draw_line(x,y) #描绘边缘 k = [] for i in 0..3 k.push(Sprite.new) end k[0].bitmap = Bitmap.new(380,10) #上 k[1].bitmap = Bitmap.new(380,10) #下 k[2].bitmap = Bitmap.new(10,400) #左 k[3].bitmap = Bitmap.new(10,400) #右 for kk in k rect = kk.bitmap.rect color = Color.new(205,205,205) kk.bitmap.fill_rect(rect, color) end k[0].x = x*20 + 20 k[0].y = y*20 + 10 k[1].x = x*20 + 20 k[1].y = y*20 + 400 k[2].x = x*20 + 10 k[2].y = y*20 + 10 k[3].x = x*20 + 400 k[3].y = y*20 + 10 end def draw(x,y) #描绘形状 a = Sprite.new a.bitmap = Bitmap.new(20,20) rect = a.bitmap.rect color = Color.new(255,255,255) a.bitmap.fill_rect(rect, color) a.x = x*20 a.y = y*20 end def dis_worm(x,y) b = @worm[[x,y]] b.bitmap.clear b.dispose @worm.delete([x,y]) @world.push([x,y]) end def dis_food b = @food[1] b.bitmap.clear b.dispose draw_food end def move case @key when 6 @head = [@balls[0][0]+1,@balls[0][1]] when 2 @head = [@balls[0][0],@balls[0][1]+1] when 4 @head = [@balls[0][0]-1,@balls[0][1]] when 8 @head = [@balls[0][0],@balls[0][1]-1] end if @head == @food[0] #吃到食物 $FENSHU_WORM+=9 score_refresh draw_worm(@head[0],@head[1]) dis_food elsif @balls.include?(@head) == true #碰到自己身体 finish elsif @head[0]<0 or @head[0]>@size or @head[1]<0 or @head[1]>@size #碰到边缘 finish else draw_worm(@head[0],@head[1]) @tail = @balls.pop dis_worm(@tail[0],@tail[1]) end wait(@speed) end def wait(n) for i in 0..n Graphics.update if i > n/2 end end def update if Input.trigger?(Input::C) @step = 1 end if Input.trigger?(Input::RIGHT) if @key == 4 @key = 4 else @key = 6 end end if Input.trigger?(Input::LEFT) if @key == 6 @key = 6 else @key = 4 end end if Input.trigger?(Input::DOWN) if @key == 8 @key = 8 else @key = 2 end end if Input.trigger?(Input::UP) if @key == 2 @key = 2 else @key = 8 end end end def draw_worm(x,y) #描绘虫 a = Sprite.new a.bitmap = Bitmap.new(20,20) rect = a.bitmap.rect color = Color.new(255,255,255) color2 = Color.new(155,155,155) a.bitmap.fill_rect(rect, color) a.bitmap.fill_rect(0,0,20,2,color2) a.bitmap.fill_rect(0,18,20,2,color2) a.bitmap.fill_rect(0,0,2,20,color2) a.bitmap.fill_rect(18,0,2,20,color2) a.x = x*20 + @ox a.y = y*20 + @oy @worm[[x,y]] = a @balls.unshift([x,y]) @world.delete([x,y]) end def draw_food #描绘食物 if @world.size == 0 finish end b = @world[rand(@world.size-1)] a = Sprite.new a.bitmap = Bitmap.new(20,20) rect = a.bitmap.rect color = Color.new(255,255,255) color2 = Color.new(155,155,155) a.bitmap.fill_rect(rect, color) a.bitmap.fill_rect(0,0,20,2,color2) a.bitmap.fill_rect(0,18,20,2,color2) a.bitmap.fill_rect(0,0,2,20,color2) a.bitmap.fill_rect(18,0,2,20,color2) a.x = b[0]*20 + @ox a.y = b[1]*20 + @oy @food = [b,a] end def finish @score2.dispose @score.dispose a = "您最后的得分是" b = $FENSHU_WORM.to_s c = "分" p a+b+c exit #如果不想游戏结束就退出请把这句##掉 end end #=========================== begin Font.default_name = (["黑体","宋体"]) a = Tanchishe.new a.main end
#==============================================================================
#
# ●贪吃蛇v1.0
# by 猫哥哥
#==============================================================================
class Tanchishe
def initialize
#生成棋盘
@size = 18
@world = []
for i in 1..@size
for j in 1..@size
@world.push([i,j])
end
end
@ox = 60 #原点
@oy = 40
draw_line((@ox/20)-1 ,(@oy/20)-1)
@worm = {}
@food = {}
@balls = []
@key = 6
draw_worm(4,8)
draw_worm(5,8)
draw_worm(6,8)
draw_worm(7,8)
draw_worm(8,8)
draw_worm(9,8)
draw_food
@speed = 5 #值越大,速度越慢
$FENSHU_WORM = 0
#分数
@score2 = Sprite.new
@score2.bitmap = Bitmap.new(200,20)
@score2.x = @ox +350
@score2.y = @oy +330
@score2.bitmap.draw_text(0, 0,200,20,"得分",1)
@score = Sprite.new
@score.bitmap = Bitmap.new(200,40)
@score.x = @ox +350
@score.y = @oy +350
score_refresh #刷新分数
@step = 0
end
def main
loop do
Graphics.update
Input.update
update
if @step == 1
move
end
end
end
def score_refresh
@score.bitmap.clear
@score.bitmap.draw_text(0, 20,200,20,$FENSHU_WORM.to_s,2 )
end
def draw_line(x,y) #描绘边缘
k = []
for i in 0..3
k.push(Sprite.new)
end
k[0].bitmap = Bitmap.new(380,10) #上
k[1].bitmap = Bitmap.new(380,10) #下
k[2].bitmap = Bitmap.new(10,400) #左
k[3].bitmap = Bitmap.new(10,400) #右
for kk in k
rect = kk.bitmap.rect
color = Color.new(205,205,205)
kk.bitmap.fill_rect(rect, color)
end
k[0].x = x*20 + 20
k[0].y = y*20 + 10
k[1].x = x*20 + 20
k[1].y = y*20 + 400
k[2].x = x*20 + 10
k[2].y = y*20 + 10
k[3].x = x*20 + 400
k[3].y = y*20 + 10
end
def draw(x,y) #描绘形状
a = Sprite.new
a.bitmap = Bitmap.new(20,20)
rect = a.bitmap.rect
color = Color.new(255,255,255)
a.bitmap.fill_rect(rect, color)
a.x = x*20
a.y = y*20
end
def dis_worm(x,y)
b = @worm[[x,y]]
b.bitmap.clear
b.dispose
@worm.delete([x,y])
@world.push([x,y])
end
def dis_food
b = @food[1]
b.bitmap.clear
b.dispose
draw_food
end
def move
case @key
when 6
@head = [@balls[0][0]+1,@balls[0][1]]
when 2
@head = [@balls[0][0],@balls[0][1]+1]
when 4
@head = [@balls[0][0]-1,@balls[0][1]]
when 8
@head = [@balls[0][0],@balls[0][1]-1]
end
if @head == @food[0] #吃到食物
$FENSHU_WORM+=9
score_refresh
draw_worm(@head[0],@head[1])
dis_food
elsif @balls.include?(@head) == true #碰到自己身体
finish
elsif @head[0]<0 or @head[0]>@size or @head[1]<0 or @head[1]>@size #碰到边缘
finish
else
draw_worm(@head[0],@head[1])
@tail = @balls.pop
dis_worm(@tail[0],@tail[1])
end
wait(@speed)
end
def wait(n)
for i in 0..n
Graphics.update if i > n/2
end
end
def update
if Input.trigger?(Input::C)
@step = 1
end
if Input.trigger?(Input::RIGHT)
if @key == 4
@key = 4
else
@key = 6
end
end
if Input.trigger?(Input::LEFT)
if @key == 6
@key = 6
else
@key = 4
end
end
if Input.trigger?(Input::DOWN)
if @key == 8
@key = 8
else
@key = 2
end
end
if Input.trigger?(Input::UP)
if @key == 2
@key = 2
else
@key = 8
end
end
end
def draw_worm(x,y) #描绘虫
a = Sprite.new
a.bitmap = Bitmap.new(20,20)
rect = a.bitmap.rect
color = Color.new(255,255,255)
color2 = Color.new(155,155,155)
a.bitmap.fill_rect(rect, color)
a.bitmap.fill_rect(0,0,20,2,color2)
a.bitmap.fill_rect(0,18,20,2,color2)
a.bitmap.fill_rect(0,0,2,20,color2)
a.bitmap.fill_rect(18,0,2,20,color2)
a.x = x*20 + @ox
a.y = y*20 + @oy
@worm[[x,y]] = a
@balls.unshift([x,y])
@world.delete([x,y])
end
def draw_food #描绘食物
if @world.size == 0
finish
end
b = @world[rand(@world.size-1)]
a = Sprite.new
a.bitmap = Bitmap.new(20,20)
rect = a.bitmap.rect
color = Color.new(255,255,255)
color2 = Color.new(155,155,155)
a.bitmap.fill_rect(rect, color)
a.bitmap.fill_rect(0,0,20,2,color2)
a.bitmap.fill_rect(0,18,20,2,color2)
a.bitmap.fill_rect(0,0,2,20,color2)
a.bitmap.fill_rect(18,0,2,20,color2)
a.x = b[0]*20 + @ox
a.y = b[1]*20 + @oy
@food = [b,a]
end
def finish
@score2.dispose
@score.dispose
a = "您最后的得分是"
b = $FENSHU_WORM.to_s
c = "分"
p a+b+c
exit #如果不想游戏结束就退出请把这句##掉
end
end
#===========================
begin
Font.default_name = (["黑体","宋体"])
a = Tanchishe.new
a.main
end
这个脚本如何更改贪吃蛇外观和被吃东西的外观呢 |
|