【脚本恢复】猫哥哥·贪吃蛇
作者:猫哥哥如果作者认为此举有任何不妥,PM我,我亲撤下!
RP爆发!
今天晚上想试写一下贪吃蛇,没想到很容易就写出来了……不知道有没有bug,欢迎大家debug
预览图一张:
http://rpg.blue/UP_PIC/200710/猫哥哥_贪吃蛇.PNG#==============================================================================## ●贪吃蛇v1.0# by 猫哥哥# #============================================================================== class Tanchishedef initialize #生成棋盘 @size = 18 @world = [] for i in 1..@size for j in 1..@size @world.push() 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.bitmap = Bitmap.new(380,10) #上 k.bitmap = Bitmap.new(380,10) #下 k.bitmap = Bitmap.new(10,400) #左 k.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.x = x*20 + 20 k.y = y*20 + 10 k.x = x*20 + 20 k.y = y*20 + 400 k.x = x*20 + 10 k.y = y*20 + 10 k.x = x*20 + 400 k.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[] b.bitmap.clear b.dispose @worm.delete() @world.push() end def dis_food b = @food b.bitmap.clear b.dispose draw_food end def move case @key when 6 @head = [@balls+1,@balls] when 2 @head = [@balls,@balls+1] when 4 @head = [@balls-1,@balls] when 8 @head = [@balls,@balls-1] end if @head == @food #吃到食物 $FENSHU_WORM+=9 score_refresh draw_worm(@head,@head) dis_food elsif @balls.include?(@head) == true #碰到自己身体 finish elsif @head@size or @head@size #碰到边缘 finish else draw_worm(@head,@head) @tail = @balls.pop dis_worm(@tail,@tail) 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[] = a @balls.unshift() @world.delete() end def draw_food #描绘食物 if @world.size == 0 finish end b = @world 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*20 + @ox a.y = b*20 + @oy @food = end def finish @score2.dispose @score.dispose a = "您最后的得分是" b = $FENSHU_WORM.to_s c = "分" p a+b+c exit #如果不想游戏结束就退出请把这句##掉 endend#=========================== begin Font.default_name = (["黑体","宋体"]) a = Tanchishe.new a.main end复制代码
本帖来自P1论坛作者yangff,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=68618若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。
页:
[1]