Retrogamecoding(.org) > Examples

8erMagicSquare

(1/1)

Peter:
Ok, here is the game.

You win:
123
456
78

just click on a number.

--- Code: ---screen(192,240,0,"8erMagic")
--'setframetimer(60)
backcolor(0,0,255)

function Sprite(id,x1,y1,w,h,fx)
   drawimage(w*fx,0,w,h,x1,y1,id)
end

function Rand(minZahl,maxZahl)
   maxZahl= (maxZahl-minZahl)
   return int(rnd()*maxZahl+minZahl)
end

xPos=0;yPos=0;x=0;y=0;j=0;n=0;m=0;num=0;a=0;i=0;jx=1
moves=0;clic=0;won=0;win=0;klic=0
Map ={}
Buf ={}

for j=1,28 do
    Map[j]=255
    Buf[j]=0
end

num = loadimage("mmedia/number.png")
win = loadsound("mmedia/won.wav")
klic= loadsound("mmedia/klic.wav")

function CheckWon()
   if won==1 then
      return 0
   end
   local jx=1
   for y=0,2 do
      for x=0,2 do
         j=(y*3+x)+1
         if Map[j] ~= jx then
            return 0
  end
         if jx==8 then
            setcaption "WON!"
            won=1
            PlaySound(win,128,1,0)
            return 0
         end
        jx=jx+1
      end
   end
end

function PushNumber()
    if mouseb()==1 and won==0 and clic==0 then
       xPos= math.floor(mousex()/64)
       yPos= math.floor(mousey()/64)
       j=(yPos*3+xPos)+1
       if Map[j]==0 or yPos >2 then
  return 0
       end
       if Map[j+1]==0 and clic==0 then
          Map[j+1] = Map[j]
          Map[j]=0
          moves = moves +1
          clic =1
          playsound(klic,128,2,0)
       elseif  Map[j-1]==0 and xPos >0 and clic==0 then
          Map[j-1] = Map[j]
          Map[j]=0
          moves = moves +1
          clic =1
          playsound(klic,128,2,0)
       elseif Map[j-3]==0 and yPos >0 and clic==0 then
          Map[j-3] = Map[j]
          Map[j]=0
          moves = moves +1
          clic =1
          playsound(klic,128,2,0)
       elseif Map[j+3]==0 and clic==0 then
          Map[j+3] = Map[j]
          Map[j]=0
          moves = moves +1
          clic =1
          playsound(klic,128,2,0)
       end
    end
end

function ShowMap()
    for y=0,2 do
      for x=0,2 do
        j=(y*3+x)+1
        if Map[j]==9 then
           Map[j] =0
        elseif Map[j] >=1 and Map[j] <=8 then
           Sprite(num,x*64,y*64,64,64,Map[j]-1)
        end
      end
    end
end

function CheckBuffer()
   a = Rand(1,9)
   for m=1,9 do
      if Buf[m]==a then
         return 0
      end
   end
   Buf[n]=a
   n = n+1
end

function SetNumber()
   n=0
   while n<10 do
     CheckBuffer()
   end
   for y=0,2 do
      for x=0,2 do
         j=(y*3+x)+1
         Map[j] = Buf[j]
      end
   end
end

function NewGame()
   if mouseb()==1 and clic==0 then
      if int(mousex()/18) >=3 and int(mousex()/18) <=5 and int(mousey()/18)==11 then
         moves =0
         won =0
         clic =1
         for i=0,9 do
            Buf[i]=0
         end
         for y=0,2 do
            for x=0,2 do
               j=(y*3+x)+1
               Map[j] =0
            end
         end
         SetNumber()
      end
   end
end

SetNumber()
repeat
k=getkey()
cls()
ShowMap()
NewGame()
PushNumber()
CheckWon()
color(0,255,0)
line(0,193,192,193)
color(255,255,255)
drawtext(62,198,"NEW")
drawtext(16,218,"MOVES ".. moves)
sync()
if mouseb()==0 then
   clic=0
end
until k==27
closewindow()

--- End code ---

Navigation

[0] Message Index

Go to full version