zoukankan      html  css  js  c++  java
  • C 坦克射击小游戏

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <conio.h>
      4 #include <windows.h>
      5 #include <stdlib.h>
      6 #define MAX 100
      7 
      8 long long int speed=0;//控制敌机速度
      9 int position_x,position_y;//飞机的位置
     10 int high,width;//地图的大小
     11 int bullet_x,bullet_y;//子弹的位置
     12 int enemy_x,enemy_y;//敌人的位置
     13 int map[MAX][MAX];
     14 /*0表示空白,1表示战机的区域,2 表示敌人战机的位置。3表示上下围墙,4表示左右围墙,5表示子弹位置*/ 
     15 int score;
     16 void startup()//初始化所有信息
     17 {
     18     high =20;
     19     width =30;
     20     position_x=high/2;
     21     position_y=width/2;
     22     bullet_x=0;
     23     bullet_y=position_y;
     24     enemy_x=2;
     25     enemy_y=position_y-1;
     26     score=0;
     27 } 
     28 void startMap()
     29 {
     30     int i,j;
     31     for(i=1;i<=high-1;i++){
     32         map[i][1]=4;
     33         for(j=2;j<=width-1;j++){
     34             map[i][j]=0;
     35         }
     36             map[i][width]=4;
     37         }
     38         //下方围墙的初始化
     39         i=high;
     40         for (j=1;j<=width;j++){
     41             map[i][j]=3;
     42         }
     43             map[bullet_x][bullet_y]=5;
     44             /*这里是战机大小初始化开始*/
     45             map[position_x-1][position_y]=1;
     46             i=position_x;
     47             for(j=position_y-2;j<=position_y+2;j++){
     48                 map[i][j]=1;
     49             }
     50                 map[position_x+1][position_y-1]=1;
     51                 map[position_x+1][position_y+1]=1;
     52                 /*初始化结束*/
     53 
     54             /*敌人战机初始化*/
     55             map[enemy_x][enemy_y]=2;
     56             map[enemy_x-1][enemy_y-1]=2;
     57             map[enemy_x-1][enemy_y+1]=2;
     58             /*敌人战机初始化结束*/                    
     59             } 
     60             void HideCursor()//隐藏光标
     61             {
     62             CONSOLE_CURSOR_INFO cursor_info={1,0};
     63             SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
     64                  
     65 }
     66 void gotoxy(int x,int y){//清理一部分屏幕
     67             HANDLE handle =GetStdHandle(STD_OUTPUT_HANDLE);
     68             COORD pos;
     69             pos.X=x;
     70             pos.Y=y;
     71             SetConsoleCursorPosition(handle,pos);
     72         } 
     73 void updateWithoutInput()//与输入无关的更新
     74         {
     75         if(bullet_x>0)
     76             bullet_x--;
     77         if((bullet_x==enemy_x)&&(bullet_x==enemy_y))
     78         {
     79             //当敌人的飞机被击中时{
     80             score++;
     81             enemy_x=0;
     82             enemy_y=rand()%width;
     83             bullet_x=0;            
     84         
     85         }
     86          
     87         if(enemy_x>high){//当飞机超出区域 
     88             enemy_x=0;
     89             enemy_y=rand()%width; 
     90         }
     91         if(speed==1)
     92         {
     93             int i,j;
     94             for(i=1;i<=10000;i++){//用来控制敌机的速度
     95                 for(j=1;j<=1000;j++){
     96                     speed=1;
     97                     
     98                 } 
     99             }
    100         }
    101             speed=0;
    102             if(speed==0){
    103                 enemy_x++;
    104                 speed=1;
    105                 
    106             }
    107         }
    108     
    109 void updateWithInput()//与输入有关的更新
    110         {
    111         char input;
    112         if(_kbhit()){//vc6.0为 _kbhit()
    113             input=_getch();//vc6.0为_getch()
    114             if(input=='a'){
    115                 position_y--;
    116             
    117             }
    118             if(input=='s'){
    119                 position_x++;
    120             
    121             }
    122             if(input=='d'){
    123                 position_y++;
    124             
    125             }
    126             if(input=='w'){
    127                 position_x--;
    128             
    129             }  
    130             if(input==' '){
    131                 bullet_x=position_x-1;
    132                 bullet_y=position_y;
    133                 
    134             }
    135             
    136         } 
    137     }
    138 void show()//展示的内容
    139     {
    140         gotoxy(0,0);
    141         int i,j;
    142         for(i=1;i<=high;i++){
    143             for(j=1;j<=width;j++){
    144                 if(map[i][j]==0){
    145                     printf(" ");
    146                     
    147                 }
    148                 if(map[i][j]==1){
    149                     printf("*");
    150                     
    151                 }
    152                 if(map[i][j]==2){
    153                     printf("#");
    154                     
    155                 }
    156                 if(map[i][j]==3){
    157                     printf("~");
    158                     
    159                 }
    160                 if(map[i][j]==4){
    161                     printf("|");
    162                     
    163                 }
    164                 if(map[i][j]==5){
    165                     printf("|");
    166                     
    167                 }
    168             }
    169                 printf("
    ");
    170                 
    171             }
    172             printf("
    你的得分:%d
    
    ",score);
    173             printf("操作说明:asdw分别操作 左下右上");
    174             printf("
    **空格是发出子弹**
    ");
    175         } 
    176     
    177 int main()
    178      {
    179          startup();
    180          while(1)
    181          {
    182              HideCursor();
    183              startMap();
    184              show();
    185              updateWithoutInput();
    186              updateWithInput();
    187          }
    188          return 0;
    189      }

    运行结果:

  • 相关阅读:
    angularjs 学习理解
    setTimeout和setInterval
    c# 数组
    c# 枚举enum
    T_SQL 字符串函数
    T_SQL 日期函数
    T-SQL函数类型——系统函数
    SqlServer 删除日志
    ASP.NET中JSON的序列化和反序列化
    jquery 选择器
  • 原文地址:https://www.cnblogs.com/Catherinezhilin/p/10273595.html
Copyright © 2011-2022 走看看