栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

球与地下城——C语言游戏

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

球与地下城——C语言游戏

游戏简介:

这个游戏就是要让小球不断往下面探索,“A”左移,“D”右移。争取跳到方块上,如果小球碰到顶部或者没有踩到方块,游戏就会结束,随着时间推进,方块上升的速度会变快。游戏结束时会有结算画面,显示得分情况。

提前准备:

1.首先要安装好Visual Studio, EasyX图形库(可以去官网下载)

2.VS要安装好C++模块

3.创建C++空项目

4.在头文件中新建fallball.h文件

5.在源文件中新建fallball.cpp文件


fallball.h如下所示:
#pragma once
#include 
#include 
#include 
#include 

#define WIDTH 300 //游戏窗口宽
#define HEIGHT 400 //游戏窗口高
#define RADIUS 10 //球的半径
#define GRAVITY 0.5 //重力加速度
#define ADD_BX 10 //球左右移动时的X轴增量
#define ADD_PERIOD 1000 //方块加速周期
#define ADD_RECY 0.5 //方块移速周期增量

static int counter = 1; //计时器,加速用
static float rect_vy = 0.5; //方块向上移动的速度


static float rect1_left_x = 0; //方块1左边的X坐标
static float rect1_width = 100; //方块1的宽度
static float rect1_height = 20; //方块1的高
static float rect1_top_y = HEIGHT / 10 * 1; //方块1的顶部Y坐标


static float rect2_right_x = WIDTH; //方块2右边的X坐标
static float rect2_width = 100; //方块2的宽度
static float rect2_height = 20; //方块2的高
static float rect2_top_y = HEIGHT / 10 * 4; //方块2的顶部Y坐标


static float rect3_left_x = WIDTH / 3; //方块3左边的X坐标
static float rect3_width = 100; //方块3的宽度
static float rect3_height = 20; //方块3的高
static float rect3_top_y = HEIGHT / 10 * 7; //方块3的顶部Y坐标


static float ball_x = WIDTH - rect2_width / 2; //球的初始X坐标
static float ball_y = rect2_top_y - RADIUS; //球的初始Y坐标
static float ball_vy = 0; //球的初始Y轴速度


static void checkCountInput();

static void refreshXY();

static void ctrlBallRect();

static int isEnd();

static void seeYouAgain();

static void fillCircleRect();

static void printScore();
fallball.cpp如下所示:
#include "fallball.h"

void printScore() {
	TCHAR s[20], m[15], n[20];
	char str1[] = "Game Over!";
	char str2[] = "score:";
	//将counter转换为字符串
	swprintf_s(s, _T("%d"), counter);
	swprintf_s(m, _T("%hs"), str1);
	swprintf_s(n, _T("%hs"), str2);
	//设置文字大小字体
	settextstyle(40, 0, _T("宋体"));
	//显示游戏结束
	outtextxy(WIDTH / 6, HEIGHT * 0.375, m);
	//显示得分情况
	outtextxy(WIDTH / 6, HEIGHT * 0.5, n);
	outtextxy(WIDTH / 30 * 17, HEIGHT * 0.5, s);
	while (1) {
		
	}
}

void fillCircleRect() {
	//绘制小球
	fillcircle(ball_x, ball_y, RADIUS);
	//绘制方块1
	float rect1_right = rect1_left_x + rect1_width;
	float rect1_bottom = rect1_top_y + rect1_height;
	fillrectangle(rect1_left_x, rect1_top_y, rect1_right, rect1_bottom);
	//绘制方块2
	float rect2_left = rect2_right_x - rect2_width;
	float rect2_bottom = rect2_top_y + rect2_height;
	fillrectangle(rect2_left, rect2_top_y, rect2_right_x, rect2_bottom);
	//绘制方块3
	float rect3_right = rect3_left_x + rect3_width;
	float rect3_bottom = rect3_top_y + rect3_height;
	fillrectangle(rect3_left_x, rect3_top_y, rect3_right, rect3_bottom);
}

void seeYouAgain() {
	//让方块1重新出现
	if (rect1_top_y + rect1_height <= 0) {
		rect1_top_y = HEIGHT;
		rect1_left_x = WIDTH / 60 * (0 + rand() % 11);
	}

	//让方块2重新出现
	if (rect2_top_y + rect2_height <= 0) {
		rect2_top_y = HEIGHT;
		rect2_right_x = WIDTH - WIDTH / 60 * (0 + rand() % 11);
	}

	//让方块3重新出现
	if (rect3_top_y + rect3_height <= 0) {
		rect3_top_y = HEIGHT;
		rect3_left_x = WIDTH / 60 * (16 + rand() % 9);
	}
}

int isEnd() {
	//如果小球落在地上或者达到顶部,游戏结束
	return (ball_y + RADIUS >= HEIGHT || ball_y - RADIUS <= 0) ? 1 : 0;
}

void ctrlBallRect() {
	if (ball_x - RADIUS <= 0)
		ball_x = RADIUS; //控制小球不出左界
	if (ball_x + RADIUS >= WIDTH)
		ball_x = WIDTH - RADIUS; //控制小球不出右界

	//如果小球落在在方块1上,则令其Y轴速度为0,且停在方块1上
	float ball_rect1 = (ball_y + RADIUS) - rect1_top_y;
	if ((ball_rect1 <= rect1_height) && (ball_rect1 >= 0)
		&& (ball_x - rect1_left_x <= rect1_width)
		&& (ball_x - rect1_left_x >= 0)) {
		ball_vy = 0;
		ball_y = rect1_top_y - RADIUS;
	}

	//如果小球落在在方块2上,则令其Y轴速度为0,且停在方块2上
	float ball_rect2 = (ball_y + RADIUS) - rect2_top_y;
	if ((ball_rect2 <= rect2_height) && (ball_rect2 >= 0)
		&& (rect2_right_x - ball_x <= rect2_width)
		&& (rect2_right_x - ball_x >= 0)) {
		ball_vy = 0;
		ball_y = rect2_top_y - RADIUS;
	}

	//如果小球落在在方块3上,则令其Y轴速度为0,且停在方块3上
	float ball_rect3 = (ball_y + RADIUS) - rect3_top_y;
	if ((ball_rect3 <= rect3_height) && (ball_rect3 >= 0)
		&& (ball_x <= rect3_left_x + rect3_width)
		&& (ball_x >= rect3_left_x)) {
		ball_vy = 0;
		ball_y = rect3_top_y - RADIUS;
	}

}

void refreshXY() {
	//更新方块坐标
	rect1_top_y -= rect_vy;
	rect2_top_y -= rect_vy;
	rect3_top_y -= rect_vy;

	//更新小球Y轴坐标
	ball_vy += GRAVITY;
	ball_y += ball_vy;
}

void checkCountInput() {
	//每隔一段时间方块移速加速一次
	if (counter % ADD_PERIOD == 0)
		rect_vy += ADD_RECY;

	if (_kbhit()) {
		char input = _getch(); //当按键时
		if (input == 'a' || input == 'A')
			ball_x -= ADD_BX;
		else if (input == 'd' || input == 'D')
			ball_x += ADD_BX;
	}
}

int main() {
	initgraph(WIDTH, HEIGHT); //创建游戏窗口
	//保持循环
	while (1) {
		checkCountInput(); //检测计时和按键
		refreshXY(); //更新球与方块的坐标
		ctrlBallRect(); //控制球与方块相对位置
		if (isEnd()) //判断游戏结束
			break;
		seeYouAgain(); //重复出现方块
		cleardevice(); //清除之前画面
		fillCircleRect(); //绘制小球和方块
		++counter; //计时
		Sleep(10); //暂停10ms
	}
	cleardevice(); //清屏
	printScore(); //打印结算画面
	closegraph(); //关闭游戏窗口
    return 0;
}
画面展示:

 


总结:这个小游戏是我花一个下午的时间写出来的,灵感来源于小时候诺基亚手机上的小游戏,几乎完全复刻,游戏本身也不复杂,但是玩起来还是童年的味道,玩的不亦乐乎。喜欢的小伙伴可以点赞收藏支持一波,感谢!

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/867234.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号