点击字母M有惊喜
/*
Mathematics
*/
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <easyx.h>
#include <math.h>
#define L 4
#define H 80
#define T 0.3141592653589793
#define T0 10
#define PI 3.1415926535897932
HWND hwnd;
int len;
short time;
double counter;
int iop[32] = { 0, 512, 1024 };
TCHAR text[32][64] = { L"按D控制火柴人向右", L"按A控制火柴人向左", L"按W控制火柴人跳跃"};
struct Node {
double r;
};
struct Point3 {
double x[3];
};
struct Player {
short x;
short y;
short stl; // 0 静止 1 向左 2 向右 3 跳跃
// 0 左; 1 右;
Node arm[2][2];
Node leg[2][2];
}player;
static inline bool KEYDOWN(int vk_code) {
return ((GetAsyncKeyState(vk_code) & 0x8000) ? true : false);
}
static inline int ab(int a, int b) {
return a * a + b * b;
}
static inline double abc(double a, double b, double c) {
return sqrt(a * a + b * b + c * c);
}
static void init() {
hwnd = initgraph(512, 512);
SetWindowText(hwnd, L"Mathematics");
releasecapture();
BeginBatchDraw();
player.x = 255;
player.y = 360;
player.stl = 0;
counter = 0.0;
return;
}
static void display(short num) {
short x1, x2, x3, y1, y2, y3;
clearrectangle(0, 0, 512, 512);
outtextxy(4, 4, L"关卡:");
if (num > 10)
outtextxy(40, 4, TCHAR(48 + num / 10));
outtextxy(48, 4, TCHAR(48 + num % 10));
setlinestyle(PS_SOLID, 2);
if (player.stl == 0) circle(player.x, player.y - 8 * L, 2 * L);
if (player.stl == 3) circle(player.x, player.y - 8 * L, 2 * L);
if (player.stl == 1) circle(player.x - int(sin(PI / 12) * 8 * L + 0.5), player.y - int(cos(PI / 12) * 8 * L + 0.5), 2 * L);
if (player.stl == 2) circle(player.x + int(sin(PI / 12) * 8 * L + 0.5), player.y - int(cos(PI / 12) * 8 * L + 0.5), 2 * L);
if (player.stl == 0) x1 = player.x;
if (player.stl == 3) x1 = player.x;
if (player.stl == 1) x1 = player.x - int(sin(PI / 12) * 6 * L + 0.5);
if (player.stl == 2) x1 = player.x + int(sin(PI / 12) * 6 * L + 0.5);
x2 = x1 + short(sin(player.arm[0][0].r) * 4 * L + 0.5);
x3 = x2 + short(sin(player.arm[0][1].r) * 4 * L + 0.5);
y1 = player.y - 6 * L;
y2 = y1 + short(cos(player.arm[0][0].r) * 4 * L + 0.5);
y3 = y2 + short(cos(player.arm[0][1].r) * 4 * L + 0.5);
line(x1, y1, player.x, player.y);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
if (player.stl == 0) x1 = player.x;
if (player.stl == 3) x1 = player.x;
if (player.stl == 1) x1 = player.x - int(sin(PI / 12) * 6 * L + 0.5);
if (player.stl == 2) x1 = player.x + int(sin(PI / 12) * 6 * L + 0.5);
x2 = x1 + short(sin(player.arm[1][0].r) * 4 * L + 0.5);
x3 = x2 + short(sin(player.arm[1][1].r) * 4 * L + 0.5);
y1 = player.y - 6 * L;
y2 = y1 + short(cos(player.arm[1][0].r) * 4 * L + 0.5);
y3 = y2 + short(cos(player.arm[1][1].r) * 4 * L + 0.5);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
x1 = player.x;
x2 = x1 + short(sin(player.leg[0][0].r) * 5 * L + 0.5);
x3 = x2 + short(sin(player.leg[0][1].r) * 5 * L + 0.5);
y1 = player.y;
y2 = y1 + short(cos(player.leg[0][0].r) * 5 * L + 0.5);
y3 = y2 + short(cos(player.leg[0][1].r) * 5 * L + 0.5);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
x1 = player.x;
x2 = x1 + short(sin(player.leg[1][0].r) * 5 * L + 0.5);
x3 = x2 + short(sin(player.leg[1][1].r) * 5 * L + 0.5);
y1 = player.y ;
y2 = y1 + short(cos(player.leg[1][0].r) * 5 * L + 0.5);
y3 = y2 + short(cos(player.leg[1][1].r) * 5 * L + 0.5);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
setlinestyle(PS_SOLID, 1);
line(0, 400, 512, 400);
FlushBatchDraw();
return;
}
static void rotate(short right) {
player.arm[0][0].r = right * (-PI / 12.0 - sin(counter) * PI / 3.0);
player.arm[1][0].r = right * (-PI / 12.0 + sin(counter) * PI / 3.0);
player.arm[0][1].r = right * (PI / 3.0 - sin(counter) * PI / 3.0);
player.arm[1][1].r = right * (PI / 3.0 + sin(counter) * PI / 3.0);
player.leg[0][0].r = right * (PI / 12.0 - sin(counter) * PI / 6.0);
player.leg[1][0].r = right * (PI / 12.0 + sin(counter) * PI / 6.0);
player.leg[0][1].r = right * (-PI / 6.0 - sin(counter) * PI / 3.0);
player.leg[1][1].r = right * (-PI / 6.0 + sin(counter) * PI / 3.0);
return;
}
static void jump() {
player.y = 360 + H * time * time / T0 / T0 - H;
if (time == T0) player.stl = 0;
else time++;
}
static void reset() {
player.arm[0][0].r /= 1.5;
player.arm[1][0].r /= 1.5;
player.arm[0][1].r /= 1.5;
player.arm[1][1].r /= 1.5;
player.leg[0][0].r /= 1.5;
player.leg[1][0].r /= 1.5;
player.leg[0][1].r /= 1.5;
player.leg[1][1].r /= 1.5;
return;
}
static void input() {
if (KEYDOWN('W')) {
if (player.stl != 3) {
time = -T0;
player.stl = 3;
}
}
else if (player.y == 360){ player.stl = 0; reset(); }
if (KEYDOWN('A') && player.stl != 3) { counter -= T; player.stl = 1; rotate(-1); }
if (KEYDOWN('D') && player.stl != 3) { counter += T; player.stl = 2; rotate(1); }
if (player.stl == 3) jump();
return;
}
static void game(short num) {
while (true) {
input();
display(num);
Sleep(40);
}
return;
}
static void regular_twenty_aspect() {
double k = (sqrt(5) - 1.0) / 2.0;
Point3 P[] = {
{-k, -1, 0}, {k ,-1, 0}, {-k, 1, 0}, {k, 1, 0},
{-1, 0, -k}, {-1, 0, k}, {1, 0, -k}, {1, 0, k},
{0, -k, -1}, {0, -k, 1}, {0, k, -1}, {0, k, 1}
};
double a = 0, b = 0, c = 0;
while (true) {
Point3 R[12]{};
double A[3][3] = {
{cos(a) * cos(b), -sin(a) * cos(c) - cos(a) * sin(b) * sin(c), sin(a) * sin(c) - cos(a) * sin(b) * cos(c)},
{sin(a) * cos(b), cos (a) * cos(c) - sin(a) * sin(b) * sin(c), -cos(a)* sin(c) - sin(a) * sin(b) * cos(c)},
{sin(b), cos(b) * sin(c), cos(b) * cos(c)}
};
for (short l = 0; l < 12; l++) {
for (short i = 0; i < 3; i++)
for (short u = 0; u < 3; u++)
R[l].x[i] += A[i][u] * P[l].x[u];
}
clearrectangle(0, 0, 512, 512);
for (short i = 0; i < 12; i++) {
for (short j = 0; j < 12; j++) {
if (i != j && R[i].x[2] >= -0.45 && R[j].x[2] >= -0.45 && abc(R[i].x[0] - R[j].x[0], R[i].x[1] - R[j].x[1], R[i].x[2] - R[j].x[2]) <= 2 * k + 0.01) {
line(int(R[i].x[0] * 64 + 0.5) + 255, int(R[i].x[1] * 64 + 0.5) + 255, int(R[j].x[0] * 64 + 0.5) + 255, int(R[j].x[1] * 64 + 0.5) + 255);
}
}
}
FlushBatchDraw();
a += PI / 180;
b += PI / 90;
c += PI / 60;
if (KEYDOWN(VK_ESCAPE)) break;
if (KEYDOWN(VK_SPACE)) Sleep(4000);
Sleep(40);
}
return;
}
static void xOy() {
ExMessage msg;
POINT ptx[] = { {480, 253}, {480, 257}, {484, 255} };
POINT pty[] = { {253, 32}, {257, 32}, {255, 28} };
BEGIN:
clearrectangle(0, 0, 512, 512);
setlinecolor(RGB(255, 255, 255));
setfillcolor(RGB(255, 255, 255));
setlinestyle(PS_SOLID, 1);
line(32, 255, 480, 255);
line(255, 32, 255, 480);
outtextxy(251, 8, L'Y');
outtextxy(488, 247, L'X');
outtextxy(239, 260, L'O');
outtextxy(4, 4, L"Mathematics");
fillpolygon(ptx, 3);
fillpolygon(pty, 3);
for (short i = -4; i <= 4; i++) {
putpixel(255 + i * 50, 254, RGB(255, 255, 255));
if (i < 0) {
outtextxy(247 + i * 50, 238, L'-');
outtextxy(255 + i * 50, 238, TCHAR(48 - i));
}
if (i > 0) {
outtextxy(251 + i * 50, 238, TCHAR(48 + i));
}
}
for (short i = -4; i <= 4; i++) {
putpixel(256, 255 + i * 50, RGB(255, 255, 255));
if (i < 0) {
outtextxy(260, 247 + i * 50, TCHAR(48 - i));
outtextxy(268, 247 + i * 50, L'i');
}
if (i > 0) {
outtextxy(260, 247 + i * 50, L'-');
outtextxy(268, 247 + i * 50, TCHAR(48 + i));
outtextxy(276, 247 + i * 50, L'i');
}
}
FlushBatchDraw();
while (true) {
if (KEYDOWN(VK_ESCAPE)) {
goto END;
}
if (peekmessage(&msg, EX_MOUSE)) {
if (!msg.lbutton) continue;
short num = -1;
for (short i = -4; i <= 4; i++) {
if (i == 0) continue;
int r2 = ab(int(msg.x - 255 - i * 50), int(msg.y - 255));
if (r2 <= 16) {
if (i > 0) num = i;
else num = 4 - i;
}
}
for (short i = -4; i <= 4; i++) {
if (i == 0) continue;
int r2 = ab(int(msg.y - 255 + i * 50), int(msg.x - 255));
if (r2 <= 16) {
if (i > 0) num = 8 + i;
else num = 12 - i;
}
}
if (ab(msg.x, msg.y) <= 1024) {
regular_twenty_aspect();
Sleep(1000);
break;
}
if (num != -1) {
setfillcolor(RGB(0, 0, 0));
len = 0;
game(num);
Sleep(1000);
break;
}
}
}
goto BEGIN;
END:
return;
}
static void close() {
EndBatchDraw();
closegraph();
}
int main() {
init();
xOy();
close();
return 0;
}