C语言实现打飞机简易游戏(半完成版)
2018-03-31 12:48
429 查看
打飞机简易游戏(半完成版),如图。
感谢 童晶 老师的教程,链接地址:http://study.163.com/forum/detail/1003961010.htm
未完成内容:①边框绘制 ②多个敌机 ③敌机子弹 ④战机生命 等等。
以下是代码。(在带c99标准的codeblocks下通过,在vc++6.0下未通过!)
*******************************************************************************************
// 打飞机简易游戏(未完成)。
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
int bullet_x,bullet_y;
int enemy_x,enemy_y;
int high_max,width_max;
int score;
int x,y;
void gotoxy(int,int);
void picture();
void changed();
void start();
void show();
void other();
void Hide();
int main(){
SetConsoleTitleA("hit planes"); //设置窗口名称。
system("mode con cols=30 lines=22"); //设置窗口大小。
start();
while(1){ //循环。
Hide();
show();
other();
changed();
}
return 0;
}
void gotoxy(int x, int y){ //坐标函数。
COORD coord;
coord.X = x;coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void start(){ //参数设置。
high_max = 20; //长度。即X轴。
width_max = 30; //宽度。即Y轴。
x = high_max / 2; //战机坐标。
y = width_max / 2;
bullet_x = -2; //子弹坐标。
bullet_y = y;
enemy_x = 0; //敌机坐标。
enemy_y = y;
score = 0; //得分
}
void show(){ //图形界面绘制。
gotoxy(0,0);
int i,j,k;
for(i = 0;i < high_max;i++){
for(j = 0;j < width_max;j++){
if((i == x) && (j == y))
picture();
else if((i == enemy_x) && (j == enemy_y))
printf("O");
else if((i == bullet_x + 1) && (j == bullet_y))
printf("|");
else if((i == 0) || (i == high_max - 1))
printf("-");
else if((j == 0) || (j == width_max - 1))
printf("|");
else
printf(" ");
}
printf("\n");
}
for(k = 0;k < (width_max / 2) - 5;k++)
printf(" ");
printf("score:%d\n",score); //得分。
}
void picture(){ //战机图形。
printf("*");
}
void changed(){ //操作控制。不区分大小写。WSAD 上下左右,J发射子弹 ,P 暂停,ESC 退出。
char sizein;
if(kbhit()){
sizein = getch();
if(((sizein == 'a') || (sizein == 'A')) && (y > 1))
y--;
if(((sizein == 'd') || (sizein == 'D')) && (y < width_max - 2))
y++;
if(((sizein == 's') || (sizein == 'S')) && (x < high_max - 2))
x++;
if(((sizein == 'w') || (sizein == 'W')) && (x > 1))
x--;
if(sizein == 27)
exit(0);
if((sizein == 'p') || (sizein == 'P'))
system("pause > nul");
if((sizein == 'j') || (sizein == 'J')){
bullet_x = x - 1;
bullet_y = y;
}
}
}
void other(){ //敌机循环生成。
if(bullet_x > -1);
bullet_x--;
if((bullet_x == enemy_x) && (bullet_y == enemy_y)){
score++;
enemy_x = -1;
enemy_y = (rand() % (width_max - 1) + 1);
bullet_x = -2;
}
if((enemy_x > high_max) || (enemy_y < 1))
{
enemy_x = -1;
enemy_y = (rand() % (width_max - 1) + 1);
}
static int speed = 0;
if(speed < 10) //调整敌机移动速度。
speed++;
if(speed == 10){
enemy_x++;
speed = 0;
}
}
void Hide(){ //隐藏光标函数调用。
CONSOLE_CURSOR_INFO CURSORINFO = {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CURSORINFO);
}
******************************************************************************************
感谢 童晶 老师的教程,链接地址:http://study.163.com/forum/detail/1003961010.htm
未完成内容:①边框绘制 ②多个敌机 ③敌机子弹 ④战机生命 等等。
以下是代码。(在带c99标准的codeblocks下通过,在vc++6.0下未通过!)
*******************************************************************************************
// 打飞机简易游戏(未完成)。
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
int bullet_x,bullet_y;
int enemy_x,enemy_y;
int high_max,width_max;
int score;
int x,y;
void gotoxy(int,int);
void picture();
void changed();
void start();
void show();
void other();
void Hide();
int main(){
SetConsoleTitleA("hit planes"); //设置窗口名称。
system("mode con cols=30 lines=22"); //设置窗口大小。
start();
while(1){ //循环。
Hide();
show();
other();
changed();
}
return 0;
}
void gotoxy(int x, int y){ //坐标函数。
COORD coord;
coord.X = x;coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void start(){ //参数设置。
high_max = 20; //长度。即X轴。
width_max = 30; //宽度。即Y轴。
x = high_max / 2; //战机坐标。
y = width_max / 2;
bullet_x = -2; //子弹坐标。
bullet_y = y;
enemy_x = 0; //敌机坐标。
enemy_y = y;
score = 0; //得分
}
void show(){ //图形界面绘制。
gotoxy(0,0);
int i,j,k;
for(i = 0;i < high_max;i++){
for(j = 0;j < width_max;j++){
if((i == x) && (j == y))
picture();
else if((i == enemy_x) && (j == enemy_y))
printf("O");
else if((i == bullet_x + 1) && (j == bullet_y))
printf("|");
else if((i == 0) || (i == high_max - 1))
printf("-");
else if((j == 0) || (j == width_max - 1))
printf("|");
else
printf(" ");
}
printf("\n");
}
for(k = 0;k < (width_max / 2) - 5;k++)
printf(" ");
printf("score:%d\n",score); //得分。
}
void picture(){ //战机图形。
printf("*");
}
void changed(){ //操作控制。不区分大小写。WSAD 上下左右,J发射子弹 ,P 暂停,ESC 退出。
char sizein;
if(kbhit()){
sizein = getch();
if(((sizein == 'a') || (sizein == 'A')) && (y > 1))
y--;
if(((sizein == 'd') || (sizein == 'D')) && (y < width_max - 2))
y++;
if(((sizein == 's') || (sizein == 'S')) && (x < high_max - 2))
x++;
if(((sizein == 'w') || (sizein == 'W')) && (x > 1))
x--;
if(sizein == 27)
exit(0);
if((sizein == 'p') || (sizein == 'P'))
system("pause > nul");
if((sizein == 'j') || (sizein == 'J')){
bullet_x = x - 1;
bullet_y = y;
}
}
}
void other(){ //敌机循环生成。
if(bullet_x > -1);
bullet_x--;
if((bullet_x == enemy_x) && (bullet_y == enemy_y)){
score++;
enemy_x = -1;
enemy_y = (rand() % (width_max - 1) + 1);
bullet_x = -2;
}
if((enemy_x > high_max) || (enemy_y < 1))
{
enemy_x = -1;
enemy_y = (rand() % (width_max - 1) + 1);
}
static int speed = 0;
if(speed < 10) //调整敌机移动速度。
speed++;
if(speed == 10){
enemy_x++;
speed = 0;
}
}
void Hide(){ //隐藏光标函数调用。
CONSOLE_CURSOR_INFO CURSORINFO = {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CURSORINFO);
}
******************************************************************************************
相关文章推荐
- 【C语言】简易扫雷游戏——C语言实现
- linux下使用C语言实现2048简易字符界面游戏
- 【算法】C语言实现简易的扑克牌游戏
- C语言实现简易版扫雷游戏
- C语言实现扫雷小游戏(设定游戏简易程度,实现空白,实现跳过第一次选中雷)
- c语言实现简易三子棋游戏
- 简易小游戏扫雷——C语言编程实现
- 用c语言实现简单三子棋游戏
- C语言实现三子棋游戏(意义版)
- 通过简单的C语言编程实现一个简易…
- C语言借助EasyX实现的生命游戏源码
- 简易小游戏(类似打飞机)的简单实现cocos2d-x-2.1.5
- C语言实现简单的记忆力测试游戏
- C语言-实现一个简易的银行排号叫号系统
- 简易游戏修改器(C#实现,提供程序下载)
- c语言实现的扫雷游戏
- 用C语言 实现猜数字游戏
- php实现的简易扫雷游戏实例
- Unity实现自己的简易游戏窗口管理器
- Linux环境下C语言实现贪吃蛇游戏