将print_screen改成在特定位置gotoxy绘制用覆盖并重新绘制#include stdioh#include stringh#include windowsh#include stdlibh#include timehconst int N = 2;const int M = 60;const int FRAME_RATE = 10;const int CAR_TIME = 10;con
#include <stdio.h> #include <string.h> #include <windows.h> #include <stdlib.h> #include <time.h>
const int N = 2; const int M = 60; const int FRAME_RATE = 10; const int CAR_TIME = 10; const int MAX_CAR = 10;
void clear(); void cls(); void pause(); void print_screen(); void tick(int ×, int frame_rate); void wait_off(int key); void wait_on(int key); bool get_pressed(int key); void draw(You you, Car cars[], int car_count); void game();
struct Sprite { char shapes[10]; int x, y, len;
Sprite(const int &_x = 0, const int &_y = 0, const char _shapes[] = "%")
{
x = _x, y = _y;
strcpy(shapes, _shapes);
len = strlen(_shapes);
}
void move(int dx, int dy)
{
x += dx;
y += dy;
if (y < 0)
{
y = 0;
}
if (y >= M)
{
y = M - 1;
}
if (x < 0)
{
x = 0;
}
if (x >= N)
{
x = N - 1;
}
}
void draw(char screen[N][M])
{
for (int i = 0; i < len; i++)
{
if (x < 0 || x >= N || y + i < 0 || y + i >= M)
{
continue;
}
screen[x][y + i] = shapes[i];
}
}
virtual void update() {}
};
struct You : Sprite { You() : Sprite(0, 1, "@#>") { }
void update()
{
if (get_pressed(VK_UP) && x > 0)
{
move(-1, 0);
}
else if (get_pressed(VK_DOWN) && x < N - 1)
{
move(1, 0);
}
}
bool touch(Sprite other)
{
if (x == other.x && y + len >= other.y && y <= other.y)
{
return true;
}
return false;
}
bool touch(Sprite other[], int count)
{
for (int i = 0; i < count; i++)
{
if (touch(other[i]))
{
return true;
}
}
return false;
}
};
struct Car : Sprite { Car() : Sprite(rand() % N, M - 1, "<{#") { }
void update()
{
move(0, -1);
if (y >= M / 3)
{
int random = rand() % 5;
if (random == 0)
{
move(1, 0);
}
else if (random == 1)
{
move(-1, 0);
}
}
}
};
char screen[N][M];
void clear() { memset(screen, '.', sizeof(screen)); }
void cls() { system("cls"); }
void pause() { system("pause"); }
void print_screen() { cls(); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { gotoxy(i, j); putchar(screen[i][j]); } } }
void tick(int ×, int frame_rate = FRAME_RATE) { Sleep(1000.0 / frame_rate); times++; }
void wait_off(int key) { while (get_pressed(key)) { } }
void wait_on(int key) { while (!get_pressed(key)) { } }
bool get_pressed(int key) { return (GetAsyncKeyState(key) & 0x8000); }
void draw(You you, Car cars[], int car_count) { clear(); you.draw(screen); for (int i = 0; i < car_count; i++) { cars[i].draw(screen); } }
void game() { srand(time(0)); You you; Car cars[MAX_CAR] = {Car()}; int car_count = 1; int frame_rate = FRAME_RATE; int times = 0; int score = 0;
while (true)
{
if (get_pressed(VK_ESCAPE))
{
break;
}
if (you.touch(cars, car_count))
{
break;
}
if (cars[0].y == 0)
{
car_count--;
score++;
for (int i = 0; i < car_count; i++)
{
cars[i] = cars[i + 1];
}
}
you.update();
for (int i = 0; i < car_count; i++)
{
cars[i].update();
}
if (get_pressed(VK_RIGHT))
{
tick(times, frame_rate * 2);
}
else
{
tick(times, frame_rate);
}
if (times > (frame_rate * 10))
{
frame_rate += 5;
times = 0;
}
if (times % (frame_rate * 3) == 0)
{
if (car_count + 1 < MAX_CAR)
{
cars[car_count++] = Car();
}
}
draw(you, cars, car_count);
print_screen();
printf("times: %d, frame rate: %d, score: %d\n", times, frame_rate, score);
}
cls();
printf("You score is %d\n\n", score);
pause();
}
int main() { game(); return 0;
原文地址: https://www.cveoy.top/t/topic/iKxu 著作权归作者所有。请勿转载和采集!