字符动画用c++写出蔡徐坤打篮球代码
时间: 2023-09-28 09:12:29 浏览: 613
以下是用C++写出蔡徐坤打篮球的字符动画代码:
```c++
#include <iostream>
#include <windows.h>
using namespace std;
void clear_screen()
{
COORD upper_left = { 0, 0 };
DWORD chars_written;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD console_size;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(console, &csbi);
console_size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(console, ' ', console_size, upper_left, &chars_written);
FillConsoleOutputAttribute(console, csbi.wAttributes, console_size, upper_left, &chars_written);
SetConsoleCursorPosition(console, upper_left);
}
void delay(int milliseconds)
{
Sleep(milliseconds);
}
int main()
{
int x = 0;
int y = 0;
int ball_x = 0;
int ball_y = 0;
int score = 0;
int direction = 0; // 0: right, 1: left
bool is_jumping = false;
bool is_shooting = false;
while (true)
{
clear_screen();
// draw court
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 80; j++)
{
if (i == 0 || i == 24 || j == 0 || j == 79)
{
cout << "#";
}
else if (i == 23 && j == 15)
{
cout << "SCORE: " << score;
}
else if (i == 22 && j == 63)
{
cout << "Cai Xukun Basketball Game";
}
else
{
cout << " ";
}
}
cout << endl;
}
// draw player
if (is_jumping)
{
if (y > 10)
{
y--;
}
else
{
is_jumping = false;
}
}
else
{
if (y < 22)
{
y++;
}
}
if (direction == 0)
{
for (int i = 0; i < x; i++)
{
cout << " ";
}
cout << "O";
x++;
if (x == 75)
{
direction = 1;
}
}
else
{
for (int i = 79; i > x; i--)
{
cout << " ";
}
cout << "O";
x--;
if (x == 5)
{
direction = 0;
}
}
// draw ball
if (is_shooting)
{
if (ball_x < 70)
{
ball_x++;
ball_y--;
}
else
{
is_shooting = false;
if (ball_y >= y && ball_y <= y + 2)
{
score++;
}
}
}
else
{
ball_x = x + 1;
ball_y = y + 1;
}
for (int i = 0; i < ball_y; i++)
{
cout << endl;
}
for (int i = 0; i < ball_x; i++)
{
cout << " ";
}
cout << "*";
delay(100);
if (GetAsyncKeyState(VK_SPACE) & 0x8000)
{
if (!is_jumping)
{
is_jumping = true;
}
}
if (GetAsyncKeyState(VK_RETURN) & 0x8000)
{
if (!is_shooting)
{
is_shooting = true;
}
}
}
return 0;
}
```
运行该程序,即可看到蔡徐坤在打篮球的字符动画效果。
阅读全文