主题:[原创]模仿文曲星上的赛车游戏
两天做好。
这是文本模式的,不知道大家喜不喜欢赛车,反正我是赛车迷,极品飞车6被全我打通了。这个游戏分5个级别,我打不过第5关,试一下吧,打到最后你会兴奋的跳起来~
过程中碰到的唯一的难题就是,键盘控制,如何让程序不中断,还能检查按键?
我参考了[url=http://www.programfan.com/club/showbbs.asp?id=14937]http://www.programfan.com/club/showbbs.asp?id=14937[/url]
俄罗斯方块的那个程序。
用if (bioskey (1)) key = bioskey (0);
再while (bioskey(1)) bioskey (0); 清除键盘缓冲区。
清单:
/*
* car.c ,a textmode game.
* designer: euclid
*/
#include "conio.h"
#include "time.h"
#include "stdlib.h"
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ESC 0x11b
#define SPACE 0x3920
#define CARX0 30
#define CARY0 24
void quit (void);
void drawcar (void);
void clearcar (void);
void waite (float tml);
/* array is data struct of autodrome.
* array [0] is hiden, to auto build new row.
* the value of array means the left road edge.
*/
int array [11] = {8,8,8,8,8,8,8,8,8,8,8};
int rootx = 20, rooty = CARY0 - 9;
int carx = CARX0, cary = CARY0;
int board = 4; /* board is unuseful ? */
/* waite () mean delay tml(second) and waite a bioskey.
* return key value.
*/
static void waite (float tml)
{
int key;
clock_t bg, ed;
bg = clock ();
if (bioskey (1))
key = bioskey (0); /* clear key memory */
switch (key) {
case UP:
clearcar ();
cary--;
drawcar ();
break;
case DOWN:
clearcar ();
cary++;
drawcar ();
break;
case LEFT:
clearcar ();
carx--;
drawcar ();
break;
case RIGHT:
clearcar ();
carx++;
drawcar ();
break;
case ESC:
quit ();
break;
case SPACE :
while (getch ());
break;
}
do {
ed = clock ();
} while ((ed - bg)/18.2 < tml);
}
static void drawcar (void)
{
gotoxy (carx, cary);
textcolor (LIGHTCYAN);
putch (30); /* car shape */
textcolor (WHITE);
}
static void clearcar (void)
{
gotoxy (carx, cary);
putch (' ');
}
void quit (void)
{
exit (0);
}
static void drawroad (void)
{
int i, j;
for (i = 1; i < 11; i++) {
gotoxy (rootx, rooty + i - 1);
for (j = 0; j < 21; j++) {
if (j >= array [i] && j <= (array [i] + board))
putch (' ');
else
putch ('N'); /* wall */
}
}
}
static void refurnishroad (void)
{
int i, j;
randomize ();
array [0] = array [1] + rand()%3 - 1;
if (array [0] < 1 )
array [0] = 1;
else if (array [0] >= 20)
array [0] = 20;
else if (array [0] >= (20 - board))
array [0] = 19 - board;
for (i = 10; i >= 1; i--) {
array [i] = array [i - 1];
gotoxy (rootx + array[i] - 1, rooty + i - 1);
putch ('N');
putch (' ');
gotoxy (rootx + array [i] + board, rooty + i - 1);
putch (' ');
putch ('N');
}
}
void bomb (void)
{
gotoxy (carx, cary);
textcolor (LIGHTRED);
putch (30);
textcolor (WHITE);
gotoxy (30, 12);
cprintf ("fail");
}
void win (void)
{
printf ("very good, you have passed!");
}
main ()
{
int i, leveltime = 12;
long score = 0 ;
float level;
clock_t bg, ed;
clrscr ();
/* gotoxy (30, 20); */
printf ("chose level:(1 ~ 5) ");
scanf ("%f", &level);
if (level < 1 || level > 5) level = 1;
clrscr ();
refurnishroad ();
/* drawroad (); */
drawcar ();
gotoxy (30, 10);
printf ("Press any key to start..");
bioskey (0); /* press any key to start */
gotoxy (30, 10);
printf (" ");
gotoxy (30, 10);
printf ("level %.0f", level);
gotoxy (45, 15);
cprintf ("score : %ld", score);
gotoxy (45, 17);
cprintf ("ESC to exit game");
gotoxy (45,19);
cprintf ("SPACE to pause");
bg = clock (); /* start clock */
while (1) {
if (level > 5)
win ();
waite (0.3 - level/20);
refurnishroad ();
drawroad ();
drawcar ();
if ((carx - rootx) < array [cary - rooty + 1] ||
(carx - rootx) > (array [cary - rooty + 1] + board)) {
bomb ();
break;
}
ed = clock ();
if ((ed - bg)/ 18.2 > leveltime) {
bg = clock (); /* refurnish clock */
level++; /* 30s level++ */
gotoxy (30, 10);
printf ("level %.0f", level);
}
if (level >= 3 && level < 5) {
gotoxy (30, 12);
board = 3; /* unuseful ? */
}
score += level * 10 ;
if ((int)((ed - bg)/18.2*10)%3 == 0.0 ) {
gotoxy (45, 15);
cprintf ("score : %ld", score);
}
}
while (bioskey (0) != ESC);
}
打通第5关的告诉我啊,我请你客
这是文本模式的,不知道大家喜不喜欢赛车,反正我是赛车迷,极品飞车6被全我打通了。这个游戏分5个级别,我打不过第5关,试一下吧,打到最后你会兴奋的跳起来~
过程中碰到的唯一的难题就是,键盘控制,如何让程序不中断,还能检查按键?
我参考了[url=http://www.programfan.com/club/showbbs.asp?id=14937]http://www.programfan.com/club/showbbs.asp?id=14937[/url]
俄罗斯方块的那个程序。
用if (bioskey (1)) key = bioskey (0);
再while (bioskey(1)) bioskey (0); 清除键盘缓冲区。
清单:
/*
* car.c ,a textmode game.
* designer: euclid
*/
#include "conio.h"
#include "time.h"
#include "stdlib.h"
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ESC 0x11b
#define SPACE 0x3920
#define CARX0 30
#define CARY0 24
void quit (void);
void drawcar (void);
void clearcar (void);
void waite (float tml);
/* array is data struct of autodrome.
* array [0] is hiden, to auto build new row.
* the value of array means the left road edge.
*/
int array [11] = {8,8,8,8,8,8,8,8,8,8,8};
int rootx = 20, rooty = CARY0 - 9;
int carx = CARX0, cary = CARY0;
int board = 4; /* board is unuseful ? */
/* waite () mean delay tml(second) and waite a bioskey.
* return key value.
*/
static void waite (float tml)
{
int key;
clock_t bg, ed;
bg = clock ();
if (bioskey (1))
key = bioskey (0); /* clear key memory */
switch (key) {
case UP:
clearcar ();
cary--;
drawcar ();
break;
case DOWN:
clearcar ();
cary++;
drawcar ();
break;
case LEFT:
clearcar ();
carx--;
drawcar ();
break;
case RIGHT:
clearcar ();
carx++;
drawcar ();
break;
case ESC:
quit ();
break;
case SPACE :
while (getch ());
break;
}
do {
ed = clock ();
} while ((ed - bg)/18.2 < tml);
}
static void drawcar (void)
{
gotoxy (carx, cary);
textcolor (LIGHTCYAN);
putch (30); /* car shape */
textcolor (WHITE);
}
static void clearcar (void)
{
gotoxy (carx, cary);
putch (' ');
}
void quit (void)
{
exit (0);
}
static void drawroad (void)
{
int i, j;
for (i = 1; i < 11; i++) {
gotoxy (rootx, rooty + i - 1);
for (j = 0; j < 21; j++) {
if (j >= array [i] && j <= (array [i] + board))
putch (' ');
else
putch ('N'); /* wall */
}
}
}
static void refurnishroad (void)
{
int i, j;
randomize ();
array [0] = array [1] + rand()%3 - 1;
if (array [0] < 1 )
array [0] = 1;
else if (array [0] >= 20)
array [0] = 20;
else if (array [0] >= (20 - board))
array [0] = 19 - board;
for (i = 10; i >= 1; i--) {
array [i] = array [i - 1];
gotoxy (rootx + array[i] - 1, rooty + i - 1);
putch ('N');
putch (' ');
gotoxy (rootx + array [i] + board, rooty + i - 1);
putch (' ');
putch ('N');
}
}
void bomb (void)
{
gotoxy (carx, cary);
textcolor (LIGHTRED);
putch (30);
textcolor (WHITE);
gotoxy (30, 12);
cprintf ("fail");
}
void win (void)
{
printf ("very good, you have passed!");
}
main ()
{
int i, leveltime = 12;
long score = 0 ;
float level;
clock_t bg, ed;
clrscr ();
/* gotoxy (30, 20); */
printf ("chose level:(1 ~ 5) ");
scanf ("%f", &level);
if (level < 1 || level > 5) level = 1;
clrscr ();
refurnishroad ();
/* drawroad (); */
drawcar ();
gotoxy (30, 10);
printf ("Press any key to start..");
bioskey (0); /* press any key to start */
gotoxy (30, 10);
printf (" ");
gotoxy (30, 10);
printf ("level %.0f", level);
gotoxy (45, 15);
cprintf ("score : %ld", score);
gotoxy (45, 17);
cprintf ("ESC to exit game");
gotoxy (45,19);
cprintf ("SPACE to pause");
bg = clock (); /* start clock */
while (1) {
if (level > 5)
win ();
waite (0.3 - level/20);
refurnishroad ();
drawroad ();
drawcar ();
if ((carx - rootx) < array [cary - rooty + 1] ||
(carx - rootx) > (array [cary - rooty + 1] + board)) {
bomb ();
break;
}
ed = clock ();
if ((ed - bg)/ 18.2 > leveltime) {
bg = clock (); /* refurnish clock */
level++; /* 30s level++ */
gotoxy (30, 10);
printf ("level %.0f", level);
}
if (level >= 3 && level < 5) {
gotoxy (30, 12);
board = 3; /* unuseful ? */
}
score += level * 10 ;
if ((int)((ed - bg)/18.2*10)%3 == 0.0 ) {
gotoxy (45, 15);
cprintf ("score : %ld", score);
}
}
while (bioskey (0) != ESC);
}
打通第5关的告诉我啊,我请你客