- C++
躲避方块游戏
- @ 2025-12-28 20:38:07
#include<conio.h>
#include<stdio.h>
#include<easyx.h>
using namespace std;
int main(){
float width,height,gravity;
float ball_x,ball_y,ball_vy,radius;
float rect_left_x,rect_top_y,rect_width,rect_height,rect_vx;
int score=0;
width = 600;
height = 400;
gravity = 0.6;
initgraph(width,height);
radius = 20;
ball_x = width/4;
ball_y = height-radius;
ball_vy=0;
rect_height = 100;
rect_width = 20;
rect_left_x = width*3/4;
rect_top_y = height - rect_height;
rect_vx = -4;
int isballonfloor = 1;
while(1)
{
if(kbhit())
{
char input=_getch();
if(input==' '&&isballonfloor)
{
ball_vy=-17;
isballonfloor=0;
}
}
ball_vy += gravity;
ball_y += ball_vy;
if(ball_y >= height-radius){
ball_vy = 0;
ball_y = height-radius;
isballonfloor=1;
}
rect_left_x += rect_vx;
if(rect_left_x <= 0)
{
rect_left_x = width;
score++;
rect_height = rand()%int(height/4) + height/4;
rect_vx = rand()/float(RAND_MAX)*4 - 7;
}
if((rect_left_x <= ball_x + radius)&&(rect_left_x + rect_width >= ball_x - radius)&&(height - rect_height <= ball_y + radius))
{
Sleep(100);
score = 0;
rect_left_x = width;
rect_height = rand()%int(height/4) + height/4;
rect_vx = rand()/float(RAND_MAX)*4 - 7;
}
cleardevice();
fillcircle(ball_x, ball_y, radius);
fillrectangle(rect_left_x, height-rect_height, rect_left_x + rect_width,height);
TCHAR s[20];
_stprintf(s, _T("%d"), score);
settextstyle(40, 0, _T("宋体"));
outtextxy(50, 30, s);
Sleep(10);
}
closegraph();
return 0;
} 不要创建新的Source File 要使用Project 顺序: 1:左键点击Files 2:右键点击New 3:右键点击Project 4:右键点击Empty Project 5:右键点击OK 需要EasyX库: https://docs.easyx.cn/zh-cn/intro https://easyx.cn/
5 comments
-
mumumu LV 5 @ 2026-1-16 20:31:13
蛤蛤蛤
-
@ 2026-1-16 20:31:13
蛤蛤蛤
-
@ 2026-1-8 21:16:25
你好
-
@ 2026-1-7 8:45:07
卞自宏到此一游
-
@ 2026-1-2 19:47:11你好呀胡秉睿
- 1