當前位置:首頁 » 編程語言 » c語言控制飛機移動
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言控制飛機移動

發布時間: 2023-02-26 18:28:37

A. 飛機的控製程序用什麼編程語言寫的

c語言,底層的東西用c語言實現的較多

B. 有什麼好玩的C語言小程序

一個「殲滅敵機」的小游戲,DEVc++編譯通過:

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <windows.h>

#include <time.h>

#define zlx 10 //增量坐標(x)讓游戲框不靠邊

#define zly 3 //增量坐標(y)讓游戲框不靠邊

#define W 26 //游戲框的寬度

#define H 24 //游戲框的高度

int jiem[22][22]={0}, wj=10; //界面數組, 我機位置(初值為10)

int speed=4,density=30, score=0,death=0; //敵機速度, 敵機密度, 玩家成績,死亡次數

int m=0,n=0; // m,n是控制敵機的變數

void gtxy (int x, int y) //控制游標位置的函數

{ COORD pos;

pos.X = x; pos.Y = y;

SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );

}

voidColor(inta) //設定顏色的函數(a應為1-15)

{ SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), a ); }

void yinc(int x=1,int y=0) //隱藏游標的函數

{ CONSOLE_CURSOR_INFO gb={x,y}; //y設為0即隱藏

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &gb);

}

void csh( ) //初始化函數

{ int i;

Color(7);

gtxy(zlx,zly); printf("╔"); gtxy(zlx+W-2,zly); printf("╗"); //左上角和右上角的框角

gtxy(zlx,zly+H-1); printf("╚"); gtxy(zlx+W-2,zly+H-1); printf("╝"); //下邊兩框角

for(i=2;i<W-2;i+=2) {gtxy(zlx+i,zly); printf("═"); } //列印上橫框

for(i=2;i<W-2;i+=2) {gtxy(zlx+i,zly+H-1); printf("═"); } //列印下橫框

for(i=1;i<H-1;i++) { gtxy(zlx,zly+i); printf("║"); } //列印左豎框

for(i=1;i<H-1;i++) {gtxy(zlx+W-2,zly+i); printf("║"); } //列印右豎框

Color(14);gtxy(19,2); printf("殲滅敵機"); Color(10);

gtxy(37,5); printf("設置:Esc ");

gtxy(37,7); printf("發射:↑ ");

gtxy(37,9); printf("控制:← → ");

gtxy(37,11);printf("得分:%d",score);

gtxy(37,13); printf("死亡:%d",death);

yinc(1,0);

}

void qcjm( ) //清除界面函數

{int i,j;

for(i=0;i<H-2;i++)

for(j=0;j<W-4;j++){gtxy(zlx+2+j,zly+1+i);printf(" ");}

}

void feiji( ) //飛機移動函數

{int i,j;

for(i=21;i>=0;i--) //從底行往上是為了避免敵機直接沖出數組

for(j=0;j<22;j++)

{if(i==21&&jiem[i][j]==3)jiem[i][j]=0; //底行賦值0 以免越界

if(jiem[i][j]==3)jiem[i][j]=0, jiem[i+1][j]=3;

}

if(jiem[20][wj]==3&&jiem[21][wj]==1) death++;

}

void zidan( ) //子彈移動函數

{ int i,j;

for(i=0;i<22;i++)

for(j=0;j<22;j++)

{if(i==0&&jiem[i][j]==2) jiem[i][j]=0;

if(jiem[i][j]==2) {if(jiem[i-1][j]==3) score+=100,printf("7");

jiem[i][j]=0,jiem[i-1][j]=2; }

}

}

void print( ) //輸出界面函數

{int i,j;

qcjm( );

for(i=0;i<22;i++)

for(j=0;j<22;j++)

{ gtxy(12+j,4+i);

if(jiem[i][j]==3) {Color(13);printf("□");}

if(jiem[i][j]==2) {Color(10);printf(".");}

if(jiem[i][j]==1) {Color(10);printf("■");}

}

gtxy(37,11); Color(10);printf("得分:%d",score);

gtxy(37,13); printf("死亡:%d",death);

}

void setting( ) //游戲設置函數

{ qcjm( );

gtxy(12,4);printf("選擇敵機速度:");

gtxy(12,5);printf(" 1.快 2.中 3.慢>>");

switch(getche( ))

{case '1': speed=2; break;

case '2': speed=4; break;

case '3': speed=5; break;

default: gtxy(12,6);printf(" 錯誤!默認值");

}

gtxy(12,7);printf("選擇敵機密度:");

gtxy(12,8);printf(" 1.大 2.中 3.小>>");

switch(getche( ))

{case '1': density=20; break;

case '2': density=30;break;

case '3': density=40; break;

default: gtxy(12,9);printf(" 錯誤!默認值");

}

for(int i=0;i<22;i++)

for(int j=0;j<22;j++)jiem[i][j]=0;

jiem[21][wj=10]=1; jiem[0][5]=3;

gtxy(12,10);printf(" 按任意鍵保存...");

getch( );

qcjm( );

}

void run( ) //游戲運行函數

{ jiem[21][wj]=1; //值為1代表我機(2則為子彈)

jiem[0][5]=3; //值為3代表敵機

SetConsoleTitle("殲滅敵機"); //設置窗口標題

while(1)

{if (kbhit( )) //如有鍵按下,控制我機左右移動、發射或進行設定

{int key;

if((key=getch( ))==224) key=getch( );

switch(key)

{ case 75: if(wj>0) jiem[21][wj]=0,jiem[21][--wj]=1; break;

case 77: if(wj<20)jiem[21][wj]=0,jiem[21][++wj]=1;break;

case 72: jiem[20][wj]=2; break;

case 27: setting( );

}

}

if(++n%density==0) //控制產生敵機的速度

{ n=0;srand((unsigned)time(NULL));

jiem[0][rand( )%20+1]=3;

}

if(++m%speed==0) {feiji( ); m=0;} //控制敵機移動速度(相對子彈而言)

zidan( );

print( );

Sleep(120); //延時120毫秒

}

}

int main( )

{csh( );

run( );

return 0;

}

新手要方便寫代碼,可以收藏下面幾個自編函數:

  • SetConsoleTitle("俄羅斯方塊"); //設置窗口左上角標題欄處出現"俄羅斯方塊"5個字

  • srand( (unsigned) time(NULL) ); //初始化隨機數發生器

  • n= rand( ) % 20; //產生隨機數0-19中的一個. 如 rand( )%5 就產生0-4中的一個數

    SetConsoleTitle( )函數在<windows.h>里,srand( )函數與rand( )函數要配合用,

    就是同時要用,在<stdlib.h>里。如果 rand( )%10+1 就產生1-10之中的一個數。

  • Sleep(300); //延時300毫秒(就是程序暫停300毫秒後繼續運行)

  • system("cls"); //清屏(把窗口裡的內容全部清除,游標定於(0,0)位置處)

    這兩個函數都在<windows.h>里。開頭4個自編函數 編寫如下:

  • void gtxy (int x, int y) //控制游標位置的函數

    { COORD pos;

    pos.X = x;

    pos.Y = y;

    SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );

    }

    void Color (int a) //設定顏色的函數

    { SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ),a ); }

    void yinc (int x,int y) //隱藏游標的函數

    { CONSOLE_CURSOR_INFO gb={ x , y }; //gb代表游標

    SetConsoleCursorInfo ( GetStdHandle(STD_OUTPUT_HANDLE), &gb );

    }

    void kou(int w,int h) //設置窗口大小的函數

    {HANDLE hl=GetStdHandle ( STD_OUTPUT_HANDLE ) ;

    COORD size={ w , h };

    SetConsoleScreenBufferSize( hl , size );

    SMALL_RECT rc={ 0, 0, w, h };

    SetConsoleWindowInfo( hl, 1, &rc );

    }

    最後這個函數,參數w是寬h是高。里邊5行中第一行定義了句柄型變數hl,並給它賦值。

    第二行定義了坐標型結構體變數size,它的取值決定了緩沖區的大小。第三行就是使用

    size的值設置好緩沖區大小。第四行定義了變數rc,它的值決定當前窗口顯示的位置與

    大小(不得超過緩沖區的大小)。前兩個0,0是從緩沖區左上角0列0行位置處開始,後兩

    個參數可以小於w和h.比如rc={0,0,w-10,h-5}; 最後一行使用rc的值設置好窗口,中間

    那個參數要為" 1 "或寫「 true 」才有效。

C. 關於C語言實現飛機大戰。為什麼這個函數能實現速度的改變,明明,speed和飛機沒有綁定。

enermy_x++是控制敵機坐標。
每次while循環都會執行一遍你發的函數。
speed為10的時候,while循環10次,敵機的坐標發生一次變化。
speed為1時,while每次循環,敵機的坐標就變化一次。那你說speed是不是跟敵機的速度有關呢?

D. c語言寫飛機大戰,為什麼我的飛機坐標不會隨著滑鼠移動而變化

如果不能隨著滑鼠移動的話,那麼證明你們起的語言的話是沒有跟隨飛機大戰那個滑鼠動的。

E. 飛機的C語言程序代碼

我有坦克的

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LENTH 7
#define WIDTH 155

#define SLEEP_TIME 30000
#define ENTER_NUM 20

char fun_ascii[LENTH][WIDTH] = {
" | ",
" _\\--__|_ ",
" II=======OOOOO[/ *02 ___| ",
" ____\\______|/-----.",
" /____________________|",
" \\@@@@@@@@@@@@@@@@@@@/ ",
" ~~~~~~~~~~~~~~~~~~~ "
};

int g_shoot_pos;
int g_shoot_pos_static;

void display_ascii(void);
void print_enter(void);

void move_ascii(void)
{
int i, j;

for (i = 0; i < LENTH; i++) {
for (j = 0; j < WIDTH; j++) {
if (fun_ascii[i][j] != ' ') {
fun_ascii[i][j - 1] = fun_ascii[i][j];
fun_ascii[i][j] = ' ';
}
}
}
}

void shoot_init(void)
{
int i, j;

for (j = 0; j < WIDTH; j++) {
if (fun_ascii[2][j] != ' ')
break;
}
--j;

g_shoot_pos_static = g_shoot_pos = j;
}

void shoot_begin(void)
{
fun_ascii[2][g_shoot_pos] = 'O';

system("clear");
print_enter();
display_ascii();
fun_ascii[2][g_shoot_pos] = ' ';
}

void shoot_ascii(void)
{
int i;

i = WIDTH / 2 - 1;

while (i > 0 && g_shoot_pos > 0) {
shoot_begin();
g_shoot_pos--;
usleep(SLEEP_TIME);
i--;
}
}

void do_shoot_ascii(void)
{
int i = 3;

shoot_init();

while (i > 0) {
shoot_ascii();
g_shoot_pos = g_shoot_pos_static;
i--;
}
}

void display_ascii(void)
{
int i, j;

for (i = 0; i < LENTH; i++) {
for (j = 0; j < WIDTH; j++)
printf("%c", fun_ascii[i][j]);
printf("\n");
}
}

void print_enter(void)
{
int i;

for (i = 0; i < ENTER_NUM; i++)
printf("\n");
}

int main(void)
{
int i = WIDTH;

while (i > 0) {
if (i == (WIDTH / 2)) {
do_shoot_ascii();
i--;
continue;
}
system("clear");
print_enter();
move_ascii();
display_ascii();
i--;
usleep(SLEEP_TIME);
}

return 0;
}