首页 > 其他分享 >基于EasyX和Raylib的打字母游戏

基于EasyX和Raylib的打字母游戏

时间:2023-01-26 16:12:09浏览次数:68  
标签:WHITE Raylib 字母 EasyX outtextxy letter 100 void


原版代码地址 https://codebus.cn/yangw/letters-shooting-game
基于 Raylib 实现时, 由于 Raylib 需要显式设置 FPS, getchar 这样的调用是不能用的。因此一开始的 welcome 界面需要用循环绘制并检测是否按下任意键的方式。
整体上比较简单, 可以认为是 字符雨 (仿黑客帝国) 的简化版。

基于 EasyX 的实现

////////////////////////////////////////////
// 程序名称:打字母游戏
// 编译环境:Visual C++ 6.0 / 2010,EasyX_20200902
// 程序编写:yangw80 <[email protected]>
// 发布日期:2010-8-26
//
#include <graphics.h>
#include <conio.h>
#include <time.h>

// 欢迎界面
void welcome()
{
	// 输出屏幕提示
	cleardevice();
	settextcolor(YELLOW);
	settextstyle(64, 0, _T("黑体"));
	outtextxy(160, 50, _T("打字母游戏"));
	settextcolor(WHITE);
	settextstyle(16, 0, _T("宋体"));
	outtextxy(100, 200, _T("就是很传统的那个掉字母然后按相应键就消失的游戏"));
	outtextxy(100, 240, _T("只是做了一个简单的实现"));
	outtextxy(100, 280, _T("功能并不很完善,比如生命数、分数等都没有写"));
	outtextxy(100, 320, _T("感兴趣的自己加进去吧"));

	// 实现闪烁的“按任意键继续”
	int c = 255;
	while (!_kbhit())
	{
		settextcolor(RGB(c, 0, 0));
		outtextxy(280, 400, _T("按任意键继续"));
		c -= 8;
		if (c < 50) c = 255;
		Sleep(30);
	}
	_getch();
	cleardevice();
}

// 退出界面
void goodbye()
{
	cleardevice();
	settextcolor(YELLOW);
	settextstyle(48, 0, _T("黑体"));
	outtextxy(104, 180, _T("多写程序  不老青春"));
	_getch();
}

// 主函数
int main()
{
	initgraph(640, 480);				// 初始化屏幕为 640x480

	welcome();							// 显示欢迎界面

	srand((unsigned)time(NULL));		// 设置随机种子
	settextstyle(20, 0, _T("Arial"));	// 设置字母的字体和大小
	setfillcolor(BLACK);				// 设置清除字母的填充区域颜色

	char target;						// 目标字母
	char key;							// 用户的按键
	int x, y;							// 字母的位置

	// 主循环
	while (true)
	{
		target = 65 + rand() % 26;		// 产生任意大写字母
		x = rand() % 620;				// 产生任意下落位置
		for (y = 0; y < 460; y++)
		{
			settextcolor(WHITE);		// 设置字母的颜色
			outtextxy(x, y, target);	// 显示字母

			if (_kbhit())
			{
				key = _getch();			// 获取用户按键

				if ((key == target) || (key == target + 32))
				{
					// 按键正确,“击落”字母(画黑色方块擦除)
					solidrectangle(x, y, x + 20, y + 20);
					break;				// 跳出循环,进行下一个字母
				}
				else if (key == 27)
				{
					goto EXIT;			// 如果按 ESC,退出游戏主循环
				}
			}

			// 延时,并清除字母
			Sleep(10);
			solidrectangle(x, y, x + 20, y + 20);
		}
	}

EXIT:
	// 退出部分
	goodbye();

	// 关闭图形界面
	closegraph();
	return 0;
}

基于 Raylib 的实现

#include "raylib.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define WIDTH 640
#define HEIGHT 480


class Letter
{
public:
    int y;
    int x;
    char c;

    void update()
    {
        y = y + 2;
        if (y >= HEIGHT)
        {
            reset();
        }
    }

    void reset()
    {
        y = 0;
        x = rand() % WIDTH;
        c = rand() % 26 + 65;
    }

    void draw()
    {
        char s[2] = { 0 };
        s[0] = c;
        DrawText(s, x, y, 20, WHITE);
    }
};

Letter letter;

void welcome()
{
    BeginDrawing();
    ClearBackground(BLACK);
    // settextcolor(YELLOW);
    // settextstyle(64, 0, _T("黑体"));
    // outtextxy(160, 50, _T("打字母游戏"));
    DrawText("Letter Shooting", 100, 50, 64, YELLOW);

    // settextcolor(WHITE);
    // settextstyle(16, 0, _T("宋体"));
    // outtextxy(100, 200, _T("就是很传统的那个掉字母然后按相应键就消失的游戏"));
    // outtextxy(100, 240, _T("只是做了一个简单的实现"));
    // outtextxy(100, 280, _T("功能并不很完善,比如生命数、分数等都没有写"));
    // outtextxy(100, 320, _T("感兴趣的自己加进去吧"));
    DrawText("Press correspoinding letter to shoot the falling letter", 100, 200, 16, WHITE);
    DrawText("This is a very simple implmentation", 100, 240, 16, WHITE);
    DrawText("Please add features you wished to it", 100, 280, 16, WHITE);


    DrawText("Press any key to start", 200, 400, 16, WHITE);
    //getchar();

    EndDrawing();
}

void startup()
{
    InitWindow(WIDTH, HEIGHT, "Letters Shooting Game");
    SetTargetFPS(60);
    srand((unsigned)time(NULL));

    letter.reset();
}

void update()
{
    char c = GetCharPressed();
    if (c == letter.c || c == letter.c + 32)
    {
        letter.reset();
    }
    else
    {
        letter.update();
    }
}

void show()
{
    BeginDrawing();
    {
        ClearBackground(BLACK);
        letter.draw();
    }
    EndDrawing();
}

int main()
{
    startup();
    while (!GetKeyPressed())
    {
        welcome();
    }
    
    while (!WindowShouldClose())
    {
        update();
        show();
    }
    CloseWindow();

    return 0;
}

标签:WHITE,Raylib,字母,EasyX,outtextxy,letter,100,void
From: https://www.cnblogs.com/zjutzz/p/17067876.html

相关文章

  • 基于EasyX和Raylib的鼠标操作
    EasyX提供的鼠标操作需要结合WindowsAPI使用,比较简陋。官方示例用法是在//https://codebus.cn/yangw/mouse-operation。Raylib可以提供强大的多的鼠标操作。不过......
  • 基于EasyX和Raylib的星空
    基于EasyX//程序名称:星空//编译环境:VisualC++6.0,EasyX_20200902//最后更新:2009-2-22//#include<graphics.h>#include<time.h>#include<conio.h>#define......
  • 【算法训练营day25】LeetCode216. 组合总和III LeetCode17. 电话号码的字母组合
    LeetCode216.组合总和III题目链接:216.组合总和III独上高楼,望尽天涯继续复健,一直在犯小的语法错误。慕然回首,灯火阑珊和昨天的题很像,主要区别在于递归返回条件和回溯......
  • C语言字符串首字母大写
    #include<stdio.h>#include<string.h>main(){charch[100];inti=0,n=0;gets(ch);while(ch[i]!='\0'){if(i==0){......
  • 基于EasyX和Raylib的字符阵
    字符阵是EasyX的经典样例程序:https://codebus.cn/yangw/character-matrix使用raylib替代easyx.除了常规的API替换,还需要额外调用SwapScreenBuffer().由于Dr......
  • 基于EasyX和Raylib的自由落体小球
    这个简陋的小游戏,在《C和C++游戏趣味编程》第三章,是逐次迭代写成的。这里贴出基于easyx和raylib的各自实现。基于EasyX//根据《C和C++游戏趣味编程》第二章仿......
  • 基于EasyX和Raylib的十字消除
    基于EasyX//根据《C和C++游戏趣味编程》第10章十字消除写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>#include<stdlib.h>#inc......
  • 基于EasyX和Raylib的别碰方块
    基于EasyX//根据《C和C++游戏趣味编程》第三章别碰方块写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>//检测按下了空格键voi......
  • 基于EasyX和Raylib的推箱子
    基于EasyX//根据《C和C++游戏趣味编程》第九章推箱子写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>#include<stdlib.h>//玩......
  • 基于EasyX和Raylib的坚持100秒
    EasyX//根据《C和C++游戏趣味编程》第12章坚持100秒写出#include<graphics.h>#include<conio.h>//_kbhit()#include<stdio.h>#include<stdlib.h>#include......