Skip to content

Commit 3aba9b4

Browse files
committed
First commit
0 parents  commit 3aba9b4

18 files changed

+2714
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.nelua text eol=lf linguist-language=lua

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Tetris Clone in Nelua
2+
3+
![Screenshot](https://raw.githubusercontent.com/edubart/nelua-tetris/master/screenshot.gif)
4+
5+
Play in your browser at https://edubart.github.io/nelua-tetris/
6+
7+
This a clone of the
8+
[tetris game](https://pt.wikipedia.org/wiki/Tetris) using
9+
[Nelua](https://github.com/edubart/nelua-lang) and
10+
[Raylib](https://www.raylib.com/).
11+
12+
Raylib bindings from [raylib-nelua](https://github.com/Andre-LA/raylib-nelua-mirror) are used.
13+
14+
## Running
15+
16+
You must have both Nelua and latest Raylib installed and working. At the moment
17+
only Linux system is supported.
18+
19+
To run simple do `nelua tetris.nelua`

assets/hit.wav

9.91 KB
Binary file not shown.

assets/levelup.wav

30.2 KB
Binary file not shown.

assets/lineclear.wav

23.5 KB
Binary file not shown.

assets/soundtrack.ogg

1.09 MB
Binary file not shown.

board.nelua

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
require 'config'
2+
require 'raylib'
3+
require 'piece'
4+
5+
global Board = @record{
6+
cells: Color[HORZ_CELLS][VERT_CELLS]
7+
}
8+
9+
function Board:piece_collides(piece: Piece*)
10+
for iy=0,piece.size-1 do
11+
for ix=0,piece.size-1 do
12+
local tx, ty = piece.x+ix, piece.y+iy
13+
if piece.layout[iy][ix] ~= 0 then
14+
if tx < 0 or tx >= HORZ_CELLS or ty < 0 or ty >= VERT_CELLS then
15+
return true
16+
end
17+
if self.cells[ty][tx].a ~= 0 then
18+
return true
19+
end
20+
end
21+
end
22+
end
23+
return false
24+
end
25+
26+
function Board:place_piece(piece: Piece*)
27+
for iy=0,piece.size-1 do
28+
for ix=0,piece.size-1 do
29+
local tx, ty = piece.x+ix, piece.y+iy
30+
if piece.layout[iy][ix] ~= 0 then
31+
self.cells[ty][tx] = piece.color
32+
end
33+
end
34+
end
35+
end
36+
37+
function Board:clear_lines()
38+
local num_clears = 0
39+
for y=0,VERT_CELLS-1 do
40+
-- the if line y is full
41+
local full = true
42+
for x=0,HORZ_CELLS-1 do
43+
if self.cells[y][x].a == 0 then
44+
full = false
45+
break
46+
end
47+
end
48+
49+
-- slide lines down
50+
if full then
51+
num_clears = num_clears + 1
52+
for ny=y,1,-1 do
53+
for x=0,HORZ_CELLS-1 do
54+
self.cells[ny][x] = self.cells[ny-1][x]
55+
end
56+
end
57+
end
58+
end
59+
return num_clears
60+
end
61+
62+
local function draw_grid()
63+
local GRID_COLOR: Color <const> = {r=0x11,g=0x11,b=0x11,a=0xff}
64+
local GAP_COLOR: Color <const> = DARKGRAY
65+
local GAP_COLOR2: Color <const> = Raylib.Fade(DARKGRAY, 0.2)
66+
67+
Raylib.DrawRectangle(GRID_OFFSET_X, GRID_OFFSET_Y,
68+
GRID_WIDTH, GRID_HEIGHT,
69+
GRID_COLOR)
70+
for iy=0,VERT_CELLS do
71+
local x = GRID_OFFSET_X - 1
72+
local w = GRID_WIDTH + 2
73+
local y = GRID_OFFSET_Y + iy*CELL_SIZE
74+
if iy == VERT_CELLS then y = y - 1 end
75+
Raylib.DrawRectangle(x, y-1, w, 3, GAP_COLOR2)
76+
Raylib.DrawRectangle(x, y, w, 1, GAP_COLOR)
77+
end
78+
for ix=0,HORZ_CELLS do
79+
local x = GRID_OFFSET_X + ix*CELL_SIZE
80+
local y = GRID_OFFSET_Y - 1
81+
local h = GRID_HEIGHT + 2
82+
if ix == HORZ_CELLS then x = x - 1 end
83+
Raylib.DrawRectangle(x-1, y, 3, h, GAP_COLOR2)
84+
Raylib.DrawRectangle(x, y, 1, h, GAP_COLOR)
85+
end
86+
Raylib.DrawRectangleLines(GRID_OFFSET_X-1, GRID_OFFSET_Y-1,
87+
GRID_WIDTH+2, GRID_HEIGHT+2,
88+
GRID_COLOR)
89+
end
90+
91+
function Board:draw()
92+
draw_grid()
93+
for iy=0,VERT_CELLS-1 do
94+
for ix=0,HORZ_CELLS-1 do
95+
local x = GRID_OFFSET_X + ix*CELL_SIZE
96+
local y = GRID_OFFSET_Y + iy*CELL_SIZE
97+
Piece.draw_cell(x, y, self.cells[iy][ix], false)
98+
end
99+
end
100+
end
101+
102+
function Board:draw_piece(piece: Piece*, shallow: boolean)
103+
local x = GRID_OFFSET_X + piece.x*CELL_SIZE
104+
local y = GRID_OFFSET_Y + piece.y*CELL_SIZE
105+
piece:draw(x, y, shallow)
106+
end

config.nelua

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
##[[
3+
-- disable the garbage collector
4+
context.globalpragmas.nogc = true
5+
6+
local compiler = require 'nelua.ccompiler'
7+
if compiler.get_cc_info().is_emscripten then
8+
PLATFORM_WEB = true
9+
primtypes.integer = primtypes.int32
10+
primtypes.uinteger = primtypes.uint32
11+
primtypes.number = primtypes.float32
12+
cflags '-Oz -fno-plt -flto'
13+
cflags '-DGRAPHICS_API_OPENGL_ES2'
14+
cflags '-s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s TOTAL_MEMORY=16777216'
15+
cflags '-I/home/bart/apps/raylib/src /home/bart/apps/raylib/build.web/src/libraylib.bc'
16+
cflags '--preload-file assets'
17+
else
18+
linklib 'raylib'
19+
end
20+
]]
21+
22+
23+
global CELL_SIZE <comptime> = 48
24+
global VERT_CELLS <comptime> = 20
25+
global HORZ_CELLS <comptime> = 10
26+
global GRID_WIDTH <comptime> = CELL_SIZE*HORZ_CELLS
27+
global GRID_HEIGHT <comptime> = CELL_SIZE*VERT_CELLS
28+
global SCREEN_WIDTH <comptime> = CELL_SIZE*HORZ_CELLS + 320
29+
global SCREEN_HEIGHT <comptime> = CELL_SIZE*VERT_CELLS + 60
30+
global GRID_OFFSET_X <comptime> = 290
31+
global GRID_OFFSET_Y <comptime> = 30
32+
global CELL_MARGIN <comptime> = 1

0 commit comments

Comments
 (0)