一些有趣的代码

谨以此文记录我敲代码过程中的一些小乐趣。

代码很简单很小巧

(部分代码系网络上查阅而来)

代码1.跳动的心脏

#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <tchar.h>

float f(float x, float y, float z) {
    float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
    return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
}

float h(float x, float z) {
    for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
        if (f(x, y, z) <= 0.0f)
            return y;
    return 0.0f;
}

void SetColorAndBackground(int ForgC, int BackC) {
    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}

int main() {
    
    SetColorAndBackground(7,4); //对于颜色的改变
    HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);
    _TCHAR buffer[25][80] = { _T(' ') };
    _TCHAR ramp[] = _T(".:-=+*#%@");

    for (float t = 0.0f;; t += 0.1f) {
        int sy = 0;
        float s = sinf(t);
        float a = s * s * s * s * 0.2f;
        for (float z = 1.3f; z > -1.2f; z -= 0.1f) {
            _TCHAR* p = &buffer[sy++][0];
            float tz = z * (1.2f - a);
            for (float x = -1.5f; x < 1.5f; x += 0.05f) {
                float tx = x * (1.2f + a);
                float v = f(tx, 0.0f, tz);
                if (v <= 0.0f) {
                    float y0 = h(tx, tz);
                    float ny = 0.01f;
                    float nx = h(tx + ny, tz) - y0;
                    float nz = h(tx, tz + ny) - y0;
                    float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
                    float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
                    *p++ = ramp[(int)(d * 5.0f)];
                }
                else
                    *p++ = ' ';
            }
        }

        for (sy = 0; sy < 25; sy++) {
            COORD coord = { 0, sy };
            SetConsoleCursorPosition(o, coord);
            WriteConsole(o, buffer[sy], 79, NULL, 0);
        }
        Sleep(33);
    }
}

效果图 

 


代码2.蓝屏警告 

#include <windows.h>
#include <iostream>

using namespace std;

//----------全屏/隐藏标题栏----------
void full_screen()
{
  HWND hwnd = GetForegroundWindow();
  //----------屏幕分辨率----------
  int cx = GetSystemMetrics(SM_CXSCREEN);
  int cy = GetSystemMetrics(SM_CYSCREEN);

  LONG l_WinStyle = GetWindowLong(hwnd, GWL_STYLE); //获取窗口信息
  SetWindowLong(hwnd, GWL_STYLE,
                (l_WinStyle | WS_POPUP | WS_MAXIMIZE) & ~WS_CAPTION & ~WS_THICKFRAME &
                    ~WS_BORDER);
  SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, 0);
}
//----------隐藏光标----------
void HideTheCursor()
{
  CONSOLE_CURSOR_INFO cciCursor;
  HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  if (GetConsoleCursorInfo(hStdOut, &cciCursor))
  {
    cciCursor.bVisible = FALSE;
    SetConsoleCursorInfo(hStdOut, &cciCursor);
  }
}
int main()
{
  system("mode 96,28");
  //----------移除快速编辑模式(对于win10用户)----------
  HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
  DWORD mode;
  GetConsoleMode(hStdin, &mode);
  mode &= ~ENABLE_QUICK_EDIT_MODE;
  SetConsoleMode(hStdin, mode);
  //----------隐藏光标----------
  HideTheCursor();
  //----------设置字体及颜色----------
  
	//SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
  system("color 1f");
  //----------移动窗口----------
  RECT rect;
  HWND hwnd = GetForegroundWindow();
  GetWindowRect(hwnd, &rect);
  MoveWindow(hwnd, -30, -7, 8600, 1080, TRUE);
  //----------全屏----------
  full_screen();
  //----------打印蓝屏内容----------
  cout << "A problem has been detected and windows has been shut\
down to prevent damage\n\
to your computer.\n\n\
DRIVER_IROL_NOT_LESS_OR_EQUAL\n\n\
If this is the first time you've seen this Stop error screen, \n\
restart your computer.If this screen appears again, follow \n\
these steps:\n\n\
Check to make sure any new hardware or software is properly installed.\n\
If this is a new installation, ask your hardware or software manufacturer\n\
for any windows updates you might need.\n\n\
If problems continue. disable or remove any newly installed hardware\n\
or software. Disable BIOS memory option such as caching or shadowing.\n\
If you need to nsu Safe Mode to remove or disable components, restart\n\
your computer, press F8 to select Advanced Startup Options, and then\n\
select Safe Mode.\n\n\
Technical information:\n\
\"\"\"STOP:0x00000050(0xFD3094c2,0x00000001,0xFBFE7617,0x00000000}\n\n\
\"\"\"SPCMDON.SYS - Address FBFE7617 base at FBFE5000, DateStamp 3d6dd67c\n"
       << endl;

  //----------停止程序(不退出)----------
  while (1)
  {
    //----------再次隐藏光标----------
    HideTheCursor();
    Sleep(1000);
  }
  return 0;
}

效果图 


代码3.屏幕刷新术

#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
//#include<bits/stdc++.h>
using namespace std;
int main()
{
  while(1){
  HWND h=GetForegroundWindow();
	ShowWindow(h,SW_MINIMIZE);
  Sleep(100);
  ShowWindow(h,SW_RESTORE);
  }
  return 0;
}

无效果图(截不了图) 


代码4.随机移动鼠标术 

#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
  POINT p;
  HWND h=GetForegroundWindow();
	ShowWindow(h,SW_MAXIMIZE);
  while(1)
  {
    GetCursorPos(&p);
    cout<<p.x<<" "<<p.y<<endl;
    srand(time(0));
    int num1=rand()%1500+1;
    int num2=rand()%800+1;
    SetCursorPos(num1,num2);
    Sleep(1000);
  }
  system("pause");
  return 0;
}

无效果图(同上) 


代码5.颜色改变术 

#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
using namespace std;
void SetColorAndBackground(int ForgC, int BackC) {
	WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
int main()
{
	//第一个是改变字体颜色,第二个是改变背景颜色
	SetColorAndBackground(5, 3);
	cout << "hello" << endl;
	SetColorAndBackground(7, 2);
	cout << "hello" << endl;
	SetColorAndBackground(7, 6);
	cout << "hello" << endl;
	SetColorAndBackground(7, 4);
	cout << "hello" << endl;
	SetColorAndBackground(7, 5);
	cout << "world" << endl;
	SetColorAndBackground(7, 8);
	cout << "world" << endl;
	SetColorAndBackground(1, 2);
	cout << "";
		return 0;
}

效果图 


代码6.图形移动

#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
using namespace std;
void gotoxy(int x, int y)
{
    COORD pos = { x,y };
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
}
int main()
{
    for (int i = 1; i <= 100; i++)
    {
        gotoxy(0, 0);
        for (int j = 1; j <= 3; j++)
        {
            cout << endl;
            for (int k = 1; k <= i; k++)
                cout << " ";
            cout << "@@@@@@" << endl;
            for (int k = 1; k <= i; k++)
                cout << " ";
            cout << "    @ " << endl;
            for (int k = 1; k <= i; k++)
                cout << " ";
            cout << "   @  " << endl;
            for (int k = 1; k <= i; k++)
                cout << " ";
            cout << "  @   " << endl;
            for (int k = 1; k <= i; k++)
                cout << " ";
            cout << " @    " << endl;
            for (int k = 1; k <= i; k++)
                cout << " ";
            cout << "@@@@@@" << endl;
        }
        Sleep(100);
    }
    system("pause");
    return 0;
}

效果图


本大佬的呕心沥血之作  本萌新的第一篇作品

未完待续。。。

  • 18
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值