Show datworms.c syntax highlighted
/* ______ ___ ___
* /\ _ \ /\_ \ /\_ \
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
* /\____/
* \_/__/
*
* A really silly little Worms grabber plugin.
*
* By Shawn Hargreaves.
*
* See readme.txt for copyright information.
*/
#include <stdio.h>
#include "allegro.h"
#include "../datedit.h"
/* what a pointless way to spend my Sunday afternoon :-) */
static int worms(void)
{
#define MAX_SIZE 65536
int *xpos = malloc(sizeof(int)*MAX_SIZE);
int *ypos = malloc(sizeof(int)*MAX_SIZE);
int head, tail, dir, dead, quit, score, counter;
int tx, ty, x, y, i, c1, c2, c3;
again:
head = 0;
tail = 0;
dir = 0;
dead = FALSE;
quit = FALSE;
score = 0;
counter = 0;
tx = -100;
ty = -100;
al_show_mouse(NULL);
if (al_bitmap_color_depth(al_screen) <= 8) {
AL_PALETTE pal;
al_get_palette(pal);
pal[0].r = 0;
pal[0].g = 0;
pal[0].b = 0;
pal[1].r = 31;
pal[1].g = 31;
pal[1].b = 31;
pal[254].r = 31;
pal[254].g = 31;
pal[254].b = 31;
pal[255].r = 63;
pal[255].g = 63;
pal[255].b = 63;
al_set_palette(pal);
c1 = 0;
c2 = 1;
c3 = 255;
}
else {
c1 = al_gui_fg_color;
c2 = al_gui_mg_color;
c3 = al_gui_bg_color;
}
al_clear_to_color(al_screen, c1);
al_drawing_mode(DRAW_MODE_XOR, NULL, 0, 0);
xpos[0] = AL_SCREEN_W/2;
ypos[0] = AL_SCREEN_H/2;
al_put_pixel(al_screen, xpos[0], ypos[0], c3);
while ((!dead) && (!quit)) {
x = xpos[head];
y = ypos[head];
switch (dir) {
case 0: x++; break;
case 1: y++; break;
case 2: x--; break;
case 3: y--; break;
}
if (x >= AL_SCREEN_W)
x -= AL_SCREEN_W;
else if (x < 0)
x += AL_SCREEN_W;
if (y >= AL_SCREEN_H)
y -= AL_SCREEN_H;
else if (y < 0)
y += AL_SCREEN_H;
head++;
if (head >= MAX_SIZE)
head = 0;
xpos[head] = x;
ypos[head] = y;
counter++;
if (counter & 15) {
al_put_pixel(al_screen, xpos[tail], ypos[tail], c3);
tail++;
if (tail >= MAX_SIZE)
tail = 0;
}
i = tail;
while (i != head) {
if ((x == xpos[i]) && (y == ypos[i])) {
dead = TRUE;
break;
}
i++;
if (i >= MAX_SIZE)
i = 0;
}
if (!(counter % (1+(score+2)/3)))
al_vsync();
al_put_pixel(al_screen, x, y, c3);
if ((tx < 0) || (ty < 0)) {
do {
tx = 16+rand()%(AL_SCREEN_W-32);
ty = 16+rand()%(AL_SCREEN_H-32);
} while ((ABS(x-tx)+ABS(y-ty)) < 64);
al_draw_circle(al_screen, tx, ty, 8, c2);
al_draw_circle(al_screen, tx, ty, 5, c2);
al_draw_circle(al_screen, tx, ty, 2, c2);
}
if ((ABS(x-tx)+ABS(y-ty)) < 9) {
al_draw_circle(al_screen, tx, ty, 8, c2);
al_draw_circle(al_screen, tx, ty, 5, c2);
al_draw_circle(al_screen, tx, ty, 2, c2);
tx = -100;
ty = -100;
score++;
}
al_text_mode(c1);
al_printf_text(al_screen, al_font_8x8, 0, 0, c2, "Score: %d", score);
if (al_key_pressed()) {
switch (al_read_key()>>8) {
case AL_KEY_RIGHT:
dir = 0;
break;
case AL_KEY_DOWN:
dir = 1;
break;
case AL_KEY_LEFT:
dir = 2;
break;
case AL_KEY_UP:
dir = 3;
break;
case AL_KEY_ESC:
quit = TRUE;
break;
}
}
}
al_drawing_mode(DRAW_MODE_SOLID, NULL, 0, 0);
al_clear_to_color(al_screen, al_gui_fg_color);
al_set_palette(datedit_current_palette);
do {
al_poll_keyboard();
} while ((al_key[AL_KEY_ESC]) || (al_key[AL_KEY_RIGHT]) || (al_key[AL_KEY_LEFT]) || (al_key[AL_KEY_UP]) || (al_key[AL_KEY_DOWN]));
al_clear_keybuf();
al_show_mouse(al_screen);
if (dead) {
char buf[64];
sprintf(buf, "Score: %d", score);
if (al_show_alert(buf, NULL, NULL, "Play", "Quit", 'p', 'q') == 1)
goto again;
}
free(xpos);
free(ypos);
return D_REDRAW;
}
/* hook ourselves into the grabber menu system */
static AL_MENU worms_menu =
{
"Worms",
worms,
NULL,
0,
NULL
};
DATEDIT_MENU_INFO datworms_menu =
{
&worms_menu,
NULL,
DATEDIT_MENU_HELP,
0
};
See more files for this project here