電腦遊戲製作開發設計論壇 首頁 電腦遊戲製作開發設計論壇
任何可以在PC上跑的遊戲都可以討論,主要以遊戲之製作開發為主軸,希望讓台灣的遊戲人有個討論、交流、教學、經驗傳承的園地
 
 常見問題常見問題   搜尋搜尋   會員列表會員列表   會員群組會員群組   會員註冊會員註冊 
 個人資料個人資料   登入檢查您的私人訊息登入檢查您的私人訊息   登入登入 

Google
DOS簡單射擊遊戲架構

 
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> Neilshih的專區
上一篇主題 :: 下一篇主題  
發表人 內容
neilshih
Neilshih專區 板主


註冊時間: 2007-06-03
文章: 33
來自: 地球
20.01 果凍幣

發表發表於: 2007-11-27, PM 10:55 星期二    文章主題: DOS簡單射擊遊戲架構 引言回覆

按左右可以移動,F鍵發射子彈。

這是我第一個寫的有圖案(?)的遊戲

以前剛開始寫程式的時候註解都沒寫@@

晚點我再補上

代碼:

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <windows.h>

struct Point{
    int x, y;
}bullet[25];

struct w_point{
    Point p1, p2;
}weapon;

void delay(float secs);               //延遲用函式
void gotoxy(int x, int y);            //選擇螢幕上座標
void move_bullet();                  //子彈移動
void fire_bullet();                  //發射子彈
void window_frame(int left, int top, int right, int bottom);   //畫出框框用
void draw_weapon(w_point *, int);      //畫出飛機
void cls_weapon(w_point, int);         //清除飛機(做出移動效果)
int index;
int bullet_x, bullet_y;
static int portvalue = 0;
static HANDLE hConsole = 0;
static int instanceCount = 0;

int main(){
    weapon.p1.x = 39;
    weapon.p1.y = 25;
    weapon.p2.x = 40;
    weapon.p2.y = 24;
    char ch, ch2;
    system("cls");
    draw_weapon(&weapon, 0);
   window_frame(30,1,50,20);
   gotoxy(10,3);
    while(1){
        bullet_x = weapon.p2.x;
        bullet_y = weapon.p2.y;
        if(kbhit()){
            ch = getch();
            if(tolower(ch) == 'f')
                fire_bullet();
            if(ch == -32){
                ch2 = getch();
                if(ch2 == 77 && weapon.p1.x < 77){
                    cls_weapon(weapon, 1);
                    draw_weapon(&weapon, 1);
                }
                if(ch2 == 75 && weapon.p1.x > 2){
                    cls_weapon(weapon, -1);
                    draw_weapon(&weapon, -1);
                }
            }
            if(tolower(ch) == 'q')
                break;
        }
        else
            move_bullet();
    }
   return 0;
}

void delay(float secs){
    float end;
    end = secs * CLOCKS_PER_SEC + clock();
    while(clock() < end);
}

void gotoxy(int x, int y){
    COORD coord;
   
    if( instanceCount == 0 ){
        hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
        instanceCount = 1;
    }
    coord.X = x - 1;
    coord.Y = y - 1;
   
    SetConsoleCursorPosition( hConsole, coord );
}

void move_bullet(){
    int i;
    delay(0.05);
    for(i = 1; i <= index; ++i){
        bullet[i].y--;
        if(bullet[i].y > 0){
            gotoxy(bullet[i].x,bullet[i].y+1);
            printf(" ");
            gotoxy(bullet[i].x,bullet[i].y);
            printf("\xdd");
        }
        else{
            gotoxy(bullet[i].x,bullet[i].y+1);
            if(index > 0){
                printf(" ");
                bullet[i].x = bullet[index].x;
                bullet[i--].y = bullet[index--].y;
            }
        }
    }
}

void fire_bullet(){
    ++index;
    bullet[index].x = bullet_x;
    bullet[index].y = bullet_y - 1;
    gotoxy(bullet[index].x, bullet[index].y);
    printf("\xdd");
    move_bullet();
}

void window_frame(int left, int top, int right, int bottom){
    int i;
    gotoxy(left, top);
        printf("\xc9");
    for(i = left+1; i < right; i++)
        printf("\xcd");
    printf("\xbb");
    gotoxy(left, bottom);
    printf("\xc8");
    for(i = left+1; i < right; i++)
        printf("\xcd");
    printf("\xbc");
    gotoxy(left, top+1);
    for(i = top; i < bottom;){
        printf("\xba");
        gotoxy(left, ++i);
    }
    gotoxy(right, top+1);
    for(i = top; i < bottom;){
        printf("\xba");
        gotoxy(right, ++i);
    }
}

void draw_weapon(w_point *w, int i){gotoxy(1,1);printf("draw:w->p1.x=%02d",w->p1.x);
    gotoxy(w->p1.x+=i, w->p1.y);
   printf("\xdb\xdb\xdb");
   gotoxy(w->p2.x+=i, w->p2.y);
   printf("\xdb");
}

void cls_weapon(w_point w, int i){gotoxy(1,2);printf("cls:w.p1.x=%02d",w.p1.x);
    gotoxy(w.p1.x, w.p1.y);
    printf("   ");
   gotoxy(w.p2.x, w.p2.y);
   printf(" ");
}
回頂端
檢視會員個人資料 發送私人訊息 發送電子郵件 雅虎訊息通 MSN Messenger
從之前的文章開始顯示:   
發表新主題   回覆主題    電腦遊戲製作開發設計論壇 首頁 -> Neilshih的專區 所有的時間均為 台灣時間 (GMT + 8 小時)
1頁(共1頁)

 
前往:  
無法 在這個版面發表文章
無法 在這個版面回覆文章
無法 在這個版面編輯文章
無法 在這個版面刪除文章
無法 在這個版面進行投票
可以 在這個版面附加檔案
可以 在這個版面下載檔案


Powered by phpBB © 2001, 2005 phpBB Group
正體中文語系由 phpbb-tw 維護製作