EasyX之跳跳球
- 实现绘制小球和矩形到固定位置
- 让矩形向左移动
- 使小球实现起跳与下落
- 解决小球二次起跳问题
- 判断小球起跳的过程中是否碰到矩形的左边,上边,以及右边,若碰到,游戏结束,分数清零
- 分数的打印
#define _CRT_SECURE_NO_WARNINGS
#include<graphics.h>
#include<cstdio>
#include<conio.h>
#include<time.h>
#define WIDTH 800
#define HEIGHT 400
void printScore(int score)
{
char buffer[20];
sprintf(buffer, "分数: %d ", score);
settextcolor(RED);
settextstyle(40, 0, "宋体");
outtextxy(50, 50, buffer);
}
int main()
{
srand((unsigned int)time(NULL));
initgraph(WIDTH, HEIGHT);
setbkcolor(WHITE);
cleardevice();
setfillcolor(RED);
int radius = 10;
int ball_x = WIDTH / 4;
int ball_y = HEIGHT - radius;
int ball_vy = 0;
int ball_a = 14;
int rec_width = 20;
int rec_height = 100;
int rec_left_topx = 3*WIDTH/4;
int rec_left_topy = HEIGHT- rec_height;
int rec_vx = -20;
int score = 0;
bool flag = false;
char userKey = '\0';
while (1)
{
BeginBatchDraw();
if ((rec_left_topx + rec_width) <= 0)
{
score+=10;
rec_width = rand() % 21 + 10;
rec_height = rand() % 81 + 80;
rec_left_topx = WIDTH;
rec_left_topy = HEIGHT - rec_height;
rec_vx = rand() % 5 + (-40);
setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
}
if (_kbhit())
{
char userKey = _getch();
if (userKey == ' ' && flag == true)
{
flag = false;
ball_vy = -80;
}
}
ball_vy = ball_vy + ball_a;
ball_y = ball_y + ball_vy;
if (ball_y >= HEIGHT - radius)
{
flag = true;
ball_vy = 0;
ball_y = HEIGHT - radius;
}
rec_left_topx = rec_left_topx + rec_vx;
if ((ball_x + radius >= rec_left_topx) &&
(ball_y + radius >= HEIGHT - rec_height) &&
(ball_x - radius <= rec_left_topx + rec_width))
{
score = 0;
Sleep(100);
MessageBox(GetHWnd(), "You die,Now B!", "游戏结束", MB_OK);
}
cleardevice();
fillcircle(ball_x, ball_y, radius);
fillrectangle(rec_left_topx, rec_left_topy,
rec_left_topx + rec_width, rec_left_topy + rec_height);
printScore(score);
Sleep(100);
EndBatchDraw();
}
closegraph();
system("pause");
return 0;
}
