Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sat Oct 25, 2014 4:26 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Slider Puzzle
PostPosted: Sat Mar 12, 2011 10:01 am 
Offline

Joined: Sat Nov 06, 2010 8:26 pm
Posts: 4
Hi,

I have a couple questions. I'm trying to make a slider puzzle, you know the one with the pictures, but i cant seem to figure out how to make the canvases shuffle around. My questions are.

- Should i use canvases, or is there a way to make pushbuttons accept graphics?
- If I should have to use canvases then how do i make them into a(n) Control Array?
-- Everytime i do and i put the code "g.drawgraphics mypic,0,0,80,80,0,0,80,80" it puts it in everysingle canvas in the array.

Finally...
- How do i shuffle them?
-- I've tried everything from making the array to selecting a random number and moving it.

Can anyone please help me. :|


Top
 Profile  
 
 Post subject: Re: Slider Puzzle
PostPosted: Sat Mar 12, 2011 11:53 am 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
first off... do not use an array of canvas.... use ONE canvas

use a simple 4 x 4 array to keep track of where the "pieces" are....

and in the Paint Event of your single canvas..... redraw the current state of the puzzle.

Use the MouseDown/Up/Move/Drag events to determine what element of your array to "move"
and on the MouseUP .... fire off the Paint event again

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
 
 Post subject: Re: Slider Puzzle
PostPosted: Sat Mar 12, 2011 12:36 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
ok... I was intrigued.......
so here it is ... not too bad for 20 min work :)

create a window with two controls
CANVAS1 (400 x 400)
PUSHBUTTON1 - Label it "New Puzzle"

Global Properties
Dim PUZZLE(3,3) as INTEGER
dim RND_NUM as RANDOM
dim SQUARE_SIZE as integer


Global CONSTANT
puzzle_size=3 ' this should match the dimensions of PUZZLE()


Global Method - NAME = NEW_PUZZLE
dim i as integer
dim x as integer
dim y as integer
dim cnt as integer
cnt=((puzzle_size+1)*(puzzle_size+1))-1
for x=0 to puzzle_size
for y=0 to puzzle_size
puzzle(x,y)=0 ' empty square
next y
next x
x=rnd_num.InRange(0,puzzle_size)
y=rnd_num.InRange(0,puzzle_size)
for i=0 to cnt
while puzzle(x,y)>0
x=rnd_num.InRange(0,puzzle_size)
y=rnd_num.InRange(0,puzzle_size)
wend
puzzle(x,y)=i
next i
square_size=(canvas1.width\(puzzle_size+1))
Canvas1.refresh


WINDOW1.OPEN EVENT
rnd_num=new random
New_Puzzle


PUSHBUTTON1.ACTION EVENT
New_Puzzle



CANVA1.PAINT EVENT
dim x as integer
dim y as integer
dim ypos as integer
dim xpos as integer

dim s as string
g.ForeColor=&c00ff00
g.fillrect 0,0,g.width,g.height
g.textsize=72

for x=0 to puzzle_size
xpos=(x*square_size)
for y=0 to puzzle_size
ypos=(y*square_size)
if puzzle(x,y)>0 then ' draw the piece
g.ForeColor=&cffffff
g.fillrect xpos,ypos,square_size,square_size
g.ForeColor=&c0000ff
g.drawrect xpos,ypos,square_size,square_size
s=str(puzzle(x,y))
g.drawstring s,xpos+((square_size-g.StringWidth(s))\2),ypos+((square_size-g.textheight)\2)+g.TextAscent
end if
next y
next x
g.ForeColor=&c000000
g.drawrect 0,0,g.width,g.height


CANVAS1.MOUSEDOWN EVENT
dim good_move as boolean
x=x\square_size
y=y\Square_Size
good_move=false
if puzzle(x,y)>0 then ' can't move the empty square
' check if move up
if y>0 and not good_move then
if puzzle(x,y-1)=0 then
puzzle(x,y-1)=puzzle(x,y)
puzzle(x,y)=0
good_move=true
end if
end if
' check if move down
if y<puzzle_size and not good_move then
if puzzle(x,y+1)=0 then
puzzle(x,y+1)=puzzle(x,y)
puzzle(x,y)=0
good_move=true
end if
end if
' check if move left
if x>0 and not good_move then
if puzzle(x-1,y)=0 then
puzzle(x-1,y)=puzzle(x,y)
puzzle(x,y)=0
good_move=true
end if
end if

' check if move right
if x<puzzle_size and not good_move then
if puzzle(x+1,y)=0 then
puzzle(x+1,y)=puzzle(x,y)
puzzle(x,y)=0
good_move=true
end if
end if
end if
if not good_move then
beep
return true
end if
canvas1.refresh

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
 
 Post subject: Re: Slider Puzzle
PostPosted: Sat Mar 12, 2011 1:05 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
and if you want to use a PICTURE....

add a graphic image to the IDE the exact same size as CANVAS1 and name it PUZZLE_PIC

change PAINT EVENT to this
dim x as integer
dim y as integer
dim ypos as integer
dim xpos as integer
dim s as string
dim xpic as integer
dim ypic as integer
const number_puzzle=false
g.ForeColor=&c00ff00
g.fillrect 0,0,g.width,g.height
g.textsize=72

for x=0 to puzzle_size
xpos=(x*square_size)
for y=0 to puzzle_size
ypos=(y*square_size)
if puzzle(x,y)>0 then ' draw the piece
if number_puzzle then
g.ForeColor=&cffffff
g.fillrect xpos,ypos,square_size,square_size
g.ForeColor=&c0000ff
g.drawrect xpos,ypos,square_size,square_size
s=str(puzzle(x,y))
g.drawstring s,xpos+((square_size-g.StringWidth(s))\2),ypos+((square_size-g.textheight)\2)+g.TextAscent
else
ypic=(puzzle(x,y)-1)\(puzzle_size+1)
xpic=(puzzle(x,y)-1)-(ypic*(puzzle_size+1))
xpic=xpic*Square_Size
ypic=ypic*Square_Size
g.drawpicture puzzle_pic,xpos,ypos,Square_Size,Square_Size,xpic,ypic,Square_Size,Square_Size
end if
end if
next y
next x
g.ForeColor=&c000000
g.drawrect 0,0,g.width,g.height

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
 
 Post subject: Re: Slider Puzzle
PostPosted: Sat Mar 12, 2011 2:54 pm 
Offline

Joined: Sat Nov 06, 2010 8:26 pm
Posts: 4
When you say "Global" Do you mean a module or do you mean put it in the window?


Top
 Profile  
 
 Post subject: Re: Slider Puzzle
PostPosted: Mon Jul 04, 2011 5:59 pm 
Offline

Joined: Fri Sep 30, 2005 11:19 am
Posts: 275
Dantez wrote:
When you say "Global" Do you mean a module or do you mean put it in the window?


It doesn't matter, or rather, from what I can read from his code, you can either put it in the window or in a module.

_________________
"If you look carefully at my lips, you'll realize that I'm actually saying something else."
Brainswitch Machina - REALStudio/REALbasic
RBScriptExtender - give RBScript power to work with any class


Top
 Profile  
 
 Post subject: Re: Slider Puzzle
PostPosted: Tue Aug 16, 2011 5:12 am 
Offline

Joined: Wed Nov 30, 2005 8:22 pm
Posts: 227
Location: L.I., New York
Wow that code works really good.


Top
 Profile  
 
 Post subject: Re: Slider Puzzle
PostPosted: Wed Nov 16, 2011 4:12 am 
Offline
User avatar

Joined: Wed Oct 06, 2010 10:25 am
Posts: 154
Location: ---
Thanks to Dave Simeons I realized the Puzzle game.
If you want to test the Dave's code you can directly download the project From
here:
http://www.eudosia.com/downloads/download.php?download=15
Image

_________________
Massimiliano Fabbri
Freelance Senior Programmer
(Eudosia Systems)
Visual Basic 6 - RealStudio - Mobile Development
Php5/Mysql - Dot Net C# - Objective C
-------------------------------------------
http://www.eudosia-systems.it


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group