IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++实现打飞机与弹球游戏 -> 正文阅读

[C++知识库]C++实现打飞机与弹球游戏

话不多说,直接上代码:

打飞机:

#include<bits/stdc++.h>
#include<Windows.h>
#define height 21
#define width 42
using namespace std;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
void pos(int x,int y){
	COORD pos={x,y};
	SetConsoleCursorPosition(hOut,pos);
}
void color(int c) {
    SetConsoleTextAttribute(hOut, c);
    return;
}
void initmap(){
	color(15);
	pos(1,height+1);
	cout<<"_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o";
	for(int i=1;i<=height;i++){
		pos(width+2,i);
		cout<<"o";
		cout<<"|";
	}
	for(int i=1;i<=height;i++){
		pos(2,i);
		cout<<"o";
		cout<<"|";
	}
	pos(1,1);
	cout<<"_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o_o";
	return;
}
int main()
{
	
	CONSOLE_CURSOR_INFO CursorInfo;
    CursorInfo.bVisible = 0;
    SetConsoleCursorInfo(hOut, &CursorInfo);
	int x=rand()%20+2,y=rand()%20+2,xvelocity=1,yvelocity=1;
	while(1){
		
		color(rand()%14+1);
		y=y+yvelocity;
		x=x+xvelocity;
		system("cls");
		pos(x,y);
		cout<<"o";
		initmap();
		if(y==height||y==1){
			yvelocity= -yvelocity;
			Beep(700,40);
		} 
		if(x==width||x==1){
			xvelocity= -xvelocity;
			Beep(700,40);
		} 
		Sleep(30);
	}
	return 0;
}

打飞机:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream>
#include<windows.h>
using namespace std;
int position_x,position_y; 
int high,width;            
int bullet_x,bullet_y; 
int enemy_x,enemy_y;
int score;
void startup() 
{
	high = 20;
    width = 30;
    position_x = high/2;
    position_y = width/2;
    bullet_x=-2;
    bullet_y=position_y;
    enemy_x=0;
    enemy_y=position_y;
    score=0;
}
void show()  
{
    system("cls");int i,j;
    for (i=0;i<high;i++)
    {
    	for (j=0;j<width;j++)
    	{
    		if ((i==position_x) && (j==position_y))  
    		{
     		cout<<"   ^ \n";
     		for(int k=1;k<=position_y;k++) 
			{
			 	cout<<" ";
			}
      		cout<<"  (_) \n";
      		for(int k=1;k<=position_y;k++) 
			{
			  	cout<<" ";
			}
      		cout<<" (___) \n";
      		for(int k=1;k<=position_y;k++)
			{
				cout<<" ";
			}
      		cout<<"(_____)\n";
    		}
    		else if(i==bullet_x&&j==bullet_y) 
			{
				cout<<" ^";
			}
    		else if(i==enemy_x&&j==enemy_y) 
			{
				cout<<"-O-";
			}
    		else 
			{
			cout<<" "; 
			}        
    	}
    cout<<"\n";
    }
}  
void updateWithInput()  
{
  char input;
  if(kbhit())  
  {
    input = getch(); 
    if (input == 'a')   
	{
		position_y--;
	} 
    if (input == 'd')  
	{
		position_y++;  
	} 
    if (input == 'w')  
	{
		position_x--;  
	} 
    if (input == 's')   
	{
		position_x++; 
	} 
    if (input== ' ' )
	{
      bullet_x=position_x-1;
      bullet_y=position_y+2;
    }
  }
}
void updateWithoutInput() 
{
    if(bullet_x >=1)
    {
		bullet_x--;
    }
    if((bullet_x==enemy_x)&&(bullet_y==enemy_y))
    {
	  	score++;
   		enemy_x=-1; enemy_y=rand()%width; 
  		bullet_x=-2; 
	}
	if(enemy_x>high)
	{
	  	enemy_x=-1; 
		enemy_y=rand()%width; 
	}
  	static int speed=0;   
  	if(speed<20) 
    {
	  	speed++;
	}
	if(speed==20)
	{
  		enemy_x++;
  		speed=0;
  	}  
}
void HideCursor()
{
   	CONSOLE_CURSOR_INFO cursor_info = {1,0}; 
  	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); 
}
int main()
{
	HideCursor();
    startup();
    while(1)  
    {
        show();  
        updateWithoutInput();  
        updateWithInput();     
    }
    return 0;
} 

OK

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-16 11:33:49  更:2021-08-16 11:36:35 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 1:52:38-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码