服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现简易的弹球小游戏

C++实现简易的弹球小游戏

2022-01-20 14:41飞天烙铁 C/C++

这篇文章主要为大家详细介绍了C++实现简易的弹球小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现弹球小游戏的具体代码,供大家参考,具体内容如下

操作说明:键盘A和D键控制左右移动,让球不要落下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <graphics.h>
#include <conio.h>
#include <time.h>
int i;
int xx=0;
int yy = 0;
class Ball
{
public:
 int x, y;
 clock_t b;
 void draw()
 {
  setfillcolor(RGB(200, 399, 100));
  fillcircle(x, y, 10);
 }
 void null()
 {
  setfillcolor(0);
  setlinecolor(0);
  fillcircle(x, y, 10);
 
 }
};
 
class Board
{
public:
 int x, y;
 void draw()
 {
  setlinecolor(RGB(100, 209, 150));
  setfillcolor(RGB(200, 399, 100));
  fillrectangle(x, y, x+60,y+10);
 
 }
 void null()
 {
  setfillcolor(0);
  setlinecolor(0);
  fillrectangle(x, y, x + 60, y + 10);
 
 }
 
};
 
 
void wall()
{
 setlinecolor(RGB(300, 109, 650));
 setfillcolor(RGB(200, 409, 300));
 POINT pts[] = { {1, 1}, {1, 350}, {10,350 }, {10,10 }, {600, 10}, {600, 350}, {610,350}, {610,1 } };
 fillpolygon(pts, 8);
}
 
 
int main()
{
 
 // 初始化图形窗口
 initgraph(640, 480,4);
 wall();
 Board board;
 board.x = 200;
 board.y = 400;
 Ball ball;
 clock_t f = clock();
 clock_t d = clock();
 
 while (1)
 {
  int i=3 ;
  if (GetAsyncKeyState('A') & 0x8000)//木板移动
  {
   if (board.x > 2 && (clock() - d) >= 10)
   {
    board.null(); board.x -= 3; board.draw(); d = clock();
   }
  }
  if (GetAsyncKeyState('a') & 0x8000)//木板移动
  {
   if (board.x > 2 && (clock() - d) >= 10)
   {
    board.null(); board.x -= 3; board.draw(); d = clock();
   }
  }
 
  if (GetAsyncKeyState('D') & 0x8000)//木板移动
  {
   if (board.x < 600 && (clock() - d) >= 10)
   {
    board.null(); board.x += 3; board.draw(); d = clock();
   }
  }
  if (GetAsyncKeyState('d') & 0x8000)//木板移动
  {
   if (board.x <600 && (clock() - d) >= 10)
   {
    board.null(); board.x += 3; board.draw(); d = clock();
   }
  }
 
  if (GetAsyncKeyState('K') & 0x8000)//发球
  {
    if  (yy==0&&xx==0&&(clock() - f) >= 10)
    {
     yy = 1;
     xx = 1;
     ball.x = board.x + 50;
     ball.y = board.y - 21;
     ball.draw();
     f = clock();
    }
  }
  if (GetAsyncKeyState('k') & 0x8000)//发球
  {
    if (yy==0&&xx==0&& (clock() - f) >= 10)
    {
     yy = 1;
     xx = 1;
     ball.x = board.x + 50;
     ball.y = board.y - 21;
     ball.draw();
     f = clock();
    }
  }
 
   //球的弹射//
  if (yy==1&&xx==1&&clock()-ball.b>5)
  {
   ball.null();
   ball.x -= 2;
   ball.y -= 2;
   ball.draw();
   ball.b = clock();
   if (ball.y <= 21)
   {
    ball.y = 21;
    ball.null();
    xx = 4;
   }
   
   if (ball.x <= 24)
   {
    ball.x = 24;
    ball.null();
    xx = 2;
   }
   
  }
 
 
  if (yy==1&&xx == 2 && clock() - ball.b > 5)
  {
   ball.null();
   ball.x += 2;
   ball.y -= 2;
   ball.draw();
   ball.b = clock();
   if (ball.y <= 21)
   {
    ball.y = 21;
    ball.null();
    xx = 3;
   }
   if (ball.x >= 580)
   {
    ball.x = 580;
    ball.null();
    xx = 1;
   }
   
  }
  if (yy==1&&xx == 3 && clock() - ball.b > 5)
  {
   ball.null();
   ball.x += 2;
   ball.y += 2;
   ball.draw();
   ball.b = clock();
   
   if (ball.x >= 580)
   {
    ball.x = 580;
    ball.null();
    xx = 4;
   }
   
   if (ball.x >= board.x && ball.x <= board.x + 60 && ball.y <= board.y + 9 && ball.y >= board.y - 11)
   {
    ball.null();
    ball.y = board.y - 21;
    ball.draw();
    xx = 2;
   }
  }
 
  if (yy==1&&xx == 4 && clock() - ball.b > 5)
  {
   ball.null();
   ball.x -= 2;
   ball.y += 2;
   ball.draw();
   ball.b = clock();
   
   
   if (ball.x <= 24)
   {
    ball.x = 24;
    ball.null();
    xx = 3;
   }
   if (ball.x >= board.x && ball.x <= board.x + 60 && ball.y <= board.y + 9 && ball.y >= board.y - 11)
   {
    ball.null();
    ball.y = board.y - 21;
    ball.draw();
    xx = 1;
   }
  }
 
  if (ball.y >= 440)
  {
   yy = 0;
   xx = 0;
   ball.null();
  }
 
 }
 
    closegraph();
 return 0;
}

小编再为大家分享一段代码:C语言实现简单的控制台弹跳小球

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
 
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
 
 
// 全局变量
 int x,y;     //小球坐标
 int velocity_x,velocity_y ; //速度
 int left,right,top,bottom; //边界
 
 
void gotoxy(int x,int y)  //光标移动到(x,y)位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}
 
 
void HideCursor() // 用于隐藏光标
{
 CONSOLE_CURSOR_INFO cursor_info = {1, 0};  // 第二个值为0表示隐藏光标
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup() // 数据初始化
{
 x = 1;
 y = 5;
 velocity_x = 1;   //速度方向
 velocity_y = 1;
 left = 0;
 right = 30;
 top = 0;
 bottom = 15;
 
 
 HideCursor();  // 隐藏光标
}
 
 
void show()  // 显示画面
{
 
 int i,j;
 for (i=0;i<=bottom;i++)
 {
  for (j=0;j<=right;j++)
  {
 
 
 
   if((i==x) && (j==y))
    {
     printf("o");    //打印小球
    }
   else if ((i==0)||(i==bottom)||(j==0)||(j==right))  //打印边界
    {
     printf("#");
    }
   else printf(" ");
  }
  printf("\n");
 }
}
void automation()  // 与用户输入无关的更新
{
 x = x + velocity_x;   
 y = y + velocity_y;
 if ((x==top)||(x==bottom))
 {
  velocity_x = -velocity_x;
  printf("\a");
 }
  
 else if ((y==left)||(y==right))
 {
  velocity_y = -velocity_y;
  printf("\a");
 }
   
 Sleep(100);  //调低小球速度
}
int main()
{
 system("color 2f");  //改变控制台颜色
 startup();     // 数据初始化
 while (1)     //  游戏循环执行
 {
  gotoxy(0,0);    // 清屏
  show();     // 显示画面
  automation();    // 与用户输入无关的更新
 }
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_61641934/article/details/120677046

延伸 · 阅读

精彩推荐
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22