From 9d2e5ef6d08ff8c1730e3413a6a14e6fdc71040b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 1 Aug 2009 14:50:32 +0000 Subject: [PATCH] Add a simple snake program using the interface. --- COPYING.ISC | 13 +++ Makefile.am | 2 +- THANKS | 1 + examples/.gitignore | 1 + examples/Makefile.am | 5 +- examples/conio-snake.cpp | 221 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 241 insertions(+), 2 deletions(-) create mode 100644 COPYING.ISC create mode 100644 examples/conio-snake.cpp diff --git a/COPYING.ISC b/COPYING.ISC new file mode 100644 index 0000000..2bf2677 --- /dev/null +++ b/COPYING.ISC @@ -0,0 +1,13 @@ +Copyright (c) Year(s), Company or Person's Name + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/Makefile.am b/Makefile.am index fa04453..8904abb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,7 +3,7 @@ SUBDIRS = kernel caca src examples tests tools caca-sharp cxx python ruby doc DIST_SUBDIRS = $(SUBDIRS) win32 -EXTRA_DIST = NOTES COPYING.GPL COPYING.LGPL bootstrap build-dos build-kernel build-win32 caca-config.in libcaca.spec libcaca.sln +EXTRA_DIST = NOTES COPYING.GPL COPYING.ISC COPYING.LGPL bootstrap build-dos build-kernel build-win32 caca-config.in libcaca.spec libcaca.sln AUTOMAKE_OPTIONS = dist-bzip2 bin_SCRIPTS = caca-config diff --git a/THANKS b/THANKS index a312701..a4f72eb 100644 --- a/THANKS +++ b/THANKS @@ -10,6 +10,7 @@ - Jan Hubicka - aafire - Michele Bini - original SDL plasma - Free Software Foundation, Inc. - multiboot.S +- Simon Huggins - conio-snake \section thanks3 Porters and packagers diff --git a/examples/.gitignore b/examples/.gitignore index 74a466e..2d5c7a5 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -2,6 +2,7 @@ blit canvas colors conio +conio-snake demo demo0 dithering diff --git a/examples/Makefile.am b/examples/Makefile.am index 10b1e81..79cba90 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -2,7 +2,7 @@ AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/caca -I$(top_builddir)/caca -noinst_PROGRAMS = blit canvas colors conio demo demo0 dithering driver event export figfont font font2tga frames fullwidth gamma hsv input spritedit swallow text transform truecolor unicode import +noinst_PROGRAMS = blit canvas colors conio conio-snake demo demo0 dithering driver event export figfont font font2tga frames fullwidth gamma hsv input spritedit swallow text transform truecolor unicode import blit_SOURCES = blit.c blit_LDADD = ../caca/libcaca.la @@ -16,6 +16,9 @@ colors_LDADD = ../caca/libcaca.la conio_SOURCES = conio.c conio_LDADD = ../caca/libcaca.la +conio_snake_SOURCES = conio-snake.cpp +conio_snake_LDADD = ../caca/libcaca.la + demo_SOURCES = demo.c demo_LDADD = ../caca/libcaca.la demo_LDFLAGS = @MATH_LIBS@ diff --git a/examples/conio-snake.cpp b/examples/conio-snake.cpp new file mode 100644 index 0000000..b3c3845 --- /dev/null +++ b/examples/conio-snake.cpp @@ -0,0 +1,221 @@ +/* + * conio-snake snake game using the API + * Copyright (c) 2003-2004 Simon Huggins + * All Rights Reserved + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +/* prototypes */ +void draw_line(int col, int row); +void show_score(); +void add_segment(); +void setup_level(); + +/* constants */ +const int maxrow=15, maxcol=77; +const int snake_start_col=33,snake_start_row=7; +const char up_key='a', down_key='z', left_key='o', right_key='p'; +const int pause_length=500000; + +/* global variables */ +int score, snake_length, speed, obstacles, level, firstpress, high_score=0; +char screen_grid[maxrow][maxcol]; +char direction = right_key; +struct snake_segment { + int row,col; +} snake[100]; + +int main() +{ + + /* Variable declarations within main() only */ + char keypress; + + do /* restart game loop */ + { + obstacles=4; level=1; score=0; speed=14; + + //randomize(); /* Ensure random seed initiated */ + + setup_level(); + + /* main loop */ + do + { + delay(speed * pause_length / 50000); /*pause*/ + + /* If key has been hit, then check it is a direction key - if so, + change direction */ + if (kbhit()) + { + keypress=(char)getch(); + if((keypress==right_key)||(keypress==left_key)|| + (keypress==up_key)||(keypress==down_key)) + direction = keypress; + } + + /* Add a segment to the end of the snake */ + add_segment(); + + /* Blank last segment of snake */ + gotoxy(snake[0].col,snake[0].row); + cprintf(" "); + /* ... and remove it from the array */ + for (int i=1;i<=snake_length;i++) + snake[i-1]=snake[i]; + /* Display snake in yellow */ + textcolor(YELLOW); + for (int i=0;i<=snake_length;i++) + { + gotoxy(snake[i].col,snake[i].row); + cprintf("O"); + } + /* keeps cursor flashing in one place instead of following snake */ + gotoxy(1,1); + + /* If first press on each level, pause until a key is pressed */ + if (firstpress) { while(!kbhit()); firstpress = 0; } + + /* Collision detection - walls (bad!) */ + if ((snake[snake_length-1].row>maxrow+1)||(snake[snake_length-1].row<=1)|| + (snake[snake_length-1].col>maxcol+1)||(snake[snake_length-1].col<=1)|| + /* Collision detection - obstacles (bad!) */ + (screen_grid[snake[snake_length-1].row-2][snake[snake_length-1].col-2]=='x')) + keypress='x'; /* i.e. exit loop - game over */ + /* Collision detection - snake (bad!) */ + for (int i=0;i1)) speed--; /* increase speed every 5 levels */ + setup_level(); /* display next level */ + } + } + } while (keypress!='x'); + + /* game over message */ + if (score > high_score) high_score = score; + show_score(); + gotoxy(30,6); textcolor(LIGHTRED); cprintf("G A M E O V E R"); + gotoxy(30,9); textcolor(YELLOW); cprintf("Another Game (y/n)? "); + do keypress=getch(); while((keypress!='y')&&(keypress!='n')); + + } while (keypress=='y'); +} + +void setup_level() +{ + + /* variables local to setup_level() */ + int row,col; + + /* Set up global variables for new level */ + snake_length=level+4; direction = right_key; + firstpress = 1; + /* Fill grid with blanks */ + for(row=0;row