求大佬解决
  • 板块灌水区
  • 楼主see_htdls
  • 当前回复8
  • 已保存回复8
  • 发布时间2024/7/15 14:22
  • 上次更新2024/7/15 15:07:09
查看原帖
求大佬解决
716862
see_htdls楼主2024/7/15 14:22

代码:

#include<graphics.h> 
#include<conio.h>
#include<iostream>
#include<bitset>
#include<windows.h>
#include<stdio.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
int max;
int main() {
	double width, height;//定义画面长度、宽度
	width = 600;
	height = 400;
	initgraph(width, height);

	double ball_x, ball_y, ball_vy, r, g;//定义小球x轴、y轴、y轴方向的速度、半径、重力加速度
	g = 0.6;
	r = 20;
	ball_x = width / 4;//x坐标等于整个画面x轴长度的四分之一
	ball_y = height - r;//y坐标等于画面的y轴长度减去圆的半径(保证圆在画面的最底端)
	ball_vy = 0; //最初小球落在地面上时y轴方向的速度为0

	double rect_left_x, rect_top_y, rect_width, rect_height;//定义方块的各个位置变量
	rect_width = 20;
	rect_height = 100;
	rect_left_x = width * 3 / 4;
	rect_top_y = height - rect_height;

	double rect_vx = -3;//定义方块的移动速度

	int score = 0;//定义得分
	while (1) {
//		if (_kbhit()) {
//			char input = _getch();
			if (KEY_DOWN(' ') && ball_y == 380) {
				ball_vy = -16;
			}
			else if(KEY_DOWN('Q')) {
				while (1) {
						Sleep(10);
						if (KEY_DOWN('E')) {
							Sleep(200);
							break;
						}
				}
			}
//		}
		ball_vy = ball_vy + g;//根据牛顿力学定律得
		ball_y = ball_y + ball_vy;//小球y轴方向的变化
		if (ball_y >= height - r) {
			ball_vy = 0;//小球落地以后速度清零
			ball_y = height - r;
		}
		rect_left_x = rect_left_x + rect_vx;
		if (rect_left_x <= 0) {//如果方块移动到最左边
			score++;
			rect_left_x = width;//方块消失以后重新出现
			rect_height = rand() % int(height / 4) + height / 4;//设置随机高度
			rect_vx = rand() / float(RAND_MAX) * 4 - 7;//设置方块随机速度
		}
		if ((rect_left_x <= ball_x + r) && (rect_left_x + rect_width >= ball_x - r) && (height - rect_height <= ball_y + r)) {
			score = 0;
			Sleep(100);//碰到方块慢动作
		}
		cleardevice();
		fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height);
		fillcircle(ball_x, ball_y, r);

		TCHAR s[20];//定义字符串数组
		_swprintf(s, _T("%d"), score);//把score转换为字符串并储存在字符串数组s中
		settextstyle(40, 0, _T("宋体"));//设置文本大小
		outtextxy(50, 30, s);//输出得分
		Sleep(10);
	}
	_getch();
	closegraph();
	return 0;
}

我做的一个小球跳越的小游戏,但在运行时发现会有较为严重的频闪,求大佬帮忙

2024/7/15 14:22
加载中...