1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

examples: tetris: draw score

This commit is contained in:
Nicolas Sauzede 2019-08-09 07:28:37 +02:00 committed by Alexander Medvednikov
parent 796b9dab74
commit efa540e883
2 changed files with 52 additions and 20 deletions

View File

@ -9,6 +9,7 @@ import gl
import gg
import glfw
import math
import freetype
const (
BlockSize = 20 // pixels
@ -80,6 +81,8 @@ struct Block {
struct Game {
mut:
// Score of the current game
score int
// Position of the current tetro
pos_x int
pos_y int
@ -100,6 +103,10 @@ struct Game {
rotation_idx int
// gg context for drawing
gg *gg.GG
// ft context for font drawing
ft *freetype.Context
// gx text config for font drawing
tcfg *gx.TextCfg
}
fn main() {
@ -113,12 +120,29 @@ fn main() {
window_title: 'V Tetris'
window_user_ptr: game
})
ft: 0
tcfg: 0
}
game.gg.window.set_user_ptr(game) // TODO remove this when `window_user_ptr:` works
game.init_game()
game.gg.window.onkeydown(key_down)
go game.run() // Run the game loop in a new thread
gg.clear(gx.White)
// Try to load font
game.ft = freetype.new_context(gg.Cfg{
width: WinWidth
height: WinHeight
use_ortho: true
font_size: 18
}, 1)
if game.ft != 0 {
// if font loaded, define default font color etc..
game.tcfg = &gx.TextCfg{
align:gx.ALIGN_LEFT
size:12
color:gx.rgb(0, 0, 170)
}
}
for {
gg.clear(gx.White)
game.draw_scene()
@ -148,6 +172,7 @@ fn (g mut Game) init_game() {
first_row[j] = - 1
last_row[j] = - 1
}
g.score = 0
}
fn (g mut Game) parse_tetros() {
@ -222,6 +247,7 @@ fn (g mut Game) delete_completed_line(y int) {
return
}
}
g.score += 10
// Move everything down by 1 position
for yy := y - 1; yy >= 1; yy-- {
for x := 1; x <= FieldWidth; x++ {
@ -282,9 +308,16 @@ fn (g &Game) draw_field() {
}
}
fn (g &Game) draw_score() {
if g.tcfg != 0 {
g.ft.draw_text(1, 2, 'score: ' + g.score.str(), g.tcfg)
}
}
fn (g &Game) draw_scene() {
g.draw_tetro()
g.draw_field()
g.draw_score()
}
fn parse_binary_tetro(t_ int) []Block {
@ -348,4 +381,3 @@ fn key_down(wnd voidptr, key, code, action, mods int) {
game.move_tetro() // drop faster when the player presses <down>
}
}

View File

@ -160,7 +160,7 @@ pub fn new_context(cfg gg.Cfg, scale int) *Context {
}
if !os.file_exists(font_path) {
println('failed to load RobotoMono-Regular.ttf')
exit(1)
return 0
}
# FT_Face face;
# if (FT_New_Face(ft, font_path.str, 0, &face))