用C语言程序做一个飞机大战游戏

时间: 2024-01-13 19:04:45 浏览: 23
好的,下面是一个简单的飞机大战游戏的C语言程序,你可以根据自己的需求进行修改和优化。 ```c #include <stdio.h> #include <conio.h> #include <windows.h> #include <time.h> #define WIDTH 40 #define HEIGHT 20 int score = 0; void setCursorPosition(int x, int y) { COORD position = {x, y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position); } void hideCursor() { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void drawBorder() { for (int i = 0; i < WIDTH + 2; ++i) { setCursorPosition(i, 0); printf("#"); setCursorPosition(i, HEIGHT + 1); printf("#"); } for (int i = 0; i < HEIGHT + 2; ++i) { setCursorPosition(0, i); printf("#"); setCursorPosition(WIDTH + 1, i); printf("#"); } } void drawPlane(int x, int y) { setCursorPosition(x, y); printf(" /\\ "); setCursorPosition(x, y + 1); printf(" / \\ "); setCursorPosition(x, y + 2); printf("/____\\"); } void drawBullet(int x, int y) { setCursorPosition(x, y); printf("*"); } void drawEnemy(int x, int y) { setCursorPosition(x, y); printf(" | "); setCursorPosition(x, y + 1); printf("\\_o_/"); setCursorPosition(x, y + 2); printf(" / \\ "); } void updateScore() { setCursorPosition(WIDTH + 4, 3); printf("Score: %d", score); } int main() { hideCursor(); drawBorder(); drawPlane(WIDTH / 2 - 3, HEIGHT - 3); int planeX = WIDTH / 2 - 3; int bulletX = -1; int bulletY = -1; int enemyX = -1; int enemyY = 1; srand((unsigned) time(NULL)); while (1) { Sleep(50); // Move plane if (kbhit()) { char c = getch(); if (c == 'a' && planeX > 1) { drawPlane(planeX, HEIGHT - 3); planeX--; } if (c == 'd' && planeX < WIDTH - 7) { drawPlane(planeX, HEIGHT - 3); planeX++; } if (c == ' ') { if (bulletY == -1) { bulletX = planeX + 3; bulletY = HEIGHT - 4; drawBullet(bulletX, bulletY); } } } // Move bullet if (bulletY > 0) { drawBullet(bulletX, bulletY); bulletY--; if (bulletY == 0) { drawBullet(bulletX, bulletY); bulletY = -1; } } // Move enemy if (enemyX == -1 && enemyY == -1) { enemyX = rand() % (WIDTH - 4) + 2; enemyY = 1; drawEnemy(enemyX, enemyY); } else if (enemyY < HEIGHT) { drawEnemy(enemyX, enemyY); enemyY++; if (bulletX != -1 && bulletY == enemyY && bulletX >= enemyX && bulletX <= enemyX + 4) { drawBullet(bulletX, bulletY); bulletY = -1; enemyX = -1; enemyY = -1; score++; updateScore(); } if (enemyY == HEIGHT) { drawEnemy(enemyX, enemyY); enemyX = -1; enemyY = -1; } } } return 0; } ``` 这个程序使用了Windows控制台API来实现游戏界面和控制游戏元素的移动。通过键盘输入来控制飞机的移动和发射子弹,敌机会自动向下移动,当子弹击中敌机时,得分加一。

相关推荐

最新推荐

recommend-type

C语言实现简单飞机大战

主要为大家详细介绍了C语言实现简单飞机大战,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

使用C语言编写圣诞表白程序

圣诞节快到了,让我们用C语言制作一个圣诞表白程序吧,下面通过本文学习下实现代码
recommend-type

C语言程序设计+研究生复试+求职+面试题

汇总了计算机研究生复试有关C语言程序设计各章节简答题,使用了易于口头表达的语言进行了总结。包括C语言程序设计基本概念及各章节问题回答。可供研究生复试或相关专业岗位面试使用。如: 简述C语⾔采取了哪些措施提...
recommend-type

socket多人聊天程序C语言版(一)

主要为大家详细介绍了socket多人聊天程序C语言版,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C语言利用EasyX实现图形化界面的小游戏

C语言利用EasyX实现图形化界面的小游戏,可实操性很强,干货满满,学完C语言不知道干啥,可以写着玩一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。