Box Problem

New to FreeBASIC? Post your questions here.
Post Reply
TurtleProgrammer
Posts: 37
Joined: Jan 26, 2017 7:54

Box Problem

Post by TurtleProgrammer »

This is probably an easy fix and one if not the dumbest question ever asked on this forum, but I am tired and most likely not thinking staight. But I have written this code to draw 25 boxes along the top of the screen but it only draws the first two.

Code: Select all

screenres 320,640

dim as integer pixel1, pixel2, pixel3, pixel4

pixel1 = 1
pixel2 = 1
pixel3 = 25
pixel4 = 25

do until pixel2 = 25
    
for pixel1 = 0 to 24
    for pixel2 = 0 to 24
        for pixel3 = 0 to 24
            for pixel4 = 0 to 24
                
line (pixel1,pixel2)-(pixel3,pixel4),15,bf
pixel1 =+ 1
pixel2 =+ 1
pixel3 = 25
pixel4 = 25
line (pixel1, pixel2)-(pixel3, pixel4),12, bf


next
next
next
next

loop


sleep
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Box Problem

Post by MrSwiss »

Have a look at the link below ...
Simple FRAME Type
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Box Problem

Post by BasicCoder2 »

Code: Select all

screenres 640,480

dim as integer pixel(1 to 4)

for i as integer = 1 to 4
    line (i*30,0)-(i*30+24,24),15,bf   'draws a white square 25x25 pixels in steps of 30 apart
next i

sleep
For a screen objects like a box it helps if you define it as a TYPE and add routines to use the type such as to display it.

Objects like a BOX type are easier to process as a list.

Code: Select all

screenres 640,480,32  '32 bit color
color rgb(0,0,0),rgb(255,255,255):cls

type BOX
    as integer x
    as integer y
    as integer w
    as integer h
    as ulong   c
end type

sub drawBox(b as BOX)
    line (b.x,b.y)-(b.x+b.w,b.y+b.h),b.c,bf
    line (b.x,b.y)-(b.x+b.w,b.y+b.h),rgb(0,0,0),b  'give it a black border
end sub

dim as BOX boxes(1 to 4)  'create a list of boxes

for i as integer = 1 to 4
    boxes(i).x = 10+i*30  'move position for each box 30 pixels to right
    boxes(i).y = 10
    boxes(i).w = 25
    boxes(i).h = 25
next i

'give each box a color
boxes(1).c = rgb(0,255,0)  'green
boxes(2).c = rgb(200,100,0)  'orange
boxes(3).c = rgb(0,0,255)  'blue
boxes(4).c = rgb(255,0,0)  'red

for i as integer = 1 to 4
    drawBox(boxes(i))
next i

sleep

BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Box Problem

Post by BasicCoder2 »

@TurtleProgrammer,
You seem to be wanting to write a particular application although as you provide no feedback so who would know.
Earlier you wanted to know how to drag an image. Here you can drag an image of a red disc.
Notice how by moving the code into subroutines and functions that return a value you end up with reusable code and a short readable MAIN.

Code: Select all

screenres 640,480,32  '32 bit color
color rgb(0,0,0),rgb(255,255,255):cls
dim shared as integer mx,my,mb,ox,oy,xd,yd   'mouse coordinates and button state

type BMIMAGE
    as integer x
    as integer y
    as integer w
    as integer h
    as any ptr img   'pointer to bitmap image
end type

type BOX
    as integer x
    as integer y
    as integer w
    as integer h
    as ulong   c
end type

sub drawBox(b as BOX)
    line (b.x,b.y)-(b.x+b.w,b.y+b.h),b.c,bf
    line (b.x,b.y)-(b.x+b.w,b.y+b.h),rgb(0,0,0),b  'give it a black border
end sub

dim shared as BOX boxes(1 to 4)  'create a list of boxes

for i as integer = 1 to 4
    boxes(i).x = 10+i*60  'move position for each box 60 pixels to right
    boxes(i).y = 10
    boxes(i).w = 50
    boxes(i).h = 50
next i

'give each box a color
boxes(1).c = rgb(0,255,0)  'green
boxes(2).c = rgb(200,100,0)  'orange
boxes(3).c = rgb(0,0,255)  'blue
boxes(4).c = rgb(255,0,0)  'red

sub displayBoxes()
    for i as integer = 1 to 4
        drawBox(boxes(i))
    next i
end sub

dim shared as any ptr image1
image1 = imagecreate(50,50)
line image1,(0,0)-(49,49),rgb(100,50,200),bf  'back ground color
circle image1,(25,25),24,rgb(255,0,0),,,,f    'draw red disc on image1

dim shared as BMIMAGE redDisc
redDisc.x = 100
redDisc.y = 100
redDisc.w = 50
redDisc.h = 50
redDisc.img = image1

sub update()
    screenlock
    cls
    displayBoxes()
    put (redDisc.x,redDisc.y),image1,trans
    screenunlock
end sub

update()

function mouseOn(pic as BMIMAGE) as boolean
    getmouse mx,my,,mb
    return mb=1 and mx>pic.x and mx<pic.x+pic.w and my>pic.y and my<pic.y+pic.h
end function

sub dragImage(pic as BMIMAGE)
    ox = mx  'save mouse position
    oy = my
    while mb=1
        getmouse mx,my,,mb
        if mx<>ox or my<>oy then  'mouse had moved
            xd = mx-ox  ''change in position of mouse
            yd = my-oy
            pic.x = pic.x + xd  'add that to image position
            pic.y = pic.y + yd
            ox = mx  'save latest mouse position
            oy = my
            update   'show change
        end if
        sleep 2
    wend
end sub

'MAIN PROGRAM

do
    
    if mouseOn(redDisc) then
        dragImage(redDisc)
    end if

    sleep 2
           
loop until multikey(&H01)
TurtleProgrammer
Posts: 37
Joined: Jan 26, 2017 7:54

Re: Box Problem

Post by TurtleProgrammer »

I am trying to write a card game but don't really want to give out the source code right now unless I absolutely need help. I am just wary of other people taking my ideas.
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Box Problem

Post by BasicCoder2 »

So it is an original card game?
There may be other sources you might find helpful.
http://www.freebasic.net/forum/viewtopi ... ard+images
.
badidea
Posts: 2594
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Box Problem

Post by badidea »

Your code does not make any sense to me.

I see inside a do-loop 4 nested for-loops in which two rectangles are drawn (and pixel variables manipulation).

I would expect 2 * 25^4 rectangles, but the 'manual' pixel variable manipulation might prevent that. Hard to follow.

Looks like a very complicated solution for a simple task.

Is this what you want?

Code: Select all

screenres 320,640

dim as integer boxWidth = 20, boxHeigth = 20

for i as integer = 0 to (320 \ boxWidth) - 1
	line (i * boxWidth, 0)-step(boxWidth-1, boxHeigth-1), 15, bf
	line (i * boxWidth, 0)-step(boxWidth-1, boxHeigth-1), 12, b
next

sleep
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Box Problem

Post by leopardpm »

TurtleProgrammer wrote:I am trying to write a card game but don't really want to give out the source code right now unless I absolutely need help. I am just wary of other people taking my ideas.
Turtle, although you may indeed have some excellent and original ideas for a program, you don't have to worry about these folks 'stealing' anything, they have done 'it all' and can provide you with excellent help if you give them the required information. They have the skills to make your program, but those are the skills they teach to you.

In fact, I have 'stolen' more great ideas and routines, not to mention learning a great deal about many different aspects of programming, from these experts than I deserve - be open, be honest, and you will learn much.

On the other hand, if you want to keep things private, tis no problem either!
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Box Problem

Post by thesanman112 »

The actual problem with your code is you have to add spacing between the boxs....you code is drawing the bixs on top of each other.....
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Box Problem

Post by thesanman112 »

Also.....your forcing an exit of the loop because your adding 25 to the actual value thats suppose to be added by the loop.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Box Problem

Post by Tourist Trap »

It's probably the simplest in the long term to get used with something like FLTK:
http://www.freebasic.net/forum/viewtopi ... 2A#p193776

I don't use this kind of library because I like learning the how-to from the start, but for what I understand here, the card game is more an affair of gameplay than original routines to draw boxes, so why not letting the dirty job be done with the help of a library?
Post Reply