
基于 EasyX
| |
| |
| #include <graphics.h> |
| #include <conio.h> |
| #include <stdio.h> |
| |
| |
| void demo_3_1_3() |
| { |
| while (1) |
| { |
| if (kbhit()) |
| { |
| char input = getchar(); |
| if (input == ' ') |
| { |
| printf("按下了空格!\n"); |
| } |
| } |
| } |
| } |
| |
| |
| void demo_3_2() |
| { |
| float width = 600; |
| float height = 400; |
| initgraph(width, height); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| while (1) |
| { |
| if (kbhit()) |
| { |
| |
| char input = _getch(); |
| if (input == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (input == 'q') |
| { |
| break; |
| } |
| } |
| |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| cleardevice(); |
| fillcircle(ball_x, ball_y, radius); |
| Sleep(10); |
| } |
| |
| closegraph(); |
| } |
| |
| |
| void demo_3_3_1() |
| { |
| float width = 600; |
| float height = 400; |
| initgraph(width, height); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| float rect_height = 100; |
| float rect_width = 20; |
| float rect_left_x = width * 3 / 4; |
| float rect_top_y = height - rect_height; |
| |
| while (1) |
| { |
| if (kbhit()) |
| { |
| |
| char input = _getch(); |
| if (input == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (input == 'q') |
| { |
| break; |
| } |
| } |
| |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| cleardevice(); |
| fillcircle(ball_x, ball_y, radius); |
| fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height); |
| Sleep(10); |
| } |
| |
| closegraph(); |
| } |
| |
| |
| void demo_3_3_2() |
| { |
| float width = 600; |
| float height = 400; |
| initgraph(width, height); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| float rect_height = 100; |
| float rect_width = 20; |
| float rect_left_x = width * 3 / 4; |
| float rect_top_y = height - rect_height; |
| float rect_vx = -3; |
| |
| while (1) |
| { |
| if (kbhit()) |
| { |
| |
| char input = _getch(); |
| if (input == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (input == 'q') |
| { |
| break; |
| } |
| } |
| |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| rect_left_x = rect_left_x + rect_vx; |
| if (rect_left_x <= 0) |
| { |
| rect_left_x = width; |
| } |
| |
| cleardevice(); |
| fillcircle(ball_x, ball_y, radius); |
| fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height); |
| Sleep(10); |
| } |
| |
| closegraph(); |
| } |
| |
| |
| void demo_3_4_2() |
| { |
| float width = 600; |
| float height = 400; |
| initgraph(width, height); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| float rect_height = 100; |
| float rect_width = 20; |
| float rect_left_x = width * 3 / 4; |
| float rect_top_y = height - rect_height; |
| float rect_vx = -3; |
| |
| while (1) |
| { |
| if (kbhit()) |
| { |
| |
| char input = _getch(); |
| if (input == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (input == 'q') |
| { |
| break; |
| } |
| } |
| |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| rect_left_x = rect_left_x + rect_vx; |
| if (rect_left_x <= 0) |
| { |
| rect_left_x = width; |
| } |
| |
| if ( (rect_left_x <= ball_x + radius) |
| && (rect_left_x + rect_width >= ball_x - radius) |
| && (height - rect_height <= ball_y + radius)) |
| { |
| Sleep(100); |
| } |
| |
| cleardevice(); |
| fillcircle(ball_x, ball_y, radius); |
| fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height); |
| Sleep(10); |
| } |
| |
| closegraph(); |
| } |
| |
| |
| void demo_3_5_4() |
| { |
| float width = 600; |
| float height = 400; |
| initgraph(width, height); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| float rect_height = 100; |
| float rect_width = 20; |
| float rect_left_x = width * 3 / 4; |
| float rect_top_y = height - rect_height; |
| float rect_vx = -3; |
| |
| while (1) |
| { |
| if (kbhit()) |
| { |
| |
| char input = _getch(); |
| if (input == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (input == 'q') |
| { |
| break; |
| } |
| } |
| |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| rect_left_x = rect_left_x + rect_vx; |
| if (rect_left_x <= 0) |
| { |
| 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 + radius) |
| && (rect_left_x + rect_width >= ball_x - radius) |
| && (height - rect_height <= ball_y + radius)) |
| { |
| Sleep(100); |
| } |
| |
| cleardevice(); |
| fillcircle(ball_x, ball_y, radius); |
| fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height); |
| Sleep(10); |
| } |
| |
| closegraph(); |
| } |
| |
| |
| |
| void demo_3_6() |
| { |
| float width = 600; |
| float height = 400; |
| initgraph(width, height); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| float rect_height = 100; |
| float rect_width = 20; |
| float rect_left_x = width * 3 / 4; |
| float rect_top_y = height - rect_height; |
| float rect_vx = -3; |
| |
| int score = 0; |
| bool first_time_being_left = false; |
| bool collision = false; |
| |
| while (1) |
| { |
| if (kbhit()) |
| { |
| |
| char input = _getch(); |
| if (input == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (input == 'q') |
| { |
| break; |
| } |
| } |
| |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| rect_left_x = rect_left_x + rect_vx; |
| |
| if (rect_left_x <= 0) |
| { |
| rect_left_x = width; |
| first_time_being_left = false; |
| collision = false; |
| |
| 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; |
| collision = true; |
| } |
| |
| |
| if (rect_left_x + rect_width < ball_x - radius && first_time_being_left == false && collision == false) |
| { |
| first_time_being_left = true; |
| score++; |
| } |
| |
| cleardevice(); |
| fillcircle(ball_x, ball_y, radius); |
| fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width, height); |
| |
| char s[20] = { 0 }; |
| sprintf(s, "score: %d", score); |
| |
| const int fontsize = 40; |
| settextstyle(fontsize, 0, _T("Consolas")); |
| |
| settextcolor(LIGHTGRAY); |
| outtextxy(50, 30, s); |
| |
| Sleep(10); |
| } |
| |
| closegraph(); |
| } |
| |
| int main() |
| { |
| |
| |
| |
| |
| |
| |
| demo_3_6(); |
| |
| return 0; |
| } |
基于 Raylib
| |
| |
| #include "raylib.h" |
| #include <stdio.h> |
| #include <stdlib.h> |
| |
| |
| void demo_3_1_3() |
| { |
| InitWindow(600, 600, "don't touch the square"); |
| SetTargetFPS(60); |
| |
| while (!WindowShouldClose()) |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (GetKeyPressed() == ' ') |
| { |
| printf("按下了空格!\n"); |
| } |
| |
| |
| BeginDrawing(); |
| { |
| ClearBackground(BLACK); |
| } |
| EndDrawing(); |
| } |
| |
| CloseWindow(); |
| } |
| |
| |
| void demo_3_2() |
| { |
| float width = 600; |
| float height = 400; |
| InitWindow(width, height, "don't touch the square"); |
| SetTargetFPS(60); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| while (!WindowShouldClose()) |
| { |
| |
| if (GetKeyPressed() == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (GetKeyPressed() == 'q') |
| { |
| break; |
| } |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| |
| BeginDrawing(); |
| { |
| ClearBackground(BLACK); |
| DrawCircle(ball_x, ball_y, 20, WHITE); |
| } |
| EndDrawing(); |
| } |
| |
| CloseWindow(); |
| } |
| |
| |
| |
| void demo_3_3_1() |
| { |
| float width = 600; |
| float height = 400; |
| InitWindow(width, height, "don't touch the square"); |
| SetTargetFPS(60); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| float rect_height = 100; |
| float rect_width = 20; |
| float rect_left_x = width * 3 / 4; |
| float rect_top_y = height - rect_height; |
| |
| while (!WindowShouldClose()) |
| { |
| |
| if (GetKeyPressed() == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (GetKeyPressed() == 'q') |
| { |
| break; |
| } |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| |
| BeginDrawing(); |
| { |
| ClearBackground(BLACK); |
| DrawCircle(ball_x, ball_y, 20, WHITE); |
| DrawRectangle(rect_left_x, height - rect_height, rect_width, rect_height, WHITE); |
| } |
| EndDrawing(); |
| } |
| |
| CloseWindow(); |
| } |
| |
| |
| void demo_3_3_2() |
| { |
| float width = 600; |
| float height = 400; |
| InitWindow(width, height, "don't touch the square"); |
| SetTargetFPS(60); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| float rect_height = 100; |
| float rect_width = 20; |
| float rect_left_x = width * 3 / 4; |
| float rect_top_y = height - rect_height; |
| float rect_vx = -3; |
| |
| while (!WindowShouldClose()) |
| { |
| |
| if (GetKeyPressed() == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (GetKeyPressed() == 'q') |
| { |
| break; |
| } |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| rect_left_x = rect_left_x + rect_vx; |
| if (rect_left_x <= 0) |
| { |
| rect_left_x = width; |
| } |
| |
| |
| BeginDrawing(); |
| { |
| ClearBackground(BLACK); |
| DrawCircle(ball_x, ball_y, 20, WHITE); |
| DrawRectangle(rect_left_x, height - rect_height, rect_width, rect_height, WHITE); |
| } |
| EndDrawing(); |
| } |
| |
| CloseWindow(); |
| } |
| |
| |
| void demo_3_4_2() |
| { |
| float width = 600; |
| float height = 400; |
| InitWindow(width, height, "don't touch the square"); |
| SetTargetFPS(60); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| float rect_height = 100; |
| float rect_width = 20; |
| float rect_left_x = width * 3 / 4; |
| float rect_top_y = height - rect_height; |
| float rect_vx = -3; |
| |
| while (!WindowShouldClose()) |
| { |
| rect_vx = -3; |
| |
| if (GetKeyPressed() == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (GetKeyPressed() == 'q') |
| { |
| break; |
| } |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| |
| |
| |
| if ( (rect_left_x <= ball_x + radius) |
| && (rect_left_x + rect_width >= ball_x - radius) |
| && (height - rect_height <= ball_y + radius)) |
| { |
| rect_vx = -1; |
| } |
| |
| rect_left_x = rect_left_x + rect_vx; |
| if (rect_left_x <= 0) |
| { |
| rect_left_x = width; |
| } |
| |
| |
| BeginDrawing(); |
| { |
| ClearBackground(BLACK); |
| DrawCircle(ball_x, ball_y, 20, WHITE); |
| DrawRectangle(rect_left_x, height - rect_height, rect_width, rect_height, WHITE); |
| } |
| EndDrawing(); |
| } |
| |
| CloseWindow(); |
| } |
| |
| |
| void demo_3_5_4() |
| { |
| float width = 600; |
| float height = 400; |
| InitWindow(width, height, "don't touch the square"); |
| SetTargetFPS(60); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| float rect_height = 100; |
| float rect_width = 20; |
| float rect_left_x = width * 3 / 4; |
| float rect_top_y = height - rect_height; |
| float rect_vx = -3; |
| float old_rect_vx = -3; |
| |
| while (!WindowShouldClose()) |
| { |
| rect_vx = old_rect_vx; |
| |
| if (GetKeyPressed() == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (GetKeyPressed() == 'q') |
| { |
| break; |
| } |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| |
| |
| |
| if ( (rect_left_x <= ball_x + radius) |
| && (rect_left_x + rect_width >= ball_x - radius) |
| && (height - rect_height <= ball_y + radius)) |
| { |
| old_rect_vx = rect_vx; |
| rect_vx = rect_vx + 2; |
| } |
| |
| rect_left_x = rect_left_x + rect_vx; |
| if (rect_left_x <= 0) |
| { |
| rect_left_x = width; |
| rect_height = rand() % int(height / 4) + height / 4; |
| rect_vx = rand() / float(RAND_MAX) * 4 - 7; |
| old_rect_vx = rect_vx; |
| } |
| |
| |
| BeginDrawing(); |
| { |
| ClearBackground(BLACK); |
| DrawCircle(ball_x, ball_y, 20, WHITE); |
| DrawRectangle(rect_left_x, height - rect_height, rect_width, rect_height, WHITE); |
| } |
| EndDrawing(); |
| } |
| |
| CloseWindow(); |
| } |
| |
| |
| |
| void demo_3_6() |
| { |
| float width = 600; |
| float height = 400; |
| InitWindow(width, height, "don't touch the square"); |
| SetTargetFPS(60); |
| |
| float gravity = 0.6; |
| float radius = 20; |
| float ball_x = width / 4; |
| float ball_y = height - radius; |
| float ball_vy = 0; |
| |
| float rect_height = 100; |
| float rect_width = 20; |
| float rect_left_x = width * 3 / 4; |
| float rect_top_y = height - rect_height; |
| float rect_vx = -3; |
| float old_rect_vx = -3; |
| |
| int score = 0; |
| bool first_time_being_left = false; |
| bool collision = false; |
| |
| while (!WindowShouldClose()) |
| { |
| rect_vx = old_rect_vx; |
| |
| if (GetKeyPressed() == ' ') |
| { |
| ball_vy = -16; |
| } |
| else if (GetKeyPressed() == 'q') |
| { |
| break; |
| } |
| ball_vy = ball_vy + gravity; |
| ball_y = ball_y + ball_vy; |
| if (ball_y >= height - radius) |
| { |
| ball_vy = 0; |
| ball_y = height - radius; |
| } |
| |
| |
| |
| |
| |
| if ( (rect_left_x <= ball_x + radius) |
| && (rect_left_x + rect_width >= ball_x - radius) |
| && (height - rect_height <= ball_y + radius)) |
| { |
| score = 0; |
| collision = true; |
| old_rect_vx = rect_vx; |
| rect_vx = rect_vx + 2; |
| } |
| |
| |
| if (rect_left_x + rect_width < ball_x - radius && first_time_being_left == false && collision == false) |
| { |
| first_time_being_left = true; |
| score++; |
| } |
| |
| rect_left_x = rect_left_x + rect_vx; |
| |
| if (rect_left_x <= 0) |
| { |
| rect_left_x = width; |
| first_time_being_left = false; |
| collision = false; |
| |
| rect_height = rand() % int(height / 4) + height / 4; |
| rect_vx = rand() / float(RAND_MAX) * 4 - 7; |
| old_rect_vx = rect_vx; |
| } |
| |
| char s[20] = { 0 }; |
| sprintf(s, "score: %d", score); |
| |
| |
| BeginDrawing(); |
| { |
| ClearBackground(BLACK); |
| DrawCircle(ball_x, ball_y, 20, WHITE); |
| DrawRectangle(rect_left_x, height - rect_height, rect_width, rect_height, WHITE); |
| |
| const int pos_x = 50; |
| const int pos_y = 30; |
| const int font_size = 40; |
| DrawText(s, pos_x, pos_y, font_size, LIGHTGRAY); |
| } |
| EndDrawing(); |
| } |
| |
| CloseWindow(); |
| } |
| |
| int main() |
| { |
| |
| |
| |
| |
| |
| |
| demo_3_6(); |
| |
| return 0; |
| } |
Greatness is never a given, it must be earned.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
2019-01-25 获取显卡的cuda算力