自己做的一個C語言小遊戲——吃金子

來源:互聯網
上載者:User

          本周資料結構課程設計,哥挑了個簡單點的題目,用C語言編寫了一個圖形介面的吃金子小遊戲。

基本想法是:

      定義了一個數組,通過資料對應到螢幕的座標。

       然後通過一些畫圖函數,畫出淘金者(紅色圓)和金子(黃色圓)。

       通過鍵盤的上下左右鍵控制淘金者的上下左右移動。

      每隔六秒鐘,遊戲介面會刷一次,此時金子會更換位置。

      如果你是高手,一分鐘吃到了20個那你就Win,如果你碰到牆壁,Game Over。

      此遊戲超挫,屬於版本一,有興趣的下下來玩玩也不錯,當然沒有那些Flash做的Beautiful。呵呵。

下面是原始碼和一些注釋:

/*
Version: v1.0
Author:xufeiyang
DateTime:2010.12.13

IDE:Turbo C 2.0
*/
/************************************/
#include "graphics.h" /*標頭檔*/
#include "time.h"
#include "stdlib.h"
#include "bios.h"
#include "dos.h"
#include "stdio.h"
#define ESC 0x11b /*鍵盤掃描碼*/
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define F1 0x3b00
#define RIGHT 0x4d00
#define YES 0x1579
#define NO 0x316e
#define RESTART 0x1372
/****************************************/
time_t start, end;
int diff;
int oldtime;
int goldnum = 0;
int Bord[20][20]=
{
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,
        2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
};
typedef struct {        /* hunger */
    int cirx;
    int ciry;
    int cirradius;
}Hunger, Gold;

Hunger hunger;
Gold gold;

int oldGoldx;
int oldGoldy;

void initBord();
void drawHunger();
void updateHunger();
void drawGold();
void updateGold();
void randomBean();
void printSum();
void delSum();
int justify();
void moveDown();
void moveUp();
void moveRight();
void moveLeft();
void winGame();
void loseGame();

int main()
{
    int x, y;
    int key = 0;
    int result;
    time_t st;

    hunger.cirradius = 9;    /*初始化淘金者和金子的半徑*/
    gold.cirradius = 5;

    initBord();

    while(1)
    {
        end = time(NULL);
        while(!kbhit() || !(key = bioskey(0)))
        {
            end = time(NULL);
            diff = difftime(end, start);
            if(diff % 6 == 0)
            {
                updateGold();
                randomBean();
                break;
            }
        }
        if(key)
        {
            switch(key)
            {
            case DOWN:
                moveDown();
                break;
            case UP:
                moveUp();
                break;
            case LEFT:
                moveLeft();
                break;
            case RIGHT:
                moveRight();
                break;
            case ESC:
                sleep(2);
                closegraph();
                exit(1);
            case RESTART:
                closegraph();
                initBord();
                break;
            } /* switch */
            delSum();
            printSum();
            key = 0;
        }    /* if */
/*        if((59 - diff)%4 == 0)
        {
            updateGold();
            randomBean();
        }
*/
        result = justify();
        if(result == 1)
        {
            winGame();
            break;
        }
        else if(result == 0)
        {
            loseGame();
            break;
        }
        else
            continue;
    }
    sleep(2);
    closegraph();
    return 0;
}
/*********************************************/
void initBord()
{
    int gd = VGA, errorcode;
    int gm = VGAHI;
    int x, y;
    srand((unsigned int)time(NULL));
    initgraph(&gd, &gm,"");
    errorcode = graphresult();
    if(errorcode < 0)
    {
        printf("Graphics error:%s/n", grapherrormsg(errorcode));
        printf("Press any key to halt:");
        exit(1);
    }
    cleardevice();
    start = time(NULL);

    do
    {
        x = 1 + rand()%18;
        y = 1 + rand()%18;
    }while(Bord[x][y] == 2);
    setbkcolor(9);
    hunger.cirx = x;
    hunger.ciry = y;
    drawHunger();
        printSum();
        randomBean();
        setcolor(BLUE);
        moveto(39, 39);
        lineto(39, 439);
        linerel(400, 0);
        linerel(0, -400);
        linerel(-400, 0);
        moveto(59, 59);
        lineto(59, 419);
        linerel(360, 0);
        linerel(0, -360);
        linerel(-360, 0);

        moveto(449, 39);
        lineto(589, 39);
        linerel(0, 400);
        linerel(-140, 0);
        linerel(0, -400);
}
void winGame()
{
    setcolor(RED);
    setfillstyle(SOLID_FILL, RED);
    outtextxy(getmaxx()/2+20, getmaxy()/2,"Y O U  W I N ! ");
}
void loseGame()
{
    setcolor(RED);
    setfillstyle(SOLID_FILL, RED);
    outtextxy(getmaxx()/2+20, getmaxy()/2," GAME OVER !");
}
int justify()
{
    if(hunger.ciry >= 19 || hunger.ciry <= 0
        || hunger.cirx >= 19 || hunger.cirx <= 0
        || (diff > 60 && goldnum < 30))
    {
        cleardevice();
        return 0;    /*撞牆者死*/
    }
    else if(diff <= 60 && goldnum >= 30)
    {
        cleardevice();
        return 1;
    }
    else
        return 2;
}
void drawHunger()
{
    Bord[hunger.cirx][hunger.ciry] = 3;
    setcolor(RED);
    circle(69 + (hunger.cirx-1) * 20, 69 + (hunger.ciry - 1) * 20, hunger.cirradius);
    setfillstyle(SOLID_FILL, RED);
    floodfill(69+(hunger.cirx-1)*20-1, 69+(hunger.ciry-1)*20-1,RED);
}
void updateHunger()
{
    Bord[hunger.cirx][hunger.ciry] = 1;
    setcolor(0);
    circle(69 + (hunger.cirx-1) * 20, 69 + (hunger.ciry - 1) * 20, hunger.cirradius);
    setfillstyle(SOLID_FILL, 0);
    floodfill(69+(hunger.cirx-1)*20-1, 69+(hunger.ciry-1)*20-1,0);
}
void randomBean()
{
    int x, y;
    do
    {
        x = 1 + rand() % 18;
        y = 1 + rand() % 18;
    }while(Bord[x][y] == 3);
    gold.cirx = x;
    gold.ciry = y;
    drawGold();
}
void drawGold()
{
    oldGoldx = gold.cirx;
    oldGoldy = gold.ciry;
    Bord[gold.cirx][gold.ciry] = 0;
    setcolor(YELLOW);
    circle(69 + (gold.cirx - 1) * 20, 69 + (gold.ciry - 1) * 20, gold.cirradius);
    setfillstyle(SOLID_FILL, YELLOW);
    floodfill(69+(gold.cirx-1)*20-2, 69+(gold.ciry-1)*20-2,YELLOW);
}
void updateGold()
{
    Bord[gold.cirx][gold.ciry] = 1;
    setcolor(0);
    circle(69 + (gold.cirx - 1) * 20, 69 + (gold.ciry - 1) * 20, gold.cirradius);
    setfillstyle(SOLID_FILL, 0);
    floodfill(69+(gold.cirx-1)*20-2, 69+(gold.ciry-1)*20-2, 0);
}
void printSum()
{
    char c[10], a[10];
    memset(c,0x00, sizeof(c));
    memset(a, 0x00, sizeof(a));
    oldtime = 60-diff;
    sprintf(c,"%d", goldnum);
    sprintf(a,"%d", 60-diff);
    /*    itoa(goldnum, c, sizeof(c));
    itoa(60-diff,a,sizeof(a));
    */
    setcolor(11);
    outtextxy(480,120,"60S Eat 30");
    outtextxy(453,140,">>EAT BEAN GAME<<");
    outtextxy(510,160,c);
    outtextxy(480,200,"SECOND LEFT ");
    setcolor(4);
    outtextxy(500,220,a);
}
void delSum()
{
    char c[10], a[10];
    memset(c, 0x00, sizeof(c));
    memset(a, 0x00, sizeof(a));
    sprintf(c,"%d", goldnum - 1);
    sprintf(a,"%d", oldtime);

    setcolor(0);
    outtextxy(510,160,c);

    setcolor(0);
    outtextxy(500,220,a);
}
void moveDown()
{
    updateHunger();
    hunger.ciry ++;
    if(Bord[hunger.cirx][hunger.ciry] == 0)
    {
        goldnum++;
        updateGold();
        randomBean();
    }
    drawHunger();
}
void moveUp()
{
    updateHunger();
    hunger.ciry --;
    if(Bord[hunger.cirx][hunger.ciry] == 0)
    {
        goldnum++;
        updateGold();
        randomBean();
    }
    drawHunger();
}
void moveRight()
{
    updateHunger();
    hunger.cirx++;
    if(Bord[hunger.cirx][hunger.ciry] == 0)
    {
        goldnum++;
        updateGold();
        randomBean();
    }
    drawHunger();
}
void moveLeft()
{
    updateHunger();
    hunger.cirx --;
    if(Bord[hunger.cirx][hunger.ciry] == 0)
    {
        goldnum++;
        updateGold();
        randomBean();
    }
    drawHunger();
}

俺直接定義成全域函數的,沒有用參數傳遞,估計兄弟姐妹們看了會有點暈。呵呵,下次做之前一定要想好最佳化一下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.