Skip to content

Commit

Permalink
Merge pull request #1906 from xushiwei/q
Browse files Browse the repository at this point in the history
llgo demo: tetris
  • Loading branch information
xushiwei authored Jun 27, 2024
2 parents 59c2bd8 + 9d989a9 commit 4f928a6
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/goplus/gogen v1.16.0
github.com/goplus/llgo v0.8.9
github.com/goplus/llgo v0.8.10-0.20240626170513-4a28893171f4
github.com/goplus/mod v0.13.10
github.com/qiniu/x v1.13.10
golang.org/x/tools v0.22.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/goplus/gogen v1.16.0 h1:hAK2ZX8vCjH+Y2QoJl9viSZ8Gw9pzE0vCz5voYBYnv4=
github.com/goplus/gogen v1.16.0/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk=
github.com/goplus/llgo v0.8.9 h1:b6EM2z8wDBDT8UPPwMrdPFx/5IlivSkDANXlg3tVwMw=
github.com/goplus/llgo v0.8.9/go.mod h1:iboncA0EqWVi0MTJI1UARRE/b+EvAGblKujuIYBFj2Q=
github.com/goplus/llgo v0.8.10-0.20240626170513-4a28893171f4 h1:VxCEiEky4yoVBJuDSJxQ83WpsyrNT0USCZUKcnVbLB4=
github.com/goplus/llgo v0.8.10-0.20240626170513-4a28893171f4/go.mod h1:iboncA0EqWVi0MTJI1UARRE/b+EvAGblKujuIYBFj2Q=
github.com/goplus/mod v0.13.10 h1:5Om6KOvo31daN7N30kWU1vC5zhsJPM+uPbcEN/FnlzE=
github.com/goplus/mod v0.13.10/go.mod h1:HDuPZgpWiaTp3PUolFgsiX+Q77cbUWB/mikVHfYND3c=
github.com/qiniu/x v1.13.10 h1:J4Z3XugYzAq85SlyAfqlKVrbf05glMbAOh+QncsDQpE=
Expand Down
176 changes: 176 additions & 0 deletions testdata/_llgo/tetris/tetris.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import (
"c"
"c/raylib"
"unsafe"
)

const (
BOARD_WIDTH = 10
BOARD_HEIGHT = 20
BLOCK_SIZE = 30

SCREENWIDTH = 300
SCREENHEIGHT = 600
)

const MAX_BLOCKS = 4

type Shape struct {
Blocks [MAX_BLOCKS]raylib.Vector2
Color raylib.Color
}

var SHAPES = []Shape{
{Blocks: [MAX_BLOCKS]raylib.Vector2{{X: 0, Y: 0}, {X: 1, Y: 0}, {X: 2, Y: 0}, {X: 3, Y: 0}}, Color: raylib.SKYBLUE},
{Blocks: [MAX_BLOCKS]raylib.Vector2{{X: 0, Y: 0}, {X: 1, Y: 0}, {X: 0, Y: 1}, {X: 1, Y: 1}}, Color: raylib.YELLOW},
{Blocks: [MAX_BLOCKS]raylib.Vector2{{X: 1, Y: 0}, {X: 0, Y: 1}, {X: 1, Y: 1}, {X: 2, Y: 1}}, Color: raylib.PURPLE},
{Blocks: [MAX_BLOCKS]raylib.Vector2{{X: 1, Y: 0}, {X: 2, Y: 0}, {X: 0, Y: 1}, {X: 1, Y: 1}}, Color: raylib.GREEN},
{Blocks: [MAX_BLOCKS]raylib.Vector2{{X: 0, Y: 0}, {X: 1, Y: 0}, {X: 1, Y: 1}, {X: 2, Y: 1}}, Color: raylib.RED},
{Blocks: [MAX_BLOCKS]raylib.Vector2{{X: 0, Y: 0}, {X: 0, Y: 1}, {X: 1, Y: 1}, {X: 2, Y: 1}}, Color: raylib.BLUE},
{Blocks: [MAX_BLOCKS]raylib.Vector2{{X: 2, Y: 0}, {X: 0, Y: 1}, {X: 1, Y: 1}, {X: 2, Y: 1}}, Color: raylib.ORANGE},
}

var board [BOARD_HEIGHT][BOARD_WIDTH]raylib.Color

var curShape Shape
var curPos raylib.Vector2

var fallTime = c.Float(0)
var fallSpeed = c.Float(0.2)
var score = 0
var scoreText = (*c.Char)(c.malloc(20))
var gameOver = false

func genShape() {
curShape = SHAPES[raylib.getRandomValue(0, 6)]
curPos = raylib.Vector2{BOARD_WIDTH/2 - 1, 0}
}

func checkCollision() bool {
for i := 0; i < MAX_BLOCKS; i++ {
x := int(curPos.X + curShape.Blocks[i].X)
y := int(curPos.Y + curShape.Blocks[i].Y)
if x < 0 || x >= BOARD_WIDTH || y >= BOARD_HEIGHT || (y >= 0 && board[y][x] != raylib.BLANK) {
return true
}
}
return false
}

func lockShape() {
for i := 0; i < MAX_BLOCKS; i++ {
x := int(curPos.X + curShape.Blocks[i].X)
y := int(curPos.Y + curShape.Blocks[i].Y)
if y >= 0 {
board[y][x] = curShape.Color
}
}
}

func rotateShape() {
rotated := curShape
for i := 0; i < MAX_BLOCKS; i++ {
x := rotated.Blocks[i].X
rotated.Blocks[i].X = -rotated.Blocks[i].Y
rotated.Blocks[i].Y = x
}

temp := curShape
curShape = rotated
if checkCollision() {
curShape = temp
}
}

func clearLines() int {
linesCleared := 0
for y := BOARD_HEIGHT - 1; y >= 0; y-- {
lineFull := true
for x := 0; x < BOARD_WIDTH; x++ {
if board[y][x] == raylib.BLANK {
lineFull = false
break
}
}
if lineFull {
for yy := y; yy > 0; yy-- {
for x := 0; x < BOARD_WIDTH; x++ {
board[yy][x] = board[yy-1][x]
}
}
for x := 0; x < BOARD_WIDTH; x++ {
board[0][x] = raylib.BLANK
}
y += 1
linesCleared += 1
}
}
return linesCleared
}

func keyPressed(key c.Int) bool {
return raylib.isKeyPressed(key) || raylib.isKeyPressedRepeat(key)
}

raylib.initWindow SCREENWIDTH, SCREENHEIGHT, c"tetris (powered by raylib and Go+)"
raylib.setTargetFPS 60
genShape
for !raylib.windowShouldClose && !gameOver {
fallTime += raylib.getFrameTime
if fallTime >= fallSpeed {
fallTime = 0
curPos.Y += 1
if checkCollision() {
curPos.Y -= 1
lockShape
linesCleared := clearLines()
score += linesCleared * 100
genShape
if checkCollision() {
gameOver = true
}
}
}

if keyPressed(raylib.KEY_LEFT) {
curPos.X--
if checkCollision() {
curPos.X++
}
}
if keyPressed(raylib.KEY_RIGHT) {
curPos.X++
if checkCollision() {
curPos.X--
}
}
if keyPressed(raylib.KEY_SPACE) || keyPressed(raylib.KEY_UP) || keyPressed(raylib.KEY_DOWN) {
rotateShape
}

raylib.beginDrawing
raylib.clearBackground raylib.RAYWHITE
for y := 0; y < BOARD_HEIGHT; y++ {
for x := 0; x < BOARD_WIDTH; x++ {
raylib.drawRectangle c.Int(x*BLOCK_SIZE), c.Int(y*BLOCK_SIZE), c.Int(BLOCK_SIZE-1), c.Int(BLOCK_SIZE-1), board[y][x]
}
}

for i := 0; i < MAX_BLOCKS; i++ {
raylib.drawRectangle c.Int((curPos.X+curShape.Blocks[i].X)*BLOCK_SIZE), c.Int((curPos.Y+curShape.Blocks[i].Y)*BLOCK_SIZE),
BLOCK_SIZE-1, BLOCK_SIZE-1, curShape.Color
}

c.Sprintf(scoreText, c"Score:%d", score)
raylib.DrawText(scoreText, 10, 10, 20, raylib.BLACK)

raylib.EndDrawing()
}

for !raylib.windowShouldClose {
raylib.beginDrawing
raylib.clearBackground raylib.RAYWHITE
raylib.drawText c"Game Over", SCREENWIDTH/2-50, SCREENHEIGHT/2-10, 20, raylib.RED
raylib.drawText scoreText, SCREENWIDTH/2-50, SCREENHEIGHT/2+10, 20, raylib.BLACK
raylib.endDrawing
}

0 comments on commit 4f928a6

Please sign in to comment.