gpt4 book ai didi

C语言实现简单弹跳球游戏

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章C语言实现简单弹跳球游戏由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例为大家分享了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
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
  // 球的坐标
  int pos_x,pos_y;
  //球坐标的变化
  int x =0;
  int y = 5;
  // 定义一个球的速度
  int velocity_x=1;
  int velocity_y=1;
 
  //定义一个球运动的范围
  int top=0;
  int botton=20;
  int lift=0;
  int right=20;
 
 
  //让球循环来回的跳动
  while (1)
  {
  //x轴的速度变化
  x = x + velocity_x;
  y = y +velocity_y;
 
  //清屏,用于每次绘图,清除上一次球的位置。
  system ( "cls" );
  for (pos_x=0 ; pos_x < x; pos_x ++)
  {
  // y轴每行画换行符。
  printf ( "\n" );
  }
  for ( pos_y =0; pos_y <y; pos_y ++)
  {
  // x轴进行空格即可
  printf ( " " );
  }
  //利用速度velocity来控制球移动的方向
  if ( x == top || x == botton) //如果球的x坐标碰到了最顶端-1,向下运动。碰到最低端20则,向上运动。
  {
  velocity_x =-velocity_x; //改变正负数,则为改变方向
  }
  if ( y == lift || y == right) //如果球的x坐标碰到了最zuo端-1,向下运动。碰到最you端20则,向上运动。
  {
  velocity_y =-velocity_y; //改变正负数,则为改变方向
  }
  //每次清屏后,进行绘0。
  printf ( "0 \n" );
  }
  system ( "pause" );
}

该段落为球弹跳的基本逻辑,可以进行直接粘贴复制。编译运行即可看到效果.

代码已经写好注释。  。

再为大家一段简单的控制台弹跳小球实现代码,感谢原作者的分享:

?
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
#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/feipo_zhm/article/details/88552811 。

最后此篇关于C语言实现简单弹跳球游戏的文章就讲到这里了,如果你想了解更多关于C语言实现简单弹跳球游戏的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com