回 帖 发 新 帖 刷新版面

主题:[原创]模仿文曲星上的赛车游戏

两天做好。
这是文本模式的,不知道大家喜不喜欢赛车,反正我是赛车迷,极品飞车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关的告诉我啊,我请你客

回复列表 (共7个回复)

沙发

难!打第一关都没过...

1.光标闪得厉害,就没办法关闭光标吗?
2.控制起来不是很方便
3.如果解决了上面两个问题,效果应该就可以了

4.赛车可以跑到赛道外??
  按着右移,然后赛车就跑到跑道外了,再然后就死掉拉
  NNNNN     NNNNNNNNNNN    score : 230
  NNNNNN     NNNNNNNNNN
  NNNNNNN     NNNNNNNNN    ESC to exit gam
  NNNNNNN     NNNNNNNNN
  NNNNN     NNNNNNNNNNN    SPACE to pause
  NNNN     NNNNNNNNNNNN
  NNN     NNNNNNNNNNNNN
  NNN     NNNNNNNNNNNNN
  NNN     NNNNNNNNNNNNN
  NNN     ▲NNNNNNNNNNN
  

板凳

写出了这么个游戏 说明了到达了什么水平呢?!

3 楼

确实,一楼的问题我知道,稍微改一下就行了,但怎么清除光标呢?
2楼,我也只是初学者

4 楼

good!!

5 楼

#include <conio.h>
void _setcursortype (int cur_t);

int cur_t:
_NOCURSOR             关闭光标
_SOLIDCURSOR          方块光标
_NORMALCURSOR         普通底线型光标

6 楼

难度有点高!呵呵

7 楼

强,过第一关都难!!!!!!!!!!!

我来回复

您尚未登录,请登录后再回复。点此登录或注册