Skip to content

Instantly share code, notes, and snippets.

@icculus
Created March 23, 2020 20:44
Show Gist options
  • Save icculus/16d42dd1dbae2cc5734ec6431536d646 to your computer and use it in GitHub Desktop.
Save icculus/16d42dd1dbae2cc5734ec6431536d646 to your computer and use it in GitHub Desktop.
Tetris in DragonRuby, code at the end of Part 1
# This is the code as it stands at the end of
# "Let's make Tetris with DragonRuby Game Toolkit!" Part 1:
# https://www.youtube.com/watch?v=xZMwRSbC4rY
$gtk.reset
class TetrisGame
def initialize args
@args = args
@next_move = 30
@score = 0
@gameover = false
@grid_w = 10
@grid_h = 20
@current_piece_x = 5
@current_piece_y = 0
@current_piece = [ [ 1, 1], [ 1, 1 ] ]
@grid = []
for x in 0..@grid_w-1 do
@grid[x] = []
for y in 0..@grid_h-1 do
@grid[x][y] = 0
end
end
end
# X AND Y ARE POSITIONS IN THE GRID, NOT PIXELS
def render_cube x, y, r, g, b, a=255
boxsize = 30
grid_x = (1280 - (@grid_w * boxsize)) / 2
grid_y = (720 - ((@grid_h-2) * boxsize)) / 2
@args.outputs.solids << [ grid_x + (x * boxsize), (720 - grid_y) - (y * boxsize), boxsize, boxsize, r, g, b, a ]
end
def render_grid
for x in 0..@grid_w-1 do
for y in 0..@grid_h-1 do
render_cube x, y, 255, 0, 0 if @grid[x][y] != 0
end
end
end
def render_grid_border
x = -1
y = -1
w = @grid_w + 2
h = @grid_h + 2
color = [ 255, 255, 255 ]
for i in x..(x+w)-1 do
render_cube i, y, *color
render_cube i, (y+h)-1, *color
end
for i in y..(y+h)-1 do
render_cube x, i, *color
render_cube (x+w)-1, i, *color
end
end
def render_background
@args.outputs.solids << [ 0, 0, 1280, 720, 0, 0, 0 ]
render_grid_border
end
def render_current_piece
for x in 0..@current_piece.length-1 do
for y in 0..@current_piece[x].length-1 do
render_cube @current_piece_x + x, @current_piece_y + y, 255, 0, 0 if @current_piece[x][y] != 0
end
end
end
def render
render_background
render_grid
render_current_piece
#render_score
end
def current_piece_colliding
for x in 0..@current_piece.length-1 do
for y in 0..@current_piece[x].length-1 do
if (@current_piece[x][y] != 0)
if (@current_piece_y + y >= @grid_h-1)
return true
elsif (@grid[@current_piece_x + x][@current_piece_y + y] != 0)
return true
end
end
end
end
return false
end
def plant_current_piece
# Make this part of the landscape
for x in 0..@current_piece.length-1 do
for y in 0..@current_piece[x].length-1 do
if @current_piece[x][y] != 0
@grid[@current_piece_x + x][@current_piece_y + y] = @current_piece[x][y]
end
end
end
@current_piece_y = 0
end
def iterate
@next_move -= 10
if @next_move <= 0 # drop the piece!
if current_piece_colliding
plant_current_piece
else
@current_piece_y += 1
end
@next_move = 30
end
end
def tick
iterate
render
end
end
def tick args
args.state.game ||= TetrisGame.new args
args.state.game.tick
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment