refactor: cleanup main loop code by moving related code into update/render functions respectively.
This commit is contained in:
parent
6cf1c9f6f7
commit
3c847094a0
1 changed files with 42 additions and 34 deletions
76
main.go
76
main.go
|
|
@ -13,12 +13,47 @@ import (
|
||||||
rl "github.com/gen2brain/raylib-go/raylib"
|
rl "github.com/gen2brain/raylib-go/raylib"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
var (
|
||||||
shape := game.NewShape(game.SHAPE_O)
|
shape = game.NewShape(game.SHAPE_O)
|
||||||
shape_pos := core.Vec2i8{X: 4, Y: 0}
|
shape_pos = core.Vec2i8{X: 4, Y: 0}
|
||||||
dropTimer := core.NewIntervalTimer(0.3)
|
dropTimer = core.NewIntervalTimer(0.3)
|
||||||
grid := game.Grid{}
|
grid = game.Grid{}
|
||||||
|
|
||||||
|
r = draw.Renderer{
|
||||||
|
Theme: &draw.Theme{
|
||||||
|
FrameBG: color.RGBA{R: 30, G: 30, B: 46, A: 255},
|
||||||
|
FrameBorder: color.RGBA{R: 242, G: 205, B: 205, A: 255},
|
||||||
|
TextHeader: color.RGBA{R: 242, G: 205, B: 205, A: 255},
|
||||||
|
Text: color.RGBA{R: 205, G: 214, B: 244, A: 255},
|
||||||
|
GridBackground: color.RGBA{R: 17, G: 17, B: 27, A: 255},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func Update(delta float32) {
|
||||||
|
if dropTimer.UpdateReset(delta) {
|
||||||
|
new_pos := shape_pos
|
||||||
|
new_pos.Y += 1
|
||||||
|
|
||||||
|
// Update position if it does not collide
|
||||||
|
if !game.CheckShapeCollision(new_pos, &shape, &grid) {
|
||||||
|
shape_pos = new_pos
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Render() {
|
||||||
|
render.Begin(r.Theme.GridBackground)
|
||||||
|
r.DrawGrid(rl.NewVector2(25, 25), grid)
|
||||||
|
draw.DrawShape(rl.NewVector2(25, 25), shape_pos, shape)
|
||||||
|
r.DrawFrame(rl.RectangleInt32{X: 400, Y: 25, Width: 250, Height: 100})
|
||||||
|
r.DrawHeaderText(410, 30, "Score")
|
||||||
|
r.DrawText(410, 65, "999999")
|
||||||
|
r.DrawFrame(rl.RectangleInt32{X: 400, Y: 150, Width: 250, Height: 200})
|
||||||
|
render.End()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
// Set random blocks to test
|
// Set random blocks to test
|
||||||
render.Init(render.Config{
|
render.Init(render.Config{
|
||||||
Title: "Tetris",
|
Title: "Tetris",
|
||||||
|
|
@ -36,35 +71,8 @@ func main() {
|
||||||
render.SetTexture(texture)
|
render.SetTexture(texture)
|
||||||
render.SetFont(&assets.Font)
|
render.SetFont(&assets.Font)
|
||||||
|
|
||||||
r := draw.Renderer{
|
|
||||||
Theme: &draw.Theme{
|
|
||||||
FrameBG: color.RGBA{R: 30, G: 30, B: 46, A: 255},
|
|
||||||
FrameBorder: color.RGBA{R: 242, G: 205, B: 205, A: 255},
|
|
||||||
TextHeader: color.RGBA{R: 242, G: 205, B: 205, A: 255},
|
|
||||||
Text: color.RGBA{R: 205, G: 214, B: 244, A: 255},
|
|
||||||
GridBackground: color.RGBA{R: 17, G: 17, B: 27, A: 255},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for !rl.WindowShouldClose() {
|
for !rl.WindowShouldClose() {
|
||||||
if dropTimer.UpdateReset(rl.GetFrameTime()) {
|
Update(rl.GetFrameTime())
|
||||||
new_pos := shape_pos
|
Render()
|
||||||
new_pos.Y += 1
|
|
||||||
|
|
||||||
// Update position if it does not collide
|
|
||||||
if !game.CheckShapeCollision(new_pos, &shape, &grid) {
|
|
||||||
shape_pos = new_pos
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
render.Begin(r.Theme.GridBackground)
|
|
||||||
r.DrawGrid(rl.NewVector2(25, 25), grid)
|
|
||||||
draw.DrawShape(rl.NewVector2(25, 25), shape_pos, shape)
|
|
||||||
r.DrawFrame(rl.RectangleInt32{X: 400, Y: 25, Width: 250, Height: 100})
|
|
||||||
r.DrawHeaderText(410, 30, "Score")
|
|
||||||
r.DrawText(410, 65, "999999")
|
|
||||||
r.DrawFrame(rl.RectangleInt32{X: 400, Y: 150, Width: 250, Height: 200})
|
|
||||||
|
|
||||||
render.End()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue