zoukankan      html  css  js  c++  java
  • C语言 函数封装版飞机游戏

    include <stdio.h>

    include <stdlib.h>

    include <conio.h>

    include <windows.h>

    // 函数外全局变量定义
    int position_x,position_y; // 飞机位置
    int bullet_x,bullet_y; // 子弹的位置
    int high,width; // 画面的尺寸
    int enemy_x, enemy_y; // 敌机位置
    int score; // 游戏得分

    void gotoxy(short x, short y) { //控制台窗口光标定位
    COORD crd = {x, y}; //定义 COORD 类型(窗口坐标)的变量 crd 并以形参x和y初始化
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), crd);
    //GetStdHandle获取控制台输出句柄并用SetConsoleCursorPosition设置光标位置
    return;
    }

    void HideCursor() // 隐藏光标的函数

    {

    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    

    }

    void startup() // 数据初始化
    {
    high = 18;
    width = 30;
    position_x = high/2;
    position_y = width/2;
    bullet_y = position_y;
    bullet_x = -1;

    enemy_x = 0;
    enemy_y = width/2;
    
    score = 0;
    HideCursor();
    

    }
    void show() // 显示画面
    {
    int i,j;
    gotoxy(0, 0); //光标移动到文本窗口的左上角,以下重画清屏
    for (i=0;i<high;i++)
    {
    for (j=0;j<width; j++)
    {
    if ((iposition_x) && (jposition_y))
    printf("*"); // 输出飞机 *
    else if ((ibullet_x) && (jbullet_y))
    printf("|"); // 输出子弹 |
    else if ((ienemy_x) && (jenemy_y))
    printf("@"); // 输出敌机 @
    else
    printf(" "); // 表示输出空格

    	}
    	printf("
    ");
    }
    printf("得分: %d
    ",score);
    

    }

    void updateWithoutInput() // 与用户输入无关的更新
    {

    if ((bullet_x ==enemy_x) && (bullet_y == enemy_y))
    {
    	score++;
    	enemy_x = 0;
    	enemy_y = rand() % width;
    	bullet_x = -1;
    
    }
    
    
    if (bullet_x > -1)
    	 
    	bullet_x--;
    
    static int speed =0;    // 用于控制飞机下落速度
    if (speed<20)
    	speed++;
    if (bullet_x > -1)
    	bullet_x--;
    if (enemy_x >high)
    {
    	enemy_x = 0;
    	enemy_y = rand() % width;
    }
    else
    {
    	if (speed==20)
    	{
    	
    		enemy_x++;
    		speed = 0;
    	}
    
    }
    

    }

    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;
    	};
    }
    

    }
    int main()
    {
    startup(); // 数据初始化
    while(1) // 游戏循环执行

    {
    	show(); // 显示画面
    	updateWithoutInput();   //  与用户输入无关的更新
    	updateWithInput();   // 与用户输入有关的更新
    
    }
    return 0;
    

    }

    人生苦短,我用python
  • 相关阅读:
    maven 仓库配置 pom中repositories属性
    Spring Boot集成持久化Quartz定时任务管理和界面展示
    gradle使用总结
    sqlserver 分页
    MyBatis特殊字符转义
    Mybatis中#{}和${}传参的区别及#和$的区别小结
    Markdown 手册
    Spring boot——logback.xml 配置详解(四)<filter>
    Spring boot——logback.xml 配置详解(三)<appender>
    Spring boot——logback.xml 配置详解(二)
  • 原文地址:https://www.cnblogs.com/niucunguo/p/14617479.html
Copyright © 2011-2022 走看看