当前位置 博文首页 > zzr17147的博客:C++实现打飞机与弹球游戏

    zzr17147的博客:C++实现打飞机与弹球游戏

    作者:[db:作者] 时间:2021-08-18 13:16

    话不多说,直接上代码:

    打飞机:

    #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

    cs