1 Star 0 Fork 0

软件二班黄桢淇 / Lucky OJ

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
消除砖块.cpp 2.73 KB
Copy Edit Raw Blame History
软件二班黄桢淇 authored 2019-05-31 14:36 . 消除砖块代码
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int ball_x,ball_y;//定义小球的x,y坐标
int v_x,v_y;//定义小球的x,y的运动速度
int high=20;
int wideth=36;//定义画面大小
int banzi_x,banzi_y;
int banjing;
int left,right;//定义下面板子的各种参数
int score1=0;
int score2=0;//定义两个得分
int zhuankuai_x1,zhuankuai_y1;//定义砖块的位置
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(hOut, pos);
}
void initialization()//初始化各数据
{
ball_x = 0;
ball_y = wideth/2;//初始化小球的位置
v_x = 1;
v_y = 1;//初始化速度方向默认向右下角
zhuankuai_x1 = 0;
zhuankuai_y1 = 5;//初始化砖块位置
banzi_y = 13;
banzi_x = high-2;
banjing = 3;
left = banzi_y - banjing;
right = banzi_y + banjing;//板子位置
}
void show()//显示的位置;
{
int i,j;
gotoxy(0,0);//清屏函数
for(i=0;i<=high;i++)
{
for(j=0;j<=wideth;j++)
{
if((i == ball_x) && (j == ball_y))
printf("o");//输出小球
else if((i == zhuankuai_x1) && (j == zhuankuai_y1))
printf("B");//输出砖块
else if(j == wideth)
printf("|");//输出右边界
else if((i == banzi_x) && (j <= right) && (j >= left))
printf("*");//输出板子
else if(i == high)
printf("_");//输出下边界
else
printf(" ");
}
printf("\n");
}
printf("你的得分:%d\n",score1);
printf("你的弹数是:%d",score2/15);//输出两个得分
}
void updatenowithkeyboard()//键盘输入无关的参量
{
if((ball_x == banzi_x-1) && (ball_y <= right) && (left <= ball_y))
{
v_x = (-1) * v_x;
v_y = (-1) * v_y;
score2++;
}//小球弹到板子上时反弹,并且弹数得分增加
static int speed=0;
if(speed < 15)
speed++;
if(speed == 15)
{
ball_x = ball_x + v_x;
ball_y = ball_y + v_y;
speed=0;
}//定义一个静态变量,使小球的速度变慢,板子速度不受影响,但是不知道为什么数字只能设置为15,其他的小球都显示不了
if(ball_x == 0)
v_x = (-1) * v_x;
if((ball_y == 0) || (ball_y == wideth))
v_y = (-1) * v_y;//小球碰到上边界,左右边界时反弹
if((ball_x == zhuankuai_x1) && (ball_y == zhuankuai_y1))
{
zhuankuai_x1 = rand() % wideth;
score1++;
}//小球碰到砖块时,砖块随机任意位置生成一个,得分+1
if(ball_x > banzi_x)
{
printf("\nGAME OVER\n");
exit(0);//当小球落到板子下面时,游戏结束
}
}
void updatewithkeyboard()//键盘输入有关的参量
{
char input;
if(kbhit())
{
input = getch();
if(input == 'a')
banzi_y--;
if(input == 'd')
banzi_y++;
left = banzi_y - banjing;
right = banzi_y + banjing;
}//用a和d来控制板子的左右移动
}
int main()
{
HideCursor();
initialization();
while(1)
{
show();
updatewithkeyboard();
updatenowithkeyboard();
}
return 0;
}
C
1
https://gitee.com/wonzenkei/Lucky-OJ.git
git@gitee.com:wonzenkei/Lucky-OJ.git
wonzenkei
Lucky-OJ
Lucky OJ
master

Search