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

游戏简介:

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

提前准备:

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

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

3.创建C++空项目

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

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


fallball.h如下所示:

/*
** 球与地下城游戏接口
*/
#pragma once
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

#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; //方块向上移动的速度

/*
** 方块1位置信息
*/
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坐标

/*
** 方块2位置信息
*/
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坐标

/*
** 方块3位置信息
*/
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();
/*
** 更新方块坐标,小球Y轴坐标
*/
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;
}

画面展示:

 


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

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

New_Teen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值