QBasic/Advanced Graphics

From Wikibooks, open books for an open world
< QBasic
Jump to navigation Jump to search

Animation[edit]

Basic Movement[edit]

Animation is basically graphics that changes over a fixed period of time. In this we will be using a do-loop .

SCREEN 7  ' we need to use a graphics enabled screen mode
animation   'calling the sub


SUB animation
    SCREEN 7
    x = 10 'set first x- coordinate
    y = 10 'set first y-coordinate
    DO
        CLS ' going back to a blank screen so that the previous rectangle is erased
        x = x + 3  ' setting increment of coordinate x
        y = y + 3   ' setting increment of coordinate y

        LINE (x, y)-(x + 5, y) 'drawing lines
        LINE (x, y + 5)-(x + 5, y + 5)
        LINE (x, y)-(x, y + 5)
        LINE (x + 5, y)-(x + 5, y + 5)

        SLEEP 2

    LOOP UNTIL INKEY$ <> ""



END SUB

Explanation:

  1. We have switched from the default qbasic text-only screen to one which enables graphics.
  2. We have called the sub which creates the animation.
  3. We have begun the do-loop until. This enables the animation to run until the user ends it by pressing a key.
  4. We have set an increment of the coordinates. This allows the box to be drawn on a new position rather than the same one. If it movement in only one direction was wished, we had to set the increment only in one variable.
  5. We have drawn lines from each coordinate to another. Note that each time one coordinate remains fixed while the others change.(In this I refer to the two coordinate sets, the first starting and the ending one)
  6. We have issued a sleep command . This stops execution for 2 seconds . Without this the do-loop will execute more quickly than we want , and the animation will be very short-lived.
  7. By using RND for the variables, you can create a randomized ,unpredictable animation.


Mouse-Control[edit]

In this step, we will use the QB64 inbuilt _mousehide,_mousex,_mousey,_mouseinput and _mousebutton commands to control the mouse input.

WARNING[edit]
These Functions only work in QB64!
[edit]
_mousehide
screen 7

mousetrack

sub mousetrack
do while _mouseinput
cls

X = _mousex
Y = _mousey

LINE (X - 10, Y)-(X + 10, Y), 15
LINE (X, Y - 10)-(X, Y + 10), 15

        IF _MOUSEBUTTON(1) THEN
            IF X > A AND X < A + 25 AND Y > B AND Y < B + 25 THEN
                message$ = "yes"
                goto action
            END IF
        END IF

loop until inkey$ <> ""
  1. Here the first function "_mousehide" prevents the default pointer mouse format to be displayed on the screen
  2. Mouseinput function retrieves mouse information.
  3. The next functions "_mousex" and "_mousey" hold the current x and y coordinates of the mouse.
  4. The lines draw a basic trigger .
  5. The "_mousebutton" function returns the value of the mouse button pressed, "1" signifies the left button being pressed.
  6. If the mouse button event has taken place within a certain enclosed area, a message in the form of "message$" is issued. This can be used later on.
  7. The procedure, if the previous condition has been fulfilled , goes to line label "action" where any commands to be executed may lie.
  8. Else , the process loops , until a "loop until" condition has been met. It can also be something other than the one given above.

Usage[edit]

These graphics and animations can be used to create a complete game. Instead of the "Mouse" functions, you could use the "Inkey$" command to issue various scenarios and cases, each with a complete code to decide what happens next.

Tip[edit]

Instead of making games which do not contain any user information, you could use ".txt" files to store information. This information can be later retrieved to make a game with a complete "Career" option.