-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1906 from xushiwei/q
llgo demo: tetris
- Loading branch information
Showing
3 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |