Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Created August 17, 2019 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/91299aa505942564b12e046cf440bce8 to your computer and use it in GitHub Desktop.
Save amirrajan/91299aa505942564b12e046cf440bce8 to your computer and use it in GitHub Desktop.
# https://twitter.com/hashtag/LOWREZJAM
###################################################################################
# YOUR GAME GOES HERE
###################################################################################
def lowrez_tick args, lowrez_sprites, lowrez_labels, lowrez_borders, lowrez_solids, lowrez_mouse
# args.state.show_gridlines = true
if args.state.you_win
lowrez_tick_you_win args, lowrez_labels
else
lowrez_move_bullets args
lowrez_move_red_ship args
lowrez_move_blue_ship args
lowrez_render_game args, lowrez_sprites
end
end
def lowrez_tick_you_win args, lowrez_labels
lowrez_labels << [10, 30, "You win!!", 255, 255, 255]
lowrez_labels << [4, 24, "Press Enter", 255, 255, 255]
if args.keyboard.key_down.enter
args.state.you_win = false
args.state.bullets.clear
args.state.blue_ship_location = nil
args.state.red_ship_location = nil
end
end
def lowrez_move_bullets args
args.state.bullets ||= []
args.state.bullets.map do |b|
b.y += 1
end
args.state.bullets = args.state.bullets.reject { |b| b.y >= 64 }
bullets_hit_red_ship = args.state.bullets.any? do |b|
b_rect = [b.x, b.y, 1, 1]
ship_rect = [args.state.red_ship_location.x, args.state.red_ship_location.y, 5, 5]
b_rect.intersects_rect? ship_rect
end
if bullets_hit_red_ship == true
args.state.you_win = true
end
end
def lowrez_move_red_ship args
args.state.red_ship_location ||= [31, 58]
if args.state.red_ship_location.x.to_i <= 1
args.state.red_ship_location.x += 1
elsif args.state.red_ship_location.x.to_i >= 62
args.state.red_ship_location.x -= 1
else
args.state.red_ship_location.x += rand * 1.randomize(:sign)
end
end
def lowrez_move_blue_ship args
args.state.blue_ship_location ||= [0, 0]
if args.keyboard.right
args.state.blue_ship_location.x += 0.5
elsif args.keyboard.left
args.state.blue_ship_location.x -= 0.5
end
if args.keyboard.key_down.space
args.state.bullets << [args.state.blue_ship_location.x + 2,
args.state.blue_ship_location.y + 3]
end
end
def lowrez_render_game args, lowrez_sprites
lowrez_sprites << [args.state.blue_ship_location.x,
args.state.blue_ship_location.y,
5,
5,
'sprites/ship_blue.png']
lowrez_sprites << args.state.bullets.map do |b|
[b.x, b.y, 1, 1, 'sprites/blue_bullet.png']
end
lowrez_sprites << [args.state.red_ship_location.x,
args.state.red_ship_location.y,
5,
5,
'sprites/ship_red.png',
180]
end
###################################################################################
# YOU CAN PLAY AROUND WITH THE CODE BELOW, BUT USE CAUTION AS THIS IS WHAT EMULATES
# THE 64x64 CANVAS.
###################################################################################
TINY_RESOLUTION = 64
TINY_SCALE = 720.fdiv(TINY_RESOLUTION)
CENTER_OFFSET = (1280 - 720).fdiv(2)
EMULATED_FONT_SIZE = 20
EMULATED_FONT_X_ZERO = 0
EMULATED_FONT_Y_ZERO = 46
def tick args
sprites = []
labels = []
borders = []
solids = []
mouse = emulate_lowrez_mouse args
args.state.show_gridlines = false
lowrez_tick args, sprites, labels, borders, solids, mouse
render_gridlines_if_needed args
render_mouse_crosshairs args, mouse
emulate_lowrez_scene args, sprites, labels, borders, solids, mouse
end
def emulate_lowrez_mouse args
args.state.new_entity_strict(:lowrez_mouse) do |m|
m.x = args.mouse.x.idiv(TINY_SCALE) - CENTER_OFFSET.idiv(TINY_SCALE) - 1
m.y = args.mouse.y.idiv(TINY_SCALE)
if args.mouse.click
m.click = [
args.mouse.click.point.x.idiv(TINY_SCALE) - CENTER_OFFSET.idiv(TINY_SCALE) - 1,
args.mouse.click.point.y.idiv(TINY_SCALE)
]
m.down = m.click
else
m.click = nil
m.down = nil
end
if args.mouse.up
m.up = [
args.mouse.up.point.x.idiv(TINY_SCALE) - CENTER_OFFSET.idiv(TINY_SCALE) - 1,
args.mouse.up.point.y.idiv(TINY_SCALE)
]
else
m.up = nil
end
end
end
def render_mouse_crosshairs args, mouse
return unless args.state.show_gridlines
args.labels << [10, 25, "mouse: #{mouse.x} #{mouse.y}", 255, 255, 255]
end
def emulate_lowrez_scene args, sprites, labels, borders, solids, mouse
args.render_target(:lowrez).sprites << sprites
args.outputs.labels << labels.map do |l|
as_label = l.label
l.text.each_char.each_with_index.map do |char, i|
[CENTER_OFFSET + EMULATED_FONT_X_ZERO + (as_label.x * TINY_SCALE) + i * 5 * TINY_SCALE,
EMULATED_FONT_Y_ZERO + (as_label.y * TINY_SCALE), char,
EMULATED_FONT_SIZE, 0, as_label.r, as_label.g, as_label.b, as_label.a, 'dragonruby-gtk-4x4.ttf']
end
end
args.render_target(:lowrez).solids << [0, 0, 1280, 720]
args.sprites << [CENTER_OFFSET, 0, 1280 * TINY_SCALE, 720 * TINY_SCALE, :lowrez]
args.primitives << [0, 0, CENTER_OFFSET, 720].solid
args.primitives << [1280 - CENTER_OFFSET, 0, CENTER_OFFSET, 720].solid
args.primitives << [0, 0, 1280, 2].solid
end
def render_gridlines_if_needed args
if args.state.show_gridlines && args.static_lines.length == 0
args.static_lines << 65.times.map do |i|
[
[CENTER_OFFSET + i * TINY_SCALE + 1, 0,
CENTER_OFFSET + i * TINY_SCALE + 1, 720, 128, 128, 128],
[CENTER_OFFSET + i * TINY_SCALE, 0,
CENTER_OFFSET + i * TINY_SCALE, 720, 128, 128, 128],
[CENTER_OFFSET, 0 + i * TINY_SCALE,
CENTER_OFFSET + 720, 0 + i * TINY_SCALE, 128, 128, 128],
[CENTER_OFFSET, 1 + i * TINY_SCALE,
CENTER_OFFSET + 720, 1 + i * TINY_SCALE, 128, 128, 128]
]
end
elsif !args.state.show_gridlines
args.static_lines.clear
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment