原始碼如下
要求敵機速度隨時間增加
麻煩大佬們幫幫忙吧!
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
int position_x, position_y;//飛機位置
int wide, high;//區域
int enemy_x, enemy_y;//敵機位置
int victor;
void HideCusor()//隱藏游標
{
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };//第二個值為0,表示隱藏游標
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)//游標移動
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void startup()//初始化
{
wide = 30;
high = 20;
position_x = wide / 2;
position_y = high - 3;
enemy_x = 5;
enemy_y = 0;
victor = 1;
HideCusor();
}
void show()
//功能:顯示游戲界面
{
gotoxy(0, 0);//游標至原點
int i, j;//行計數,列計數
for (i = 0;i < high;i++)
{
for (j = 0;j < wide;j++)
{
if ((i == position_y) && (j == position_x))//飛機
printf("*");
else if ((i == enemy_y) && (j == enemy_x))//敵機
printf("$");
else
printf(" ");
if ((enemy_x == position_x) && (enemy_y == position_y))//判斷勝負
{
enemy_y = -1;
victor = 0;
}
}
printf("\n");
}
}
void UpdateWithoutInput()
{
//功能:控制敵機移動
static int speed = 0;//減慢$下降的速度
if (speed < 20)
speed++;
if (speed == 20)
{
if (enemy_y > high)
{
enemy_x = rand() % 31;
enemy_y = 0;
}
else
{
enemy_y++;
speed = 0;
}
}
}
void UpdateWithInput()
//功能:通過按鍵控制己方移動
{
char input;
if (_kbhit())
{
input = _getch();//輸入
if (input == 'w')
position_y--;
if (input == 's')
position_y++;
if (input == 'a')
position_x--;
if (input == 'd')
position_x++;
}
}
int main()
{
startup();
while (victor)
{
show();
UpdateWithoutInput();
UpdateWithInput();
}
printf("GAME OVER!\nhaha\n");
return 0;
}
uj5u.com熱心網友回復:
寫個timer函式,定時呼叫UpdateWithoutInput函式就可以了uj5u.com熱心網友回復:
void UpdateWithoutInput()
{
//功能:控制敵機移動
static int speed = 1;//減慢$下降的速度
if (speed < 20) {
speed++;
}
if (speed == 20)
{
if (enemy_y > high)
{
enemy_x = rand() % 31;
enemy_y = 0;
mySpeed++;//mySpeed是全域變數,初始值為1,mySpeed值越大速度越快
if (mySpeed >= 5)
{
mySpeed = 1;
}
}
else
{
enemy_y+= mySpeed;//----改變飛機速度-----
speed = 0;
}
}
}
if ((enemy_x == position_x) && (enemy_y >= position_y))//判斷勝負 ,把==改為>=
{
enemy_y = -1;
victor = 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/206165.html
標籤:C++ 語言
下一篇:回文陷阱
