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(" ");
}
|
|
|