zoukankan      html  css  js  c++  java
  • 代码实现最简单的游戏————空中飞机

    以下代码简单的实现了:
    1,控制飞机的移动
    2,飞机发射子弹
    3,敌机的生成与移动
    4,随着分数的增大,地敌机的移动速度加快
    5,防止飞机越界
    6,实现游戏的暂停和结束

    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<windows.h>
    
    
    int position_x, position_y;//飞机位置
    int bullet_x, bullet_y;//子弹位置
    int enemy_x, enemy_y;//敌人位置
    int high, width;//游戏地图的尺寸
    int score;//得分
    int velocity = 35;//速度
    
    void HideCursor()//隐藏光标的函数
    {
    	CONSOLE_CURSOR_INFO cursor_info = {1, 0};//第二个值为零表示隐藏光标,如果好奇的伙伴把0变为1,就会发现光标在狂闪
    	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    }
    
    void startup()//初始化游戏数据
    {
    	high = 20;//初始化地图
    	width = 30;
    	position_x = high / 2;//初始化飞机的位置
    	position_y = width / 2;
    	bullet_x = 0;//子弹的位置
    	bullet_y = position_y;//因为子弹在飞机头的上方所以横坐标相同
    	enemy_x = 0;//敌机的位置
    	enemy_y = position_y;
    }
    
    void gotoxy(int x, int y)//将光标移动到(x,y)的位置,进行从新画图,相当与代替了system("cls");避免了画面狂闪。
    {
    	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    	COORD pos;
    	pos.X = x;
    	pos.Y = y;
    	SetConsoleCursorPosition(handle, pos);
    
    }
    
    void show()//显示画面
    {
    	gotoxy(0, 0);//光标移动到原点
    	int i, j;
    	for (i = 0; i < high; i++)
    	{
    		for (j = 0; j < width; j++)
    		{
    			if ((i == position_x) && (j == position_y))
    
    				printf("*");//输出飞机
    			else if ((i == position_x + 1) && (j == position_y - 2))
    				printf("*****");
    			else if ((i == position_x + 2) && (j == position_y - 2))
    				printf(" * * ");
    			else if ((i == enemy_x) && (j == enemy_y))
    				printf("@");//输出敌机
    			else if ((i == bullet_x) && (j == bullet_y))
    				printf("|");//输出子弹
    			else
    				printf(" ");//输出空格
    		}
    		printf("\n");
    	}
    	printf("得分:%d", score);
    
    }
    
    void updateWithoutInput() //与用户输入无关的数据
    {
    	if (bullet_x - 1)//子弹的移动
    		bullet_x--;
    	
    	if ((bullet_x == enemy_x) && (bullet_y == enemy_y))
    	{
    		score++;
    		velocity -= 1;//随着击落一架敌机分数增加,下以辆敌机速度加快;
    		enemy_x = -1;//从新初始化敌机坐标
    		enemy_y = rand() % width;//随机生成位置
    	}
    	if (enemy_x > high)
    	{
    		enemy_x = -1;//如果敌机已经出戒线则重新生成敌机
    		enemy_y = rand() % width;//随机生成坐标
    	}
    	
    	static int speed1 = 0;//避免更新数据影响用户的输入
    	if (speed1 < velocity)
    		speed1++;
    	if (speed1 == velocity)
    	{
    		enemy_x ++;//敌机的向下移动
    		speed1 = 0;
    		
    
    	}
    	
    	
    }
    
    void updateWithInput()//与用户输入有关的数据
    {
    	char input,j;
    	if (_kbhit())
    	{
    		input = _getch();//——getch()直接获取字符,不用按回车
    		switch (input)
    		{
    		case 'a':if (position_y - 2 == 0);else position_y--; break;//以下if()语句都加入了防止越界
    		case 'd':if (position_y + 2 == width); else position_y++; break;
    		case 'w':if (position_x == 0);else position_x--; break;
    		case 's':if (position_x + 3 == high);else  position_x++; break;
    		case ' ':bullet_x = position_x - 1; bullet_y = position_y; break;//发射的子弹初始位置在飞机的上空,
    		case 'p':j = _getch(); if (j == 27)exit(0); //当输入‘p’暂停游戏,输入除ESC的按键继续游戏,如果为ESC结束程序,ESC的对应的ascill是27,exit()为结束程序的函数
    		}
    	}
    }
    
    int main()
    {
    	HideCursor();//隐藏光标
    	startup();//初始化游戏数据
    	while (1)
    	{
    		show(); //显示画面
    		updateWithoutInput();//与用户无关的数据更新
    		updateWithInput();   //与用户输入有关的数据更新
    	}
    	return 0;
    }
    
    追求吾之所爱
  • 相关阅读:
    WCF步步为营(三):使用配置文件改变使用服务的方式
    WCF步步为营(五):数据契约
    弹性工作制下的IT项目管理
    C#拾遗系列(8):异常
    WCF步步为营(一):简单示例
    敏捷的 "道"
    从中国男足看项目管理
    WCF步步为营(二):使用配置文件改变发布服务的方式
    WCF步步为营(四):客户端使用代理类使用服务
    C#拾遗系列(9):继承、接口、扩展方法、分部类、类操作、Ref and Out、可空类型
  • 原文地址:https://www.cnblogs.com/rstz/p/12393230.html
Copyright © 2011-2022 走看看