-
Notifications
You must be signed in to change notification settings - Fork 546
/
tetris.gop
175 lines (153 loc) · 4.26 KB
/
tetris.gop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import (
"c"
"c/raylib"
)
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
}