基於EasyX和Raylib的打字母遊戲


原版代碼地址 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章