×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How do I move a graphic ?

How do I move a graphic ?

How do I move a graphic ?

(OP)
I want to put a graphic on the screen and move it left and right using the 'm' and 'n' key (like a ship in space invaders for example).  Why doesn't the attached code work (its horribly slow and jerky)? Is there a better way of doing it?  Also, how do I load a graphic from elsewhere into my program (say one created by PAINT) rather than draw it in the program itself (like below).  Thanks.

SCREEN 1
DIM box%(1 TO 200)
x1% = 0: x2% = 10: y1% = 0: y2% = 10
LINE (x1%, y1%)-(x2%, y2%), 2, BF
GET (x1%, y1%)-(x2%, y2%), box%
x1% = 100: y1% = 100
DO
  IF INKEY$ = CHR$(109) THEN
     x1% = x1% + 10
  END IF
  IF INKEY$ = CHR$(110) THEN
     x1% = x1% - 10
  END IF
  PUT (x1%, y1%), box%, XOR
LOOP UNTIL INKEY$ = CHR$(27)

RE: How do I move a graphic ?

How to get something from paint:  First you have to convert it from .bmp to a BSAVE format.  Here is a program to do that.  I'm not sure who wrote it, but I didn't

'DYNAMIC$
CLS
filename$ = "c:\example.bmp"
OPEN filename$ FOR BINARY AS #1
header$ = SPACE$(14)
sizing$ = SPACE$(4)
GET #1, 1, header$
GET #1, 15, sizing$
bmpinfosize = CVI(sizing$)
'bmpinfosize - Is the size of the information header for the bitmap.
'              Different bitmap versions have variations in filetypes.
'              40 is a standard windows 3.1 bitmap.
'              12 is for OS/2 bitmaps
'The next routine reads in the appropriate headers and colour tables.
'nbits is the number of bits per pixel - i.e. number of colours
'1 bit = 2 colours, 4 bits = 16 colours, 8 bits = 256 colours, etc.
'the 24 bit mode does not have a palette, its colours are expressed as
'image data

'Design of a windows 3.1 bitmap - Taken from bmp.zip on the
'x2ftp.oulu.fi ftp site under /pub/msdos/programming/formats
'Specifications for a Windows 3.1 bitmap. (.BMP)
'Email any questions/responses to me at zabudsk@ecf.utoronto.ca
'or post to alt.lang.basic or comp.lang.basic.misc.

'       | # of   |
'Offset | bytes  | Function (value)
'-------+--------+--- General Picture information starts here---------
'  0    |   2    | (BM) - Tells us that the picture is in bmp format
'  2    |   4    | Size of the file (without header?)
'  6    |   2    | (0) Reserved1 - Must be zero
'  8    |   2    | (0) Reserved2 - Must be zero
'  10   |   4    | Number of bytes offset of the picture data
'-------+--------+--- Information Header starts here -----------------
'  14   |   4    | (40/12) Size of information header (Win3.1/OS2)
'  18   |   4    | Picture width in pixels
'  22   |   4    | Picture Height in pixels
'  26   |   2    | (1) Number of planes, must be 1
'  28   |   2    | Number of bits per pixel (bpp), must be 1,4,8 or 24
'  30   |   4    | (0) Compression - 0 means no compression, 1,2 are RLEs
'  34   |   4    | Image size in bytes
'  38   |   4    | picture width in pels per metre
'  42   |   4    | picture height in pels per metre
'  46   |   4    | (0) Number of colours used in the picture, 0 means all
'  50   |   4    | (0) Number of important colours, 0 means all
'-------+--------+--- Palette data starts here -----------------------
'  54   |   1    | (b) - blue intensity component, color 0 - range 0 to 255
'  55   |   1    | (g) - green intensity component, color 0 - range 0 to 255
'  56   |   1    | (r) - red intensity component, color 0 - range 0 to 255
'  57   |   1    | (0) - unused
'  58   |   1    | (b) - blue intensity component, color 0 - range 0 to 255
'  ...  | ...    |
'  54   | 4*2^bpp| total range of palette
'-------+--------+--- Image data starts here -------------------------
'54+    | width* | Bitmap data starting at lower left portion of the
'(4*2^n)| height*| image moving from left towards right. Moving up 1
'       | (8/bpp)| pixel when at the right hand side of the image, starting
'       |        | from the left side again, until the top right of the
'       |        | image is reached

'Note that this format is slightly different for a OS/2 Bitmap.
'The header is the same up to (but not including) bit 30-
'The palette colour values follow at bit 30, with the form...
'1 byte blue intensity
'1 byte green intensity
'1 byte red intensity
'For each colour of the picture.
'Bitmapped image data follows the colour tables


'Special note: When storing 1 bit (2 colour) pictures.
'8 horizontal pixels are packed into 1 byte. Each bit determines
'the colour of one pixel (colour 0 or colour 1)

'4 bit pictures (16 colours) use 2 nibbles (4 bits) for each pixel
'thus there are 2 pixels for each byte of image data.

'8 bit pictures use 1 byte per pixel. Each byte of image data
'represents one of 256 colours.

'24 bit pictures express colour values by using 3 bytes and each has a
'value between 0 and 255. The first byte is for red, the second is for
'green and the third is for blue. Thus (256)^3 or 2^24 of 16777216 different
'colours.

IF bmpinfosize = 12 THEN
   infoheader$ = SPACE$(12)
   GET #1, 15, infoheader$
   nbits = CVI(MID$(infoheader$, 15, 4))
  
   IF nbits = 1 THEN
      palet$ = SPACE$(6)
      GET #1, bmpinfosize + 15, palet$
   ELSEIF nbits = 4 THEN
      palet$ = SPACE$(48)
      GET #1, bmpinfosize + 15, palet$
   ELSEIF nbits = 8 THEN
      palet$ = SPACE$(768)
      GET #1, bmpinfosize + 15, palet$
   END IF
ELSEIF bmpinfosize = 40 THEN
   infoheader$ = SPACE$(40)
   GET #1, 15, infoheader$
   nbits = CVI(MID$(infoheader$, 15, 4))
   IF nbits = 1 THEN
      palet$ = SPACE$(8)
      GET #1, bmpinfosize + 15, palet$
   ELSEIF nbits = 4 THEN
      palet$ = SPACE$(64)
      GET #1, bmpinfosize + 15, palet$
   ELSEIF nbits = 8 THEN
      palet$ = SPACE$(1024)
      GET #1, bmpinfosize + 15, palet$
   END IF
END IF
     

ft$ = MID$(header$, 1, 2)
'PRINT "Type of file (Should be BM): "; ft$

filesize = CVL(MID$(header$, 3, 4))
'PRINT "Size of file: "; filesize

r1 = CVI(MID$(header$, 7, 2))
'PRINT "Reserved 1: "; r1

r2 = CVI(MID$(header$, 9, 2))
'PRINT "Reserved 2: "; r2

offset = CVL(MID$(header$, 11, 4))
'PRINT "Number of bytes offset from beginning: "; offset

'PRINT

headersize = CVL(MID$(infoheader$, 1, 4))
'PRINT "Size of header: "; headersize

picwidth = CVL(MID$(infoheader$, 5, 4))
'PRINT "Width: "; picwidth
'
picheight = CVL(MID$(infoheader$, 9, 4))
'PRINT "Height: "; picheight

nplanes = CVI(MID$(infoheader$, 13, 4))
'PRINT "Planes: "; nplanes

'PRINT "Bits per plane: "; nbits

'PRINT

IF headersize = 40 THEN
'   PRINT "Compression: ";
   comptype = CVL(MID$(infoheader$, 17, 4))
   IF comptype = 0 THEN PRINT "None"
   IF comptype = 1 THEN PRINT "Run Length - 8 Bits"
   IF comptype = 2 THEN PRINT "Run Length - 4 Bits"
  
   imagesize = CVL(MID$(infoheader$, 21, 4))
'   PRINT "Image Size (bytes): "; imagesize
  
   xsize = CVL(MID$(infoheader$, 25, 4))
'   PRINT "X size (pixels per metre): "; xsize
  
   ysize = CVL(MID$(infoheader$, 29, 4))
'   PRINT "Y size (pixels per metre): "; ysize
  
   colorsused = CVL(MID$(infoheader$, 33, 4))
'   PRINT "Number of colours used: "; colorsused
  
   neededcolours = CVL(MID$(infoheader$, 37, 4))
'   PRINT "Number of important colours: "; neededcolours
END IF
'PRINT
'PRINT "Press Any key to continue."
'WHILE INKEY$ = ""
'WEND

IF nbits = 1 THEN
   SCREEN 11
ELSEIF nbits = 4 THEN
   SCREEN 12
ELSEIF nbits = 8 OR nbits = 24 THEN
   SCREEN 13
END IF
IF bmpinfosize = 40 THEN ngroups = 4
IF bmpinfosize = 12 THEN ngroups = 3

IF nbits = 24 THEN
   IF ngroups = 3 THEN
      FOR c = 0 TO 63
         d = c * 4
         palet$ = palet$ + CHR$(d) + CHR$(d) + CHR$(d)
         palet$ = palet$ + CHR$(d) + CHR$(d) + CHR$(d + 1)
         palet$ = palet$ + CHR$(d) + CHR$(d + 1) + CHR$(d)
         palet$ = palet$ + CHR$(d + 1) + CHR$(d) + CHR$(d)
      NEXT c
   ELSEIF ngroups = 4 THEN
      FOR c = 0 TO 63
         d = c * 4
         palet$ = palet$ + CHR$(d) + CHR$(d) + CHR$(d) + CHR$(0)
         palet$ = palet$ + CHR$(d) + CHR$(d) + CHR$(d + 1) + CHR$(0)
         palet$ = palet$ + CHR$(d) + CHR$(d + 1) + CHR$(d) + CHR$(0)
         palet$ = palet$ + CHR$(d + 1) + CHR$(d) + CHR$(d) + CHR$(0)
      NEXT c
   END IF
END IF

FOR x = 1 TO LEN(palet$) STEP ngroups
   zb# = INT((ASC(MID$(palet$, x, 1))) / 4)
   zg# = INT((ASC(MID$(palet$, x + 1, 1))) / 4)
   zr# = INT((ASC(MID$(palet$, x + 2, 1))) / 4)
   zc# = zb# * 65536# + zg# * 256# + zr#
   cres = ASC(MID$(palet$, x + 3, 1))
   PALETTE ((x - 1) / ngroups), zc#
NEXT x

IF nbits = 24 THEN
   y = picheight - 1
   x = 0
   dat$ = "   "
   WHILE y >= 0
      WHILE x < picwidth
         GET 1, , dat$
         p1 = INT((ASC(MID$(dat$, 1, 1)) + ASC(MID$(dat$, 1, 1)) + ASC(MID$(dat$, 1, 1))) / 3)
         PSET (x, y), p1
         x = x + 1
      WEND
      y = y - 1
      x = 0
   WEND
ELSEIF nbits = 8 THEN
   y = picheight - 1
   x = 0
   dat$ = " "
   WHILE y >= 0
      WHILE x < picwidth
         GET 1, , dat$
         PSET (x, y), ASC(dat$)
         x = x + 1
      WEND
      y = y - 1
      x = 0
   WEND
ELSEIF nbits = 4 THEN
   y = picheight - 1
   x = 0
   dat$ = " "
   WHILE y >= 0
      WHILE x < picwidth
        GET 1, , dat$
        LOCATE 1, 1
        p2 = ASC(dat$) AND 15
        p1 = (ASC(dat$) AND 240) / 16
        PSET (x, y), p1
        PSET (x + 1, y), p2
        x = x + 2
      WEND
      y = y - 1
      x = 0
   WEND
ELSEIF nbits = 1 THEN
   y = picheight - 1
   x = 0
   dat$ = " "
   WHILE y >= 0
      WHILE x < picwidth
        GET 1, , dat$
        p1 = ASC(dat$)
        FOR p = 0 TO 7
           PSET (x + (7 - p), y), (p1 AND 2 ^ p) / 2 ^ p
        NEXT p
        x = x + 8
      WEND
      y = y - 1
      x = 0
   WEND
END IF
DIM scrn!(16000)
GET (0, 0)-(319, 199), scrn!
DEF SEG = VARSEG(scrn!(0))
BSAVE "c:\example.bsv", VARPTR(scrn!(0)), 62532
DEF SEG
CLOSE


Adjust the size of the array accordingly to the resolution of the screen mode being used and the size of the image.

RE: How do I move a graphic ?

then load into qbasic.

DEF SEG = VARSEG(scrn!(0))
BLOAD "c:\example.bsv", VARPTR(scrn!(0))
DEF SEG

The reason that your code doesn't work is because everytime that you call INKEY$, it resets itself, so you have to allow it to last until it gets all of the way through the loop, the way that you do that is to make INKEY$ into a variable.

DO
  a$ = INKEY$
  IF a$ = CHR$(109) THEN
     x1% = x1% + 10
  END IF
  IF a$ = CHR$(110) THEN
     x1% = x1% - 10
  END IF
  PUT (x1%, y1%), box%, XOR
LOOP UNTIL a$ = CHR$(27)

RE: How do I move a graphic ?

If you place the cursor on "PUT" and press F1 you'll get the help file to open. THEN go down to where it says "PUT (graphical)" and click on it.  THEN go down to the bottom AGAIN and it'll tell you about the actionverbs.

  1. you need to learn to use code remarks (ex: REM or ').
  2. it's flashing cause you are using XOR repeatedly, read the help files.
  3. the first box stays on screen cause you didn't clear it.  Place a blank box there or use CLS.
  4. as mentioned above, you need to capture the keystroke before the movements, or else you'll have those jerky movements because QB has moved on to the next line, like "N" and you're still pressing "M".
  5. problem arises when beyond the screen--my code below is the "cheap" way out.  You'll have to construct it better.
here's my version:

CODE

SCREEN 1
DIM box%(1 TO 200), BlankBox(1 TO 200)
LOCATE 20, 1   '--print instructions
PRINT " USE m & n KEYS TO MOVE LEFT AND RIGHT"
PRINT " USE esc KEY TO EXIT"
ON ERROR GOTO ScreensEnd

GET (0, 0)-STEP(10, 10), BlankBox   '--copy a blankbox
LINE (0, 0)-STEP(10, 10), 2, BF     '--create and copy a filledbox
GET (0, 0)-STEP(10, 10), box%
PUT (0, 0), BlankBox, PSET          '--blank out previous box
PUT (100, 100), box%, PSET      '--place a box there; makes sure it works

DO                              '--outer loop for picture display

    DO                          '--inner loop for keystroke capture
        A$ = INKEY$
    LOOP WHILE A$ = ""
    IF A$ = CHR$(109) THEN          '--use "m" to move right
        x1% = 10
    ELSEIF A$ = CHR$(110) THEN      '--use "n" to move left
        x1% = -10
    ELSEIF A$ = CHR$(27) THEN       '--use "esc" to exit...or
        EXIT DO                     '--just go off the screen
    ELSE
        A$ = ""
    END IF
    PUT STEP(0, 0), BlankBox, PSET  '--clear previous box
    PUT STEP(x1%, 0), box%, PSET    '--place box in new position
LOOP
CLS
END

'--error handeler for off-screen
ScreensEnd:
LOCATE 20, 1
PRINT STRING$(39, 32)
PRINT STRING$(39, 32)
LOCATE 20, 1
PRINT "You went off the edge of the screen"
PRINT "Program will now terminate...game over"
SLEEP 4
END

This should help you some.  If you need further explainations please post them here.

--MiggyD

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close