Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Challenge: 2D Tank

Name: Anonymous 2010-05-14 15:43

The Challenge:
|---->Program a 2d tank application in the language of your choice that adheres to the following:

#-The tank must rotate using the left and right directional keys.
#-The tank must loosely resemble a tank.
#-The tank must move forward or backward respectively from the direction it is facing using the up and down directional keys.
#-Pressing spacebar must fire a 'shot' in the direction the tank is facing.
#-The perspective of the user must be fixed and facing down at the tank.
#-The tank, shot, and ground must each have their own color. (all shots may have the same color)


<------You have 24 hours!------>
GET TO WORK!

Name: Anonymous 2010-05-14 18:04

>>1
I finished early, what do I get?

/* gcc -o susstank -ansi susstank.c -lm `allegro-config --libs` */

/* Copyright (c) 2010 Xarn
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <stdio.h>
#include <math.h>
#include <allegro.h>

#define WIDTH  729
#define HEIGHT 729

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

int main(void)
{
    int tx = WIDTH / 2, ty = HEIGHT / 2, mx = tx, my = ty, firing = 0;
    fixed tangle = 0, mangle = 0;
    double rtangle = 0, rmangle = 0;
    BITMAP *background, *tank, *miss, *buffa;

    allegro_init();
    install_keyboard();
    install_timer();

    set_color_depth(32);
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, WIDTH, HEIGHT, 0, 0) != 0) {
        fprintf(stderr, "Couldn't initialise Allegro!\n");
        return 1;
    }
    set_window_title("SUSSTANK");

    buffa = create_bitmap(WIDTH, HEIGHT);
    background = load_pcx("back.pcx", NULL);
    tank = load_pcx("tank.pcx", NULL);
    miss = load_pcx("miss.pcx", NULL);
    if (!background || !tank || !miss) {
        fprintf(stderr, "Couldn't load images!\n");
        return 2;
    }

    for (;;) {
        int k;

        blit(background, buffa, 0, 0, 0, 0, WIDTH, HEIGHT);
        if (firing)
            pivot_sprite(buffa, miss, mx, my, miss->w / 2, miss->h / 2, mangle);
        pivot_sprite(buffa, tank, tx, ty, tank->w / 2, tank->h / 2, tangle);
        blit(buffa, screen, 0, 0, 0, 0, WIDTH, HEIGHT);

        if (keypressed()) {
            k = readkey();

            if ((k & 0xff) == 'q')
                break;

            switch (k >> 8) {
            case KEY_SPACE:
                if (!firing) {
                    mx = tx;
                    my = ty;
                    mangle = tangle;
                    rmangle = rtangle;
                    firing = 1;
                }
                break;
            case KEY_LEFT:
                tangle -= 8 << 16;
                rtangle = (tangle >> 16) * M_PI / 128;
                break;
            case KEY_RIGHT:
                tangle += 8 << 16;
                rtangle = (tangle >> 16) * M_PI / 128;
                break;
            case KEY_UP:
                tx += 8 * cos(rtangle);
                ty += 8 * sin(rtangle);
                break;
            case KEY_DOWN:
                tx -= 8 * cos(rtangle);
                ty -= 8 * sin(rtangle);
                break;
            }
        }

        if (firing) {
            mx += 20 * cos(rmangle);
            my += 20 * sin(rmangle);
            if (mx < -50 || mx > WIDTH + 50 || my < -50 || my > HEIGHT + 50)
                firing = 0;
        }

        rest(20);
    }

    return 0;
} END_OF_MAIN()


Get images from http://cairnarvon.rotahall.org/susstank/

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List