平抛运动模拟器

· · 个人记录

#include<iostream>
#include<fstream>
#include<windows.h>
#include<conio.h>
#include<stdio.h>
#include<time.h>
#include<sstream>
#include<cstring>
#include<cmath>
#include<iomanip>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
int a[36][76],px,py;
double x,y,v0,g,t,h;
void console(){
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);  
    CONSOLE_SCREEN_BUFFER_INFO bInfo; 
    GetConsoleScreenBufferInfo(hOut, &bInfo );  
    SetConsoleTitle("平抛运动模拟器 - 兴安狠活");
}
void color(int a){
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void cls(){//赟刑道提供,如有侵权请联系删除。    
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coordScreen = { 0, 0 };    // home for the cursor
    SetConsoleCursorPosition( hConsole, coordScreen );
}
void Line(string str){//该函数如需借鉴请先联系沙兴安 
    int i,l,w;
    w=152;
    l=str.length();
    for(i=0;i<(w-l)/2;i++)
    cout<<" ";
    cout<<str<<endl;
    return ;
}
void line(string str,int w){//该函数如需借鉴请先联系沙兴安 
    int i,l;
    l=str.length();
    for(i=0;i<(w-l)/2;i++)
    cout<<" ";
    cout<<str;
    return ;
}
void printline(string str){//该函数如需借鉴请先联系沙兴安 
    int i,l,w;
    w=152;
    l=str.length();
    color(7);
    for(i=0;i<(w-l)/2;i++)
    cout<<"=";
    color(14);
    cout<<str;
    color(7);
    for(i=0;i<(w-l)/2;i++)
    cout<<"=";
    if(str.length()%2==1) cout<<"=";
    cout<<endl;
    return ;
}
void render(){
    x=v0*t;
    y=h-g*t*t*0.5;
    px=round(x);
    py=round(y);
    for(int i=35;i>=1;i--){
        if(i%2==0) color(159);
        else color(223);
        if(i<10) cout<<"0";
        cout<<i;
        for(int j=1;j<=75;j++){
            if(i==py && j==px){
                a[i][j]=1;
                color(191);
                cout<<"  ";
            }
            else{
                if(a[i][j]==1){
                    color(255);
                    cout<<"  "; 
                }else{
                    color(7);
                    cout<<"  "; 
                }
            }
        }
        cout<<endl;
    }
    color(159);
    cout<<"00";
    for(int i=1;i<=75;i++){
        if(i%2==0) color(159);
        else color(223);
        if(i<10) cout<<"0";
        cout<<i;
    }
    cout<<endl;
    printline("平抛运动模拟器");
    cout<<"坐标:("<<fixed<<setprecision(2)<<y<<","<<fixed<<setprecision(2)<<x<<")\t\t位移:";
    cout<<fixed<<setprecision(5)<<sqrt(x*x+(h-y)*(h-y));
    cout<<"m\t\tt="<<fixed<<setprecision(1)<<t<<"            ";
}
int main(){
    console();
    system("mode con:cols=153 lines=43");
    color(11);
    Line("兴安科技出品 - 兴安狠活系列");
    color(14);
    Line("平抛运动模拟器");
    color(7);
    cout<<"输入初始速度(单位:m/s):";
    cin>>v0;
    cout<<"输入重力加速度(单位:m/s^2,常规值为9.8m/s^2):"; 
    cin>>g;
    cout<<"输入抛出高度(单位:m,最高35m):"; 
    cin>>h;
    if(h>35) h=35;
    system("cls");
    while(y>=0){
        cls();
        render();
        Sleep(100);
        if(v0>=15){
            for(int k=1;k<=4;k++){
                t+=0.025;
                x=v0*t;
                y=h-g*t*t*0.5;
                px=round(x);
                py=round(y);
                for(int i=1;i<=35;i++){
                    for(int j=1;j<=75;j++){
                        if(i==py && j==px) a[i][j]=1;
                    }
                }
            }   
        }else t+=0.1;
    }
    return 0;
}