可以提供个简单的飞机游戏的C代码吗?
时间: 2023-02-12 15:26:35 浏览: 72
以下是一个简单的飞机游戏的C代码示例:
```
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
/* 画出飞机 */
setcolor(RED);
line(150, 250, 180, 300);
line(180, 300, 210, 250);
line(210, 250, 210, 270);
line(210, 270, 150, 270);
line(150, 270, 150, 250);
/* 画出火花 */
setcolor(YELLOW);
line(170, 290, 170, 310);
line(190, 290, 190, 310);
getch();
closegraph();
return 0;
}
```
这是一个简单的飞机游戏的代码示例,它使用了graphics.h库在屏幕上画出了一个飞机的图形。这个游戏只有一个静态的图像,如果要实现更多的功能,需要更多的代码和编程知识。
相关问题
用c语言写出一个打飞机小游戏代码
以下是一个简单的打飞机小游戏的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 25
// 定义飞机的位置和状态
int planeX = SCREEN_WIDTH / 2;
int planeY = SCREEN_HEIGHT - 2;
int planeState = 0; // 0: 未被击中,1: 已被击中
// 定义子弹的位置和状态
int bulletX = -1;
int bulletY = -1;
int bulletState = 0; // 0: 不存在,1: 存在
// 定义敌机的位置和状态
int enemyX = -1;
int enemyY = -1;
int enemyState = 0; // 0: 不存在,1: 存在
// 清屏函数
void clearScreen() {
system("cls");
}
// 设置光标位置函数
void setCursorPosition(int x, int y) {
COORD position = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}
// 显示飞机函数
void showPlane() {
setCursorPosition(planeX, planeY);
printf("^");
}
// 移动飞机函数
void movePlane(int x, int y) {
if (planeState == 1) {
return;
}
if (x < 0 || x >= SCREEN_WIDTH || y < 0 || y >= SCREEN_HEIGHT) {
return;
}
planeX = x;
planeY = y;
}
// 显示子弹函数
void showBullet() {
if (bulletState == 0) {
return;
}
setCursorPosition(bulletX, bulletY);
printf("|");
}
// 移动子弹函数
void moveBullet() {
if (bulletState == 0) {
return;
}
bulletY--;
if (bulletY < 0) {
bulletState = 0;
}
}
// 显示敌机函数
void showEnemy() {
if (enemyState == 0) {
return;
}
setCursorPosition(enemyX, enemyY);
printf("V");
}
// 移动敌机函数
void moveEnemy() {
if (enemyState == 0) {
return;
}
enemyY++;
if (enemyY >= SCREEN_HEIGHT) {
enemyState = 0;
}
}
// 发射子弹函数
void fireBullet() {
if (bulletState == 1) {
return;
}
bulletX = planeX;
bulletY = planeY - 1;
bulletState = 1;
}
// 发射敌机函数
void fireEnemy() {
if (enemyState == 1) {
return;
}
enemyX = rand() % SCREEN_WIDTH;
enemyY = 0;
enemyState = 1;
}
// 判断是否碰撞函数
int isCollide(int x1, int y1, int x2, int y2) {
if (x1 == x2 && y1 == y2) {
return 1;
}
return 0;
}
// 游戏循环函数
void gameLoop() {
while (1) {
clearScreen();
showPlane();
showBullet();
showEnemy();
if (_kbhit()) {
int ch = _getch();
switch (ch) {
case 'a':
movePlane(planeX - 1, planeY);
break;
case 'd':
movePlane(planeX + 1, planeY);
break;
case 's':
movePlane(planeX, planeY + 1);
break;
case ' ':
fireBullet();
break;
}
}
moveBullet();
moveEnemy();
if (isCollide(bulletX, bulletY, enemyX, enemyY)) {
bulletState = 0;
enemyState = 0;
}
if (isCollide(planeX, planeY, enemyX, enemyY)) {
planeState = 1;
break;
}
Sleep(100);
}
clearScreen();
setCursorPosition(SCREEN_WIDTH / 2 - 5, SCREEN_HEIGHT / 2);
printf("Game Over");
}
// 主函数
int main() {
gameLoop();
return 0;
}
```
这个小游戏使用了 Windows 平台的控制台窗口作为游戏界面,使用了 conio.h 库来获取键盘输入,使用了 windows.h 库来设置控制台窗口的光标位置和清屏。游戏循环中使用了 Sleep() 函数来控制游戏速度。
c语言飞机小游戏代码
这个C语言飞机小游戏代码主要包括两个部分:飞机图案打印和飞机移动控制。飞机图案打印部分使用了for循环来移动光标到需要打印飞机图案的行和列,并使用printf函数来打印飞机图案。飞机移动控制部分使用了getch函数来获取玩家从键盘输入的字符,根据不同的字符来控制飞机的移动方向。具体实现细节可以参考下面的代码:
```
#include <stdio.h>
#include <conio.h> //需要用到getch函数
int main()
{
int x = 5, y = 10; //飞机初始位置
int ch; //存储玩家输入的字符
int isFire = 0; //是否发射激光
int isKilled = 0; //是否击中靶子
int ny = 30; //靶子所在行数
while (1) //游戏循环
{
system("cls"); //清屏
//打印靶子
if (!isKilled)
{
for (int j = 0; j < ny; j++)
printf(" ");
printf("+\n");
}
//打印飞机
for (int i = 0; i < x; i++)
printf("\n");
for (int j = 0; j < y; j++)
printf(" ");
printf(" *\n");
for (int j = 0; j < y; j++)
printf(" ");
printf("*****\n");
for (int j = 0; j < y; j++)
printf(" ");
printf(" * * \n");
//控制飞机移动
if (_kbhit()) //检测是否有键盘输入
{
ch = _getch(); //获取键盘输入的字符
if (ch == 'a' || ch == 'A')
y--;
if (ch == 'd' || ch == 'D')
y++;
if (ch == 'w' || ch == 'W')
x--;
if (ch == 's' || ch == 'S')
x++;
if (ch == ' ')
isFire = 1;
}
//打印激光
if (isFire == 1)
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
printf(" ");
printf(" |\n");
}
if (y + 2 == ny)
isKilled = 1;
isFire = 0;
}
//判断游戏是否结束
if (isKilled == 1)
{
printf("\n\n\n\n\n");
printf("Game Over!\n");
break;
}
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)