跳到主要內容

[Code Daily] 2020/06/18 單字記憶遊戲(一)

今天要開始實作一個小遊戲:單字記憶遊戲。
遊戲的規則,就是有許多蓋著的牌,每張牌有一個英文單字,或者某個英文單字的中文翻譯。
玩家僅能翻開牌觀看單字幾秒,牌會自動蓋上。必須將單字牌跟對應的翻譯牌在短時間內同時掀開,才能得到分數。

先從最簡單的功能開始實作:如何用滑鼠觸發事件,把牌掀開,並顯示單字。
程式語言採用 C++ 搭配 SDL2。
工作環境:elementary OS.

1.    首先我想要在某個位置 rendering true type font,這就需要安裝 SDL_ttf.h:

sudo apt-get install libsdl2-ttf-dev
g++ compile 時記得要 link: -lSDL2_ttf

2.    到 fontsquirrel 可以下載免費的 ttf 字型。我選擇的字型是 "Raleway"。

接下來分成三個步驟:
  1. initialized SDL_Renderer。
  2. 讀 Texture 圖檔,讀 ttf 檔案。把所有牌的可能圖案都先畫到一個 SDL_Texture 內。
  3. 滑鼠觸發事件處理。繪圖。
重點在第三個步驟
滑鼠觸發了某個按鈕,按鈕物件就會更新自己的狀態,例如將狀態更新為 Botton UP/Down,然後根據這個狀態值,去將 SDL_Texture 裁切出要顯示的部份,把切下來的部份 render 到一個 SDL_Renderer 物件上。

明天繼續完成細節的部份。

留言

這個網誌中的熱門文章

[Linux] Elementary OS 字體調校

用 gesetting 取得 elementary os 的等寬字體(這也是終端機默認字體): gsettings get org.gnome.desktop.interface monospace-font-name 會顯示目前字型跟字體大小: Roboto Mono 10 設定字體大小: gsettings set org.gnome.desktop.interface monospace-font-name 'Roboto Mono 12' 可以微調 text-scaling-factor: gsettings set org.gnome.desktop.interface text-scaling-factor <value>

[Vim] 我的 .vimrc

我用過的文字編輯器有 sublime text/atom/notepad++/gedit 等等。 後來我在 window 和 Linux 寫程式還是比較慣用 vim。 以下整理我常用的 .vimrc:  set nu "show line number. set rnu "related line number. nmap ; : "replace ';' with ':' in normal mode. set ai "auto indent set mouse=a "activate mouse set expandtab "replace tab with space. set tabstop=4 "replace tab with 4 spaces. set bg=dark "background set to dark. color desert "color scheme. syntax on "enable syntax highlight. " auto complete: inoremap ( ()<Esc>i inoremap ' ''<Esc>i inoremap " ""<Esc>i inoremap [ []<Esc>i inoremap { {}<Esc>i set cursorline " active cursorline 到此 vim 搭配 Ctrl + N 當自動補全,已經很好用了,之後有好用設定會再補上來。

[C++] 計算程式執行時間:clock 和 time 函式

要計算一段程式碼執行時間,可以用 time 函式: 1 2 3 4 5 time_t start, end; start = time(NULL); // DO SOMETHING... end = time(NULL); cout " [Msg] total time: " difftime(end, start) " sec" endl; time 函式的精確度只到達秒,如果要精確到毫秒等級,要用 clock 函式: 1 2 3 4 5 6 clock_t start, end; start = clock(); // DO SOMETHING... end = clock(); cout ( double )(finish - start) / CLOCKS_PER_SEC endl;