用户:
RendexORE查看:8 回复:4 评论:8 创建时间:2024-07-09T22:31:42

RendexORE#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include <windows.h>
using namespace std;
const int width = 20;
const int height = 40;
int birdX, birdY;
int pipe1X, pipe1Y, pipe2X, pipe2Y;
int score;
void down()
{
birdY++;
}
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void draw() {
system("cls");
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (i == 0 || i == width - 1 || j == 0 || j == height - 1) {
cout << "#";
} else if (i == birdX && j == birdY) {
cout << "@";
} else if (i == pipe1X || i == pipe2X) {
if (j >= pipe1Y && j <= pipe1Y + 5) {
cout << " ";
} else if (j >= pipe2Y && j <= pipe2Y + 5) {
cout << " ";
} else {
cout << "|";
}
} else {
cout << " ";
}
}
cout << endl;
}
cout << "Score: " << score << endl;
}
void update() {
down();
pipe1X--;
pipe2X--;
if (pipe1X < 0) {
pipe1X = width - 1;
pipe1Y = rand() % (height - 6) + 1;
}
if (pipe2X < 0) {
pipe2X = width - 1;
pipe2Y = rand() % (height - 6) + 1;
}
if (birdX == pipe1X || birdX == pipe2X) {
if (birdY >= pipe1Y && birdY <= pipe1Y + 5) {
score++;
} else if (birdY >= pipe2Y && birdY <= pipe2Y + 5) {
score++;
} else {
exit(0);
}
}
}
int main() {
srand(time(0));
birdX = width / 2;
birdY = height / 2;
pipe1X = width - 1;
pipe1Y = rand() % (height - 6) + 1;
pipe2X = width - 1;
pipe2Y = rand() % (height - 6) + 1;
score = 0;
while (true) {
draw();
if (_kbhit()) {
char key = _getch();
if (key == ' ') {
birdY-=2;
}
}
update();
Sleep(100);
}
system("cls");
cout << "Your Score : " << score << endl ;
system("pause");
system("pause");
return 0;
}
点赞0
评论
imgainary_number#include <iostream>
#include <cmath>
const int screenWidth = 80; // 输出屏幕的宽度
const int screenHeight = 40; // 输出屏幕的高度
struct Vec3 {
double x, y, z;
Vec3(double x_ = 0, double y_ = 0, double z_ = 0) : x(x_), y(y_), z(z_) {}
double lengthSquared() const {
return x * x + y * y + z * z;
}
double length() const {
return sqrt(lengthSquared());
}
Vec3 normalize() const {
double len = length();
return Vec3(x / len, y / len, z / len);
}
Vec3 operator+(const Vec3& rhs) const {
return Vec3(x + rhs.x, y + rhs.y, z + rhs.z);
}
Vec3 operator-(const Vec3& rhs) const {
return Vec3(x - rhs.x, y - rhs.y, z - rhs.z);
}
Vec3 operator*(double scalar) const {
return Vec3(x * scalar, y * scalar, z * scalar);
}
double dot(const Vec3& rhs) const {
return x * rhs.x + y * rhs.y + z * rhs.z;
}
Vec3 cross(const Vec3& rhs) const {
return Vec3(y * rhs.z - z * rhs.y,
z * rhs.x - x * rhs.z,
x * rhs.y - y * rhs.x);
}
};
struct Sphere {
Vec3 center;
double radius;
Sphere(const Vec3& center_, double radius_) : center(center_), radius(radius_) {}
bool intersect(const Vec3& orig, const Vec3& dir, double& t) const {
Vec3 oc = orig - center;
double a = dir.dot(dir);
double b = 2.0 * oc.dot(dir);
double c = oc.dot(oc) - radius * radius;
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) return false;
double sqrtD = sqrt(discriminant);
double t1 = (-b - sqrtD) / (2.0 * a);
double t2 = (-b + sqrtD) / (2.0 * a);
if (t1 > t2) std::swap(t1, t2);
if (t1 < 0) {
t1 = t2;
if (t1 < 0) return false;
}
t = t1;
return true;
}
};
char rayTrace(const Vec3& orig, const Vec3& dir, const Sphere& sphere) {
double t;
if (sphere.intersect(orig, dir, t)) {
Vec3 hitPoint = orig + dir * t;
Vec3 normal = (hitPoint - sphere.center).normalize();
double intensity = normal.dot(Vec3(1, 1, 1).normalize());
const char chars[] = "@%#*+=-:. ";
int charIdx = static_cast<int>(intensity * (sizeof(chars) - 1));
return chars[charIdx];
}
return ' ';
}
int main() {
Sphere sphere(Vec3(0, 0, -10), 3.0);
double screenDist = 10.0;
for (double t = 0; t < 100; t += 0.1) {
Vec3 cameraPos(6 * cos(t), 0, 10 * sin(t));
for (int y = 0; y < screenHeight; ++y) {
for (int x = 0; x < screenWidth; ++x) {
double screenX = (2.0 * x - screenWidth) / double(screenWidth);
double screenY = (2.0 * y - screenHeight) / double(screenHeight);
Vec3 rayDir(screenX, -screenY, -screenDist);
rayDir = rayDir.normalize();
char pixel = rayTrace(cameraPos, rayDir, sphere);
std::cout << pixel;
}
std::cout << std::endl;
}
std::cout << "\x1b[H";
}
return 0;
}
光线追踪
点赞0
评论