GitHub 排行榜 C 位出道:手把手教你玩轉 V 語言版的俄羅斯方塊!|CSDN 博文精選

GitHub 排行榜 C 位出道:手把手教你玩轉 V 語言版的俄羅斯方塊!|CSDN 博文精選GitHub 排行榜 C 位出道:手把手教你玩轉 V 語言版的俄羅斯方塊!|CSDN 博文精選

作者 | beyondma

本文精選自 CSDN 博客,已獲作者授權

最近 V 語言在千呼萬喚之後,終於迎來開源,並正式發佈了首個可用版本。其一經推出,便強勢登頂GitHub的榜首,引來各方熱議。目前V已經可以實現自我編譯迭代,筆者大致瞭解了一下V語言,主要有如下一些特性:

  • 快速編譯:V的CPU核心每秒編譯約120萬行代碼;V也可以調用C,編譯速度下降到≈100k行/秒/CPU;

  • 安全策略:沒有空;沒有全局變量(意味著變量都是在函數體中聲明);沒有未定義的行為;

  • 性能:和C一樣快,操作C沒有任何成本,不支持運行時反射,編譯器只有400K,整個語言及其標準庫都小於400kb;V目前是用V寫的,你可以在0.4秒內完成(到今年年底,這個數字將降至≈0.15秒);

  • 強大的圖形能力:支持在GDI+/Cocoa繪圖之上的跨平臺繪圖庫,以及一個基於OpenGL的圖形庫,以支持加載複雜的三維對象與紋理。

由於曾經做過一段時間的DIRECTX的開發,V語言對於圖形能力的特性宣傳最吸引筆者的注意。所以我到其官網及Github上學習了一下相關內容,按照編譯運行了一下俄羅斯方塊的例程,接下來向大家做一下分享。

GitHub 排行榜 C 位出道:手把手教你玩轉 V 語言版的俄羅斯方塊!|CSDN 博文精選GitHub 排行榜 C 位出道:手把手教你玩轉 V 語言版的俄羅斯方塊!|CSDN 博文精選

安裝V語言

如果只是HELLO WORLD程序是非常簡單的,只需要按照https://vlang.io官網的標準步驟來執行即可。而如果使用其圖形處理能力筆者目前只在UBANTU平臺測試成功——下面均是以UBANTU為例來說明。

首先將整個項目克隆下來,再make即可:

 git clone https://github.com/vlang/v
cd v
make

如果報curl command not found,則執行以下命令安裝curl:

sudo apt install curl

接下來再建立軟鏈接:

sudo ln -s /home/machao/v/v /usr/local/bin/v

然後運行v就能進到v語言的命令行了。而如果要運行俄羅斯方塊還需要以下這些庫的支持:

sudo apt install libglfw3 libglfw3-dev libfreetype6-dev libcurl3-dev
GitHub 排行榜 C 位出道:手把手教你玩轉 V 語言版的俄羅斯方塊!|CSDN 博文精選

運行俄羅斯方塊

接下來cd tetris,進入到俄羅斯方塊的目錄,先使用gedit tetris.v。來看一下v語言的代碼樣例,其主要部分如下:

fn main {
glfw.init//實始化類
mut game := &Game{gg: 0} // TODO
game.parse_tetros
game.init_game
mut window := glfw.create_window(glfw.WinCfg {
width: WinWidth
height: WinHeight
title: 'V Tetris'
ptr: game // glfw user pointer
})
window.make_context_current
window.onkeydown(key_down)//註冊事件
gg.init
game.gg = gg.new_context(gg.Cfg {
width: WinWidth
height: WinHeight
use_ortho: true // This is needed for 2D drawing
})
go game.run // Run the game loop in a new thread
gl.clear // For some reason this is necessary to avoid an intial flickering
gl.clear_color(255, 255, 255, 255)
for {
gl.clear
gl.clear_color(255, 255, 255, 255)
game.draw_scene
window.swap_buffers
glfw.wait_events
if window.should_close {
window.destroy
glfw.terminate
exit(0)
}
}
}

fn (g mut Game) move_right(dx int) {
// Reached left/right edge or another tetro?
for i := 0; i < TetroSize; i++ {
tetro := g.tetro[i]
y := tetro.y + g.pos_y
x := tetro.x + g.pos_x + dx
row := g.field[y]
if row[x] != 0 {
// Do not move
return
}
}
g.pos_x += dx
}

fn (g mut Game) delete_completed_lines {
for y := FieldHeight; y >= 1; y-- {
g.delete_completed_line(y)
}
}

fn (g mut Game) delete_completed_line(y int) {
for x := 1; x <= FieldWidth; x++ {
f := g.field[y]
if f[x] == 0 {
return
}
}
// Move everything down by 1 position
for yy := y - 1; yy >= 1; yy-- {
for x := 1; x <= FieldWidth; x++ {
mut a := g.field[yy + 1]
mut b := g.field[yy]
a[x] = b[x]
}
}
}

// Place a new tetro on top
fn (g mut Game) generate_tetro {
g.pos_y = 0
g.pos_x = FieldWidth / 2 - TetroSize / 2
g.tetro_idx = rand.next(BTetros.len)
g.rotation_idx = 0
g.get_tetro
}

// Get the right tetro from cache
fn (g mut Game) get_tetro {
idx := g.tetro_idx * TetroSize * TetroSize + g.rotation_idx * TetroSize
g.tetro = g.tetros_cache.slice(idx, idx + TetroSize)
}

fn (g mut Game) drop_tetro {
for i := 0; i < TetroSize; i++ {
tetro := g.tetro[i]
x := tetro.x + g.pos_x
y := tetro.y + g.pos_y
// Remember the color of each block
// TODO: g.field[y][x] = g.tetro_idx + 1
mut row := g.field[y]
row[x] = g.tetro_idx + 1
}
}

fn (g &Game) draw_tetro {
for i := 0; i < TetroSize; i++ {
tetro := g.tetro[i]
g.draw_block(g.pos_y + tetro.y, g.pos_x + tetro.x, g.tetro_idx + 1)
}
}

fn (g &Game) draw_block(i, j, color_idx int) {
g.gg.draw_rect((j - 1) * BlockSize, (i - 1) * BlockSize,
BlockSize - 1, BlockSize - 1, Colors[color_idx])
}

fn (g &Game) draw_field {
for i := 1; i < FieldHeight + 1; i++ {
for j := 1; j < FieldWidth + 1; j++ {
f := g.field[i]
if f[j] > 0 {
g.draw_block(i, j, f[j])
}
}
}
}

fn (g &Game) draw_scene {
g.draw_tetro
g.draw_field
}

fn parse_binary_tetro(t int) Block {
res := [Block{} ; 4]
mut cnt := 0
horizontal := t == 9// special case for the horizontal line
for i := 0; i <= 3; i++ {
// Get ith digit of t
p := int(math.pow(10, 3 - i))
mut digit := int(t / p)
t %= p
// Convert the digit to binary
for j := 3; j >= 0; j-- {
bin := digit % 2
digit /= 2
if bin == 1 || (horizontal && i == TetroSize - 1) {
// TODO: res[cnt].x = j
// res[cnt].y = i
mut point := &res[cnt]
point.x = j
point.y = i
cnt++
}
}
}
return res
}

// TODO: this exposes the unsafe C interface, clean up
fn key_down(wnd voidptr, key, code, action, mods int) {
if action != 2 && action != 1 {
return
}
// Fetch the game object stored in the user pointer
mut game := &Game(glfw.get_window_user_pointer(wnd))
switch key {
case glfw.KEY_ESCAPE:
glfw.set_should_close(wnd, true)
case glfw.KeyUp:
// Rotate the tetro
game.rotation_idx++
if game.rotation_idx == TetroSize {
game.rotation_idx = 0
}
game.get_tetro
if game.pos_x < 0 {
game.pos_x = 1
}
case glfw.KeyLeft:
game.move_right(-1)
case glfw.KeyRight:
game.move_right(1)
case glfw.KeyDown:
game.move_tetro // drop faster when the player presses <down>
}
}

我們看到這個程序是在這行代碼window.onkeydown(key_down)來進行事件註冊的,其渲染是在draw_scene函數進行渲染的。

使用v run tetris.v命令就能看到以下的效果了。左邊是debug窗口,右邊是程序效果:

GitHub 排行榜 C 位出道:手把手教你玩轉 V 語言版的俄羅斯方塊!|CSDN 博文精選

後面筆者還會繼續關注V語言的發展,為大家帶來第一手的教程分享。

CSDN博客原文:https://blog.csdn.net/BEYONDMA/article/details/94349691,歡迎大家入駐 CSDN 博客。

【END】

熱 文推 薦

☞微軟為何痛失移動操作系統?

☞漫畫:一文學會面試中常問的 IO 問題!

☞庫克迴應喬納森離職;微信新版本取消“語音轉文字”功能;Mac Pro生產遷至中國 | 極客頭條

☞程序員們如何破局 5G?

☞軟件為什麼會淪為遺留系統?

☞因為有了 TA,搞定行業應用開發,不怕不怕啦!

☞除了V神,17個以太坊大會講師的演講精華都在這兒了!

☞2019年技術盤點容器篇(二):聽騰訊雲講講踏入成熟期的容器技術 | 程序員硬核評測

☞50行Python代碼,獲取公眾號全部文章

☞不寫一行代碼,也能玩轉Kaggle競賽?

☞馬雲曾經偶像,終於把阿里留下的1400億敗光了!

GitHub 排行榜 C 位出道:手把手教你玩轉 V 語言版的俄羅斯方塊!|CSDN 博文精選

點擊閱讀原文,輸入關鍵詞,即可搜索您想要的 CSDN 文章。

GitHub 排行榜 C 位出道:手把手教你玩轉 V 語言版的俄羅斯方塊!|CSDN 博文精選

你點的每個“在看”,我都認真當成了喜歡

相關推薦

推薦中...