Archive / / main.cpp
2004-08-16 03:02:36 UTC
previous next
#include "allegro.h" #include "time.h" BITMAP *buffer; // Stores the x,y values for each part of the snake // 3072 = 64 x 48 (the maximum possible length of the snake) char snake_location[3072][2]; // Show the beginning and end of the snake in snake_location int snake_head; int snake_tail; char snake_direction; #define SNAKE_UP 0 #define SNAKE_DOWN 1 #define SNAKE_LEFT 2 #define SNAKE_RIGHT 3 char bit_x = 50; char bit_y = 30; bool show_menu(bool gameover); int num_bits = 0; char eatbit = 0; int speed = 5; int main(void) { // Start allegro allegro_init(); install_keyboard(); if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0) { set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); allegro_message("Unable to set any graphic mode\n%s\n", allegro_error); return 1; } set_palette(desktop_palette); buffer = create_bitmap(640,480); clear_to_color(buffer, makecol(0, 0, 0)); srand(time(0)); snake_direction = SNAKE_RIGHT; snake_head = 7; snake_tail = 5; snake_location[5][0] = 10; snake_location[5][1] = 10; snake_location[6][0] = 11; snake_location[6][1] = 10; snake_location[7][0] = 12; snake_location[7][1] = 10; if (!show_menu(false)) return 0; for (;;) { if (keypressed()) { int keydown = readkey() >> 8; if (keydown == KEY_ESC) return 0; if (keydown == KEY_LEFT && snake_direction != SNAKE_RIGHT) snake_direction = SNAKE_LEFT; if (keydown == KEY_RIGHT && snake_direction != SNAKE_LEFT) snake_direction = SNAKE_RIGHT; if (keydown == KEY_UP && snake_direction != SNAKE_DOWN) snake_direction = SNAKE_UP; if (keydown == KEY_DOWN && snake_direction != SNAKE_UP) snake_direction = SNAKE_DOWN; if (keydown == KEY_P) { textout_centre_ex(screen, font, "Paused - Press any key to continue", 320, 235, makecol(255, 255, 255), -1); readkey(); } } char new_snake_x, new_snake_y; new_snake_x = snake_location[snake_head][0]; new_snake_y = snake_location[snake_head][1]; switch(snake_direction) { case SNAKE_UP: new_snake_y--; break; case SNAKE_DOWN: new_snake_y++; break; case SNAKE_LEFT: new_snake_x--; break; case SNAKE_RIGHT: new_snake_x++; break; } if (new_snake_x < 0 || new_snake_x > 64 || new_snake_y < 0 || new_snake_y > 48) { if (!show_menu(true)) return 0; snake_direction = SNAKE_RIGHT; snake_head = 7; snake_tail = 5; snake_location[5][0] = 10; snake_location[5][1] = 10; snake_location[6][0] = 11; snake_location[6][1] = 10; snake_location[7][0] = 12; snake_location[7][1] = 10; continue; } if (new_snake_x == bit_x && new_snake_y == bit_y) { num_bits++; eatbit = 4; bit_x = rand() % 64; bit_y = rand() % 48; } clear_to_color(buffer, makecol(0, 0, 0)); textprintf_ex(buffer, font, 0, 0, makecol(255, 255, 255), -1, "Score: %i", num_bits); // Remove tail if (eatbit == 0) { snake_tail++; if (snake_tail == 3072) snake_tail = 0; } else { eatbit--; } for(int i = snake_tail;;) { rectfill(buffer, snake_location[i][0]*10, snake_location[i][1]*10, snake_location[i][0]*10+9, snake_location[i][1]*10+9, makecol(255, 255, 255)); if (snake_location[i][0] == new_snake_x && snake_location[i][1] == new_snake_y) { /* if (!show_menu(true)) return 0; snake_direction = SNAKE_RIGHT; snake_head = 7; snake_tail = 5; snake_location[5][0] = 10; snake_location[5][1] = 10; snake_location[6][0] = 11; snake_location[6][1] = 10; snake_location[7][0] = 12; snake_location[7][1] = 10; break; */ allegro_message("you dead!"); exit(0); return 0; } if (i == snake_head) break; i++; if (i == 3072) i = 0; } snake_head++; if (snake_head == 3072) snake_head = 0; snake_location[snake_head][0] = new_snake_x; snake_location[snake_head][1] = new_snake_y; rectfill(buffer, new_snake_x*10, new_snake_y*10, new_snake_x*10+9, new_snake_y*10+9, makecol(255, 0, 0)); rectfill(buffer, bit_x*10, bit_y*10, bit_x*10+9, bit_y*10+9, makecol(0, 255, 0)); blit(buffer,screen,0,0,0,0,640,480); rest(80 - speed*4); } } END_OF_MAIN(); bool show_menu(bool gameover) { for (;;) { rect(buffer, 230, 180, 410, 280, makecol(255, 255, 255)); rectfill(buffer, 231, 181, 409, 279, makecol(0, 0, 0)); textout_centre_ex(buffer, font, (gameover) ? "Game over!" : "Welcome to nibbles", 320, 200, makecol(255, 255, 255), -1); textprintf_ex(buffer, font, 278, 220, makecol(255, 255, 255), -1, "1. Speed: %i", speed); textprintf_ex(buffer, font, 278, 240, makecol(255, 255, 255), -1, (gameover) ? "2. Restart Game" : "2. Start Game"); textprintf_ex(buffer, font, 278, 250, makecol(255, 255, 255), -1, "3. Quit"); blit(buffer,screen,0,0,0,0,640,480); int choice = readkey() >> 8; if (choice == KEY_ESC || choice == KEY_3) return false; if (choice == KEY_1) { speed++; speed = speed % 10; } if (choice == KEY_2) return true; } } END_OF_FUNCTION(show_menu);