Skip to content

Instantly share code, notes, and snippets.

@mariovisic
Created September 28, 2022 21:55
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 mariovisic/d571fd883ae3545cc20b75dc15ba0ff4 to your computer and use it in GitHub Desktop.
Save mariovisic/d571fd883ae3545cc20b75dc15ba0ff4 to your computer and use it in GitHub Desktop.
Frogger game in Ruby 2D
require 'ruby2d'
GRID_SIZE = 40
# Set the window size
set width: GRID_SIZE * 12
set height: GRID_SIZE * 9
# Create a new shape
frogger = Square.new(
x: GRID_SIZE * rand(12), y: GRID_SIZE * 8,
size: GRID_SIZE,
color: 'olive',
z: 1
)
boats = []
trucks = []
game_state = :in_progress
Rectangle.new(x: 0, y: GRID_SIZE * 7, width: GRID_SIZE * 12, height: GRID_SIZE, color: 'blue')
Rectangle.new(x: 0, y: GRID_SIZE * 5, width: GRID_SIZE * 12, height: GRID_SIZE, color: 'gray')
on :key_down do |event|
if game_state == :in_progress
case event.key
when 'left' then frogger.x -= GRID_SIZE
when 'right' then frogger.x += GRID_SIZE
when 'up' then frogger.y -= GRID_SIZE
when 'down' then frogger.y += GRID_SIZE
end
end
end
update do
if game_state == :lost
@text ||= Text.new("whamp whamp whamp", x: 0, y: Window.height / 2, size: 72, color: 'red')
Window.set(background: 'white')
elsif game_state == :won
@text ||= Text.new("🎉 !!! Home FREE !!! 🎉", x: 0, y: Window.height / 2, size: 72, color: 'random')
Window.set(background: 'white')
else
if Window.frames % 180 == 1
boats.push(Rectangle.new(x: -GRID_SIZE * 2, y: GRID_SIZE * 7, width: GRID_SIZE * 2, height: GRID_SIZE, color: 'red'))
end
if Window.frames % 240 == 1
trucks.push(Rectangle.new(x: Window.width, y: GRID_SIZE * 5, width: GRID_SIZE * 3, height: GRID_SIZE, color: 'purple'))
end
boats.each { |boat| boat.x += 1}
trucks.each { |truck| truck.x -= 0.8}
if (boats + trucks).any? { |vehicle| vehicle.contains?(frogger.x + (frogger.width / 2), frogger.y + (frogger.height / 2)) }
game_state = :lost
elsif frogger.y == 0
game_state = :won
end
end
end
# Show the window
show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment