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

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

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

C语言实现简单弹球游戏

2021-08-19 12:33繁星璀璨G C/C++

这篇文章主要为大家详细介绍了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
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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <windows.h>
//#include <system.h>
 
#define ROW 20
#define COL 50
 
static void SetPos(int x, int y)//刷新窗口,类似于system(“cls”)
{
  
  COORD point = { x, y };//光标要设置的位置x,y
  HANDLE HOutput = GetStdHandle(STD_OUTPUT_HANDLE);//使用GetStdHandle(STD_OUTPUT_HANDLE)来获取标准输出的句柄
  SetConsoleCursorPosition(HOutput, point);//设置光标位置
}
 
void init(char bg[][COL],char ch)//设置窗口
{
 int i,j;
 for (i=0;i<ROW;i++)
 {
 for (j=0;j<COL;j++)
 {
 if (i==0||i==ROW-1||j==0||j==COL-1)//边缘设置为字符
 {
 bg[i][j]=ch;
 }
 else
 {
 bg[i][j]=' ';//其他位置是空格
 }
 }
 }
}
 
void display(char bg[][COL],char ch)//显示函数,打印bg[i][j]
{
 int i,j;
 for (i=0;i<ROW;i++)
 {
 for (j=0;j<COL;j++)
 {
 printf ("%c",bg[i][j]);
 }
 printf ("\n");
 }
}
 
void move(int *x,int *y,int *x_inc,int *y_inc)//控制球的移动 ,上下,左右分别控制
{
 if (*x==0||*x==ROW-1)//等于0或者 ROW-1,即是边界,便忘反方向走
 {
 *x_inc = -*x_inc;
 }
 *x += *x_inc;//单步移动
 if (*y==0||*y==COL-1)//同上,
 {
 *y_inc = -*y_inc;
 }
 *y += *y_inc;
}
int main( )
{
 int i,j;
 int x=3,y=3,x_inc=1,y_inc=1;//初始化球的位置
 char bg[ROW][COL];
 while(1)//死循环
 {
 SetPos(0,0);//刷新窗口
 init(bg,'#');//窗口边界
 bg[x][y]='O';//球的位置
 move(&x,&y,&x_inc,&y_inc);//移动
 display(bg,'#');//显示
 Sleep(100);//延时,太快看不清球移动
 }
 
 return 0;
}

效果

C语言实现简单弹球游戏

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

原文链接:https://blog.csdn.net/qq_40788199/article/details/84825011

延伸 · 阅读

精彩推荐