用c语言编写一个台球游戏,使用dev c++运行
时间: 2024-02-27 15:56:04 浏览: 222
基于C++的台球游戏源码.rar
5星 · 资源好评率100%
好的,以下是一个简单的用 C 语言编写的台球游戏代码。请您在 Dev-C++ 编辑器中打开一个新的源文件,将以下代码复制进去,然后保存并运行即可。
```
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define PI 3.1415926
#define WIDTH 480
#define HEIGHT 640
#define BALL_RADIUS 20
#define BALL_DIAMETER 40
#define BALL_SPEED 10
int ball_x = WIDTH / 2 - BALL_RADIUS; // 球的初始位置
int ball_y = HEIGHT / 2 - BALL_RADIUS;
int ball_dir = 0; // 球的初始运动方向
int ball_speed = BALL_SPEED; // 球的初始速度
int ball_color = 4; // 球的初始颜色
int score = 0; // 得分
int left, right, top, bottom; // 桌子的边界
void draw_ball()
{
setcolor(ball_color);
setfillstyle(1, ball_color);
fillellipse(ball_x, ball_y, BALL_RADIUS, BALL_RADIUS);
}
void erase_ball()
{
setcolor(0);
setfillstyle(1, 0);
fillellipse(ball_x, ball_y, BALL_RADIUS, BALL_RADIUS);
}
void move_ball()
{
erase_ball();
double rad = ball_dir * PI / 180.0;
ball_x += ball_speed * cos(rad);
ball_y += ball_speed * sin(rad);
if (ball_x <= left + BALL_RADIUS || ball_x >= right - BALL_RADIUS) {
ball_dir = 180 - ball_dir;
ball_x += ball_speed * cos(rad);
ball_y += ball_speed * sin(rad);
}
if (ball_y <= top + BALL_RADIUS || ball_y >= bottom - BALL_RADIUS) {
ball_dir = -ball_dir;
ball_x += ball_speed * cos(rad);
ball_y += ball_speed * sin(rad);
}
draw_ball();
}
int main()
{
initgraph(WIDTH, HEIGHT);
srand((unsigned)time(NULL));
left = WIDTH / 4;
right = WIDTH * 3 / 4;
top = HEIGHT / 4;
bottom = HEIGHT * 3 / 4;
draw_ball();
while (1) {
if (kbhit()) {
char ch = getch();
if (ch == 'q') break;
}
move_ball();
delay(50);
}
closegraph();
return 0;
}
```
这是一个简单的模拟台球运动的游戏,您可以根据自己的需求进行修改和完善。
阅读全文