VgaGames4

VgaGames4 - tutorial

# Tutorial 2

The body of the game is created, now we add the player-racket. We can move it up and down with the cursor-keys or a gamecontroller. When it collides with the upper or lower border we stop the moving.

At first an overview about the file pingpong.c.

    pingpong.c: Overview

    /* global declarations */
    [CODEBOX]

    /* show help-text */
    static VG_BOOL show_help(void) { ... (see pingpong.c) ... }

    /* main function */
    int main(int argc, char **argv) {
      /* variable declaration */
      [CODEBOX]

      /* initializing */
      [...]      /* opening */
      [CODEBOX]  /* set keys */
      [CODEBOX]  /* initialize player-racket */
      [...]      /* show help, start background music */

      /* game loop */
      [...]      /* receive input-events and check the local key-strokes */
      [CODEBOX]  /* check key-strokes for player-racket */
      [CODEBOX]  /* draw out */
      [...]      /* flush and wait */

      /* end game */
      [...]      /* destroy and exit */
    }

An overview about the used variables in function main(). We add the player-racket structure.
[To Position]

    pingpong.c: the variables in function main()

    const int border_size = 10;  /* size of borders in pixels */

    int winw, winh;  /* window's width and height */
    int audc_bgmusic;  /* audio descriptors */

    struct {  /* player's structure */
      struct VG_Image *imgp;  /* image */
      struct VG_Rect rect;    /* rectangle position */
    } player;

    /* helper variables */
    struct VG_Rect rect;
    struct VG_Position posi;

At the beginning of the file pingpong.c we add the keys for the player-racket.
[To Position]

    pingpong.c: global declarations

    /* keys */
    struct {
      int k_quit_lalt;  /* part of quit: Left-ALT */
      int k_quit_q;     /* part of quit: Q */
      int k_pause;      /* pause */
      int k_up;         /* move up */
      int k_down;       /* move down */
    } kref;

When setting the keys we add the player-racket keys: - move the player-racket up with - keyboard: [Cursor UP] - gamecontroller: [Right axis: up] it shall be changeable in a key-change menu - move the player-racket down with - keyboard: [Cursor DOWN] - gamecontroller: [Right axis: down] it shall be changeable in a key-change menu
[To Position]

    pingpong.c: continuing the function main()

    /* set keys */

    /* quit with ALT+Q, not changeable */
    if ((kref.k_quit_lalt = vg4->input->key_insert("Quit-LALT", VG_FALSE, VG_FALSE)) == 0) { VG_dest(); exit(1); }
    vg4->input->key_setkbd(kref.k_quit_lalt, VG_INPUT_KBDCODE_LALT);
    if ((kref.k_quit_q = vg4->input->key_insert("Quit-Q", VG_FALSE, VG_FALSE)) == 0) { VG_dest(); exit(1); }
    vg4->input->key_setkbd(kref.k_quit_q, VG_INPUT_KBDCODE_Q);

    /* pause with P, changeable */
    if ((kref.k_pause = vg4->input->key_insert("Pause", VG_TRUE, VG_FALSE)) == 0) { VG_dest(); exit(1); }
    vg4->input->key_setkbd(kref.k_pause, VG_INPUT_KBDCODE_P);

    /* move-up with cursor-key UP, changeable */
    if ((kref.k_up = vg4->input->key_insert("Move up", VG_TRUE, VG_FALSE)) == 0) { VG_dest(); exit(1); }
    vg4->input->key_setkbd(kref.k_up, VG_INPUT_KBDCODE_UCURS);
    vg4->input->key_setgc(kref.k_up, 0, VG_INPUT_GCAXIS_RIGHTY_UP);

    /* move-down with cursor-key DOWN, changeable */
    if ((kref.k_down = vg4->input->key_insert("Move down", VG_TRUE, VG_FALSE)) == 0) { VG_dest(); exit(1); }
    vg4->input->key_setkbd(kref.k_down, VG_INPUT_KBDCODE_DCURS);
    vg4->input->key_setgc(kref.k_down, 0, VG_INPUT_GCAXIS_RIGHTY_DOWN);

Furthermove we initialize the player-racket, using the structure-variable player. The racket-image is loaded and the rectangle of the racket is set by the width and height of the image and its position.
[To Position]

    pingpong.c: continuing the function main()

    /* initialize player-racket, put it at the left */

    /* load image */
    player.imgp = vg4->image->load("files/player.bmp");
    if (player.imgp == NULL) { VG_dest(); exit(1); }

    /* set rectangle */
    vg4->image->getsize(player.imgp, NULL, &player.rect.w, &player.rect.h);
    player.rect.x = 0;
    player.rect.y = (winh - player.rect.h) / 2;

In the game-loop we check the key-strokes for using the player-racket and move it according to the key-stroke at 2 pixels. If it collides with the upper or lower border, whose height is border_size pixels, the moving stops.
[To Position]

    pingpong.c: continuing the game-loop of the function main()

      /* +++ player: check for key-strokes +++ */

      /* moving up */
      if (vg4->input->key_pressed(kref.k_up)) {
        player.rect.y -= 2;
        if (player.rect.y < border_size) { player.rect.y = border_size; }
      }

      /* moving down */
      if (vg4->input->key_pressed(kref.k_down)) {
        player.rect.y += 2;
        if (player.rect.y > winh - border_size - player.rect.h) { player.rect.y = winh - border_size - player.rect.h; }
      }

Additionally the player-racket is drawn out.
[To Position]

    pingpong.c: continuing the game-loop of the function main()

      /* +++ draw out +++ */

      /* clear window */
      vg4->window->clear();

      /* draw background and borders */

      vg4->window->fill(vg4->misc->colorbrightness(VG_COLOR_BLUE, 50));

      /* top border */
      rect.x = rect.y = 0; rect.w = winw; rect.h = border_size;
      vg4->window->draw_rect(&rect, VG_COLOR_RGB(0xb1, 0, 0), VG_TRUE);

      /* bottom border */
      rect.x = 0; rect.y = winh - border_size; rect.w = winw; rect.h = border_size;
      vg4->window->draw_rect(&rect, VG_COLOR_RGB(0xb1, 0, 0), VG_TRUE);

      /* right border */
      rect.x = winw - border_size; rect.y = 0; rect.w = border_size; rect.h = winh;
      vg4->window->draw_rect(&rect, VG_COLOR_RGB(0xb1, 0, 0), VG_TRUE);

      /* draw player */
      vg4->window->copy(player.imgp, vg4->misc->rect2position(&posi, &player.rect), NULL);

<<Previous | Download | Next>>