zoukankan      html  css  js  c++  java
  • 运用数组实现反弹消砖块

    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<Windows.h>
    
    #define High 15
    #define Width 20
    
    int ball_x, ball_y;
    int ball_vx, ball_vy;
    int position_x, position_y;
    int ridus;
    int left, right;
    int canvas[High][Width] = { 0 };
    
    void gotoxy(int x,int y)
    {
    	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    	COORD pos;
    	pos.X = x;
    	pos.Y = y;
    	SetConsoleCursorPosition(handle, pos);
    
    }
    void HideCursor()
    {
    	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
    	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    
    }
    
    void startup()
    {
    	ball_x = High-3;
    	ball_y = Width / 2;
    	ball_vx = 1;
    	ball_vy = 1;
    	canvas[ball_x][ball_y] = 1;
    	
    	ridus = 5;
    	position_x = High-1;
    	position_y = Width/2;
    	left = position_y - ridus;
    	right = position_x + ridus;
    
    	int k;
    	for (k = left; k <= right; k++)
    		canvas[position_x][k] = 2;
    	int i;
    	for (i = 0; i < Width; i++)
    		for (k = 0; k < High / 4; k++)
    			canvas[k][i] = 3;
    
    }
    
    void show()
    {
    	gotoxy(0, 0);
    	int i, j;
    	for (i = 0; i < High;i++)
    	{
    		for (j = 0; j < Width; j++)
    		{
    			if (canvas[i][j] == 0)
    				printf(" ");
    			else if (canvas[i][j] == 1)
    				printf("o");
    			else if (canvas[i][j] == 2)
    				printf("*");
    			else if (canvas[i][j] == 3)
    				printf("#");
    		}
    		printf("|\n");
    	}
    	for (j = 0; j < Width; j++)
    		printf("_");
    	printf("\n");
    }
    
    void updateWithoutInput()
    {
    	if (ball_x == High - 2)
    	{
    		if ((ball_y >=left) && (ball_y <= right))
    		{
    			printf("\a");
    		}
    		else
    		{
    			printf("GAMEOVER!");
    			system("pause");
    			exit(0);
    		}
    		
    	}
    	static int speed = 0;
    	if (speed < 7)
    		speed++;
    	if (speed == 7)
    	{
    		speed = 0;
    		canvas[ball_x][ball_y] = 0;
    		ball_x = ball_x + ball_vx;
    		ball_y = ball_y + ball_vy;
    		canvas[ball_x][ball_y] = 1;
    		if ((ball_x == 0) || (ball_x == High - 2))
    			ball_vx = -ball_vx;
    		if ((ball_y == 0) || (ball_y == Width - 1))
    			ball_vy = -ball_vy;
    		if (canvas[ball_x-1][ball_y] == 3)
    		{
    			ball_vx = -ball_vx;
    			canvas[ball_x-1][ball_y] = 0;
    			printf("\a");
    		}
    	}
    	
    }
    
    void updateWithIput()
    {
    	char input;
    	if (_kbhit())
    	{
    		input = _getch();
    		if (input == 'a' && left > 0)
    		{
    			canvas[position_x][right] = 0;
    			position_y--;
    			left = position_y - ridus;
    			right = position_y + ridus;
    			canvas[position_x][left] = 2;
    		}
    		if (input == 'd' && right < Width - 1)
    		{
    			canvas[position_x][left] = 0;
    			position_y++;
    			left = position_y - ridus;
    			right = position_y + ridus;
    			canvas[position_x][right] = 2;
    		}
    		if (input == ' ')
    		{
    			canvas[ball_x][ball_y] = 0;
    			ball_x = position_x-1;
    			ball_y = position_y;
    			canvas[ball_x][ball_y] = 1;
    			
    		}
    	}
    }
    
    int main()
    {
    	HideCursor();
    	startup();
    	while (1)
    	{
    		show();
    		updateWithoutInput();
    		updateWithIput();
    	}
    	return 0;
    }
    
    
    追求吾之所爱
  • 相关阅读:
    面试官问我注解怎么使用?我这样告诉他
    dump 叶子节点
    dump 分支块
    设置 NSZombieEnabled 定位 EXC_BAD_ACCESS 错误
    EBS业务学习之应收管理
    EBS业务学习之应付管理
    EBS多组织结构
    总帐模块表结构
    iOS界面不能点击(tableView 的cell 不能使用点击事件,tableView也不能上下滚动)
    EBS业务学习之库存管理
  • 原文地址:https://www.cnblogs.com/rstz/p/12393222.html
Copyright © 2011-2022 走看看