C语言课程设计与游戏开发实践教程
第一章 C游戏开发快速入门
- 学习本章前需要掌握的语法知识:标识符、变量、常量、运算符与表达式,以及printf、scanf、if-else、while、for语句的用法
- 原书中环境VS2010
- 随书资源[密:cq2g]
1.1弹跳球小球
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
int main(void)
{
int i, j;
int x = 0;
int y = 5;
int height = 20;
int velocity_x = 1;
int velocity_y = 1;
int left = 0;
int right = 20;
int top = 0;
int bottom = 10;
while ( 1 )
{
x += velocity_x;
y += velocity_y;
system("cls"); // 清屏函数
// 输出小球上面的空行
for ( i = 0; i < x; i++ )
printf("\n");
// 输出小球左边的空行
for ( j = 0; j < y; j++ )
printf(" ");
printf("o"); // 输出小球o
printf("\n");
Sleep(50); // 在输出图形后等待50ms
if ( x == top || x == bottom )
velocity_x = -velocity_x;
if ( y == left || y == right )
velocity_y = -velocity_y;
}
return 0;
}
思考题
1.如果不用Sleep函数,能否利用循环语句实现速度变慢的效果?
2.尝试利用printf("\a")实现小球碰撞到边界时响铃的效果。
3.尝试为反弹球游戏绘制边框。
1.2 简单的飞机游戏
#include <stdio.h>
#include <stdlib.h>
#include <conio.h> // 输入函数:getch()、kbhit()
int main(void)
{
int i, j;
int x = 5;
int y = 10;
char input;
int isFire = 0; // 1显示飞机在正上方输出激光竖线
int ny = 5; // 第一个靶子,放在第一行的ny列上
int isKilled = 0;
// 0显示靶子,1靶子被击中则不显示
while ( 1 )
{
system("cls"); // 清屏函数
if ( !isKilled ) // 输出靶子
{
for ( j = 0; j < ny; j++ )
printf(" ");
printf("+ \n");
}
if ( isFire == 0 ) // 输出飞机上面的空行
{
for ( i = 0; i < x; i++ )
printf("\n");
}else
{
for ( i = 0; i < x; i++ ) // 输出飞机上面的激光光线
{
for ( j = 0; j < y; j++ )
printf(" ");
printf(" |\n");
}
if ( ny == y + 2 ) // +2 是因为激光在飞机的正中间,距离左边的而坐标
isKilled = 1; // 击中靶子
isFire = 0;
}
// 下面输出一个复杂的飞机图案
for ( j = 0; j < y; j++ )
printf(" ");
printf(" * \n"); // 输出飞机
for ( j = 0; j < y; j++ )
printf(" ");
printf("*****\n");
for ( j = 0; j < y; j++ )
printf(" ");
printf(" * * \n");
if ( kbhit() ) // 判断是否有输入
{
input = getch(); // 根据用户的不同输入来移动,不必输入回车
if ( input == 'a' )
y--; // 位置左移
if ( input == 'd' )
y++; // 位置右移
if ( input == 'w' )
x--; // 位置上移
if ( input == 's' )
x++; // 位置下移
if ( input == ' ' )
isFire = 1;
}
}
return 0;
}
思考题
1.如何让靶子移动起来。
2.如何统计和显示击中得分。
总结
1、大框架:建立(x,y)坐标【行、列】建立循环体。
2、循环体作用域中使用if else语句创建图案。
3、对于不同的图案可使用标记变量进行操作判断。
(如果这个点需要移动则放在大框内,否则放在框架外)
//以下函数只能在windows os使用
#include <stdlib.h> // 提供system("cls");清屏函数
#include <windows.h> // 提供Sleep(50);睡眠50ms
#include <conio.h>
/*
提供: 输入函数:getch(),不需要回车就可以得到输入行的字符。
kbhit()函数在用户有键盘输入时,返回1,否则返回0;
*/
第二章 函数封装的游戏开发
2.1 飞机游戏
#include <stdio.h>
#include <stdlib.h> // 提供 system("cls"); 清屏函数
#include <conio.h> // 提供 getch()、kbhit() 函数
#include <Windows.h> // 提供 void gotoxy(int x, int y); 函数
// 全局变量
int position_x, position_y; // 飞机位置
int bullet_x, bullet_y; // 子弹位置
int enemy_x, enemy_y; // 敌机位置
int high, width; // 游戏画面尺寸
int score; // 得分
void startup() // 数据初始化
{
score = 0;
high = 20;
width = 30;
position_x = high/2;
position_y = width/2;
bullet_x = -1;
bullet_y = position_y;
enemy_x = 0;
enemy_y = position_y;
}
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 show() /* 展示画面 */
{
//system("cls"); // 清屏
gotoxy(0,0); // 光标移动到原点位置,以下重画清屏
int i, j;
for ( i = 0; i < high; i++ )
{
for ( j = 0; j < width; j++ )
{
if ( (i == position_x) && (j == position_y) )
printf("*"); // 输出飞机*
else if ( (i == enemy_x) && (j == enemy_y) )
printf("@"); // 输出敌机@
else if( (i == bullet_x) && (j == bullet_y) )
printf("|"); // 输出子弹|
else
printf(" "); // 输出空格
}
printf("\n");
}
printf("得分:%d\n", score);
}
void updateWithoutInput() /* 与用户输入无关的更新 */
{
if ( bullet_x > -1 )
bullet_x--;
// 用来控制敌机向下移动的速度,每隔几次循环才移动一次敌机
// 这样修改,虽然用户按键的交互速度还是很快,但 NPC 的移动显示可以降速
static int speed = 0;
if ( speed < 10 )
{
speed++;
}
if ( speed == 10 )
{
enemy_x++;
speed = 0;
}
if ( (bullet_x == enemy_x) && (bullet_y == enemy_y) ) //子弹击中敌机
{
score++;
enemy_x = -1; // 产生新的飞机
enemy_y = rand() % width;
bullet_x = -2; // 子弹无效(防止穿射)【等于0以下均可,这里只是用-2标识被击落】
}
if ( enemy_x > high ) // 敌机抛出显示屏幕
{
enemy_x = -1; // 产生新的飞机
enemy_y = rand() % width;
}
}
void updateWithInput() /* 与用户输入有关的更新 */
{
char input;
if (kbhit()) // 判断是否有输入
{
input = getch(); // 根据用户的不同输入来移动,不必输入回车
if ( input == 'a' )
position_y--; // 位置左移
if ( input == 'd' )
position_y++; // 位置右移
if ( input == 'w' )
position_x--; // 位置上移
if ( input == 's' )
position_x++; // 位置下移
if ( input == ' ' )
{
bullet_x = position_x - 1; // 发射子弹的初始位置在飞机的正上方
bullet_y = position_y;
}
}
}
int main()
{
startup(); // 数据的初始化
while ( 1 ) // 游戏循环执行
{
show(); // 显示画面
updateWithoutInput(); // 与用户输入无关的更新
updateWithInput(); // 与用户输入有关的更新
}
return 0;
}
思考题:
1.参考1.2.3节中的方法,尝试实现复杂的飞机图形。
2.随着积分的增加加快敌机的下落速度。
3.防止玩家操控飞机飞出边界。
4.增加按Esc键后暂停的功能。
2.2 用函数实现反弹球消砖块
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
// 全局变量
int high, width; // 游戏画面大小
int ball_x, ball_y; // 小球的坐标
int ball_vx, ball_vy; // 小球的速度
int position_x, position_y; // 挡板的中心坐标
int ridus; // 挡板的半径大小
int left, right; // 挡板的左右位置
int ball_number; // 反弹小球的次数
int block_x, block_y; // 砖块的位置
int score; // 消除砖块的个数
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 startup() /* 数据初始化 */
{
high = 15;
width = 20;
ball_x = 0;
ball_y = width / 2;
ball_vx = 1;
ball_vy = 1;
ridus = 5;
position_x = high;
position_y = width / 2;
left = position_y - ridus;
right = position_y + ridus;
ball_number = 0;
block_x = 0;
block_y = width / 2 + 1;
score = 0;
}
void show() /* 显示画面 */
{
gotoxy(0, 0); // 光标移动到原点位置,以下重画清屏
int i, j;
for ( i = 0; i <= high + 1; i++ )
{
for ( j = 0; j <= width; j++ )
{
if ( (i == ball_x) && (j == ball_y) )
printf("0"); // 输出小球
else if ( j == width )
printf("|"); // 输出右边框
else if ( i == high + 1 )
printf("-"); // 输出下边框
else if ( (i == position_x) && (j >= left) && (j <= right) )
printf("*"); // 输出挡板
else if ( (i == block_x) && (j == block_y) )
printf("B");
else
printf(" "); // 输出空格
}
printf("\n");
}
printf("反弹小球数:%d\n", ball_number);
printf("消除的砖块数:%d\n", score);
}
void updateWithoutInput() /* 与用户输入无关的更新 */
{
if ( ball_x == high - 1 )
{
if ( (ball_y >= left) && (ball_y <= right) ) // 被挡板挡住
{
ball_number++;
printf("\a");
}else
{
printf("游戏失败\n");
system("pause");
exit(EXIT_FAILURE);
}
}
if ( (ball_x == block_x) && (ball_y == block_y) )
{
score++; // 分数加1
block_y = rand() % width; // 产生新的砖块
}
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
if ( (ball_x == 0) || (ball_x == high - 1) )
ball_vx = -ball_vx;
if ( (ball_y == 0) || (ball_y == width - 1) )
ball_vy = -ball_vy;
Sleep(80);
}
void updateWithInput() /* 与用户输入有关的更新 */
{
char input;
if ( kbhit() ) // 判断是否有输入
{
input = getch(); // 根据用户的不同输入来移动,不必输入回车
if ( input == 'a' )
{
position_y--; // 位置左移
left = position_y - ridus;
right = position_y + ridus;
}
if ( input == 'd' )
{
position_y++; // 位置右移
left = position_y - ridus;
right = position_y + ridus;
}
}
}
int main(void)
{
startup(); // 游戏的初始化
while ( 1 )
{
show(); // 显示画面
updateWithoutInput(); // 与用户输入无关的更新
updateWithInput(); // 与用户输入有关的更新
}
return 0;
}
思考题
1、增加砖块,使得集中概率增大。
2、实现对小球更多的操控,从而可以调整击中的砖块。
2.3 flappy bird
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h>
// 全局变量
int high, width; // 游戏画面大小
int bird_x, bird_y; // 小鸟的坐标
int bar1_y, bar1_xDown, bar1_xTop; // 障碍物
int score; // 得分,经过障碍物的个数
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 startup() /* 数据的初始化 */
{
high = 20;
width = 59;
bird_x = 0;
bird_y = width / 3;
bar1_y = width / 2;
bar1_xDown = high / 3;
bar1_xTop