# Ruby/pacman.rb at j2402 - JD2024student/Ruby
JD-2024 Students' repository

Fork: 0
JD2024student / Ruby
Transfer to URL with SHA Find file
Newer

Older
Ruby / pacman.rb
AKIBA Hiroki 2 days ago 4 KB 2024-10-10 20:34:26
Raw Blame History

    #!/usr/bin/env ruby#-*- coding:utf-8 -*-require 'io/console'require 'csv'def hoge  firld = CSV.read("course1.csv")  stage1 = {    "0" => "\e[37m・",    "1" => "\e[34m@@",    "2" => "  ",    "3" => "\e[33m●",    "4" => "\e[31m●",    "5" => "\e[33m○"  }  stage2 = {    "0" => "\e[37m・",    "1" => "\e[34m##",    "2" => "  ",    "3" => "\e[33m:D",    "4" => "\e[31m●",    "5" => "\e[33m○"  }   def makescreen(map, code)    print("\e[2J\e[1;1H")    map.each do |row|      row.each do |column|        print code[column]      end      puts    end    print("\e[m")  end   a=0  y=11  x=7  firld[y][x]="3"   ey=5  ex=7  b=firld[ey][ex]  firld[ey][ex]="4"  makescreen(firld, stage1)  #makescreen(firld, stage2)  printf("score:%d",a)  while true        if (key=STDIN.getch)=="\C-c"      break    end    if key=="\e"&&STDIN.getch=="["      key=STDIN.getch    end    if key=="A"#↑            if firld[y-1][x]=="1"        redo       elsif firld[y-1][x]=="0"        firld[y][x]="2"        y-=1        firld[y][x]="3"        a+=100        if y==-1&&x==7          y=14        end      elsif firld[y-1][x]=="2"        firld[y][x]="2"        y-=1        firld[y][x]="3"        if y==-1&&x==7          y=14        end      end          elsif key=="B"#↓            if firld[y+1]==nil        firld[y][x]="2"        y=0        firld[y][x]="3"      elsif firld[y+1][x]=="1"        redo      elsif firld[y+1][x]=="0"        firld[y][x]="2"        y+=1        firld[y][x]="3"        a+=100      elsif firld[y+1][x]=="2"        firld[y][x]="2"        y+=1        firld[y][x]="3"      end          elsif key=="C"#→            if firld[y][x+1]==nil        firld[y][x]="2"        x=0        firld[y][x]="3"      elsif firld[y][x+1]=="1"        redo      elsif firld[y][x+1]=="0"        firld[y][x]="2"        x+=1        firld[y][x]="3"        a+=100      elsif firld[y][x+1]=="2"        firld[y][x]="2"        x+=1        firld[y][x]="3"      end          elsif key=="D"#←            if firld[y][x-1]=="1"        redo      elsif firld[y][x-1]=="0"        firld[y][x]="2"        x-=1        firld[y][x]="3"        a+=100        if y==7&&x==-1          x=14        end      elsif firld[y][x-1]=="2"        firld[y][x]="2"        x-=1        firld[y][x]="3"        if y==7&&x==-1          x=14        end      end    end    makescreen(firld, stage1)    #makescreen(firld, stage2)    printf("score:%d",a)    c=0    while true      if c>1        sleep(0.5)        firld[y][x]="5"        makescreen(firld, stage1)        #makescreen(firld, stage2)        sleep(0.9)        print("\e[5mgameover!")        return      end      srand()      enemy=rand(4)      if enemy==0 #↑                if firld[ey-1][ex]=="1"          #壁なので進まない=何もしない          redo        elsif firld[ey-1][ex]=="0"          firld[ey][ex]=b          ey-=1          b=firld[ey][ex]          firld[ey][ex]="4"          break        elsif firld[ey-1][ex]=="2"          firld[ey][ex]=b          ey-=1          b=firld[ey][ex]          firld[ey][ex]="4"          break        end              elsif enemy==1 #↓                if firld[ey+1]==nil          firld[ey][ex]=b          ey=0          b=firld[ey][ex]          firld[ey][ex]="4"          break        elsif firld[ey+1][ex]=="1"          #          redo        elsif firld[ey+1][ex]=="0"          firld[ey][ex]=b          ey+=1          b=firld[ey][ex]          firld[ey][ex]="4"          break        elsif firld[ey+1][ex]=="2"          firld[ey][ex]=b          ey+=1          b=firld[ey][ex]          firld[ey][ex]="4"          break        end              elsif enemy==2 #→                if firld[ey][ex+1]==nil          firld[ey][ex]=b          ex=0          b=firld[ey][ex]          firld[ey][ex]="4"          break        elsif firld[ey][ex+1]=="1"          #          redo        elsif firld[ey][ex+1]=="0"          firld[ey][ex]=b          ex+=1          b=firld[ey][ex]          firld[ey][ex]="4"          break        elsif firld[ey][ex+1]=="2"          firld[ey][ex]=b          ex+=1          b=firld[ey][ex]          firld[ey][ex]="4"          break        end              elsif enemy==3 #←                if firld[ey][ex-1]=="1"          #          redo        elsif firld[ey][ex-1]=="0"          firld[ey][ex]=b          ex-=1          b=firld[ey][ex]          firld[ey][ex]="4"          break        elsif firld[ey][ex-1]=="2"          firld[ey][ex]=b          ex-=1          b=firld[ey][ex]          firld[ey][ex]="4"          break        end      end      c+=1    end        makescreen(firld, stage1)    #makescreen(firld, stage2)    printf("score:%d\n",a)    if a==10000      print("\e[5mgameclear!")      return    end  endendhoge