Created
September 26, 2022 10:31
-
-
Save mariovisic/f2aae97cb6cf311e0d7762cbf39ca804 to your computer and use it in GitHub Desktop.
Simple Frogger game in Ruby 2d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ruby2d' | |
GRID_SIZE = 80 | |
set width: GRID_SIZE * 12 | |
set height: GRID_SIZE * 9 | |
frogger = Square.new(size: GRID_SIZE, color: 'olive', y: Window.height - GRID_SIZE, x: GRID_SIZE * 4) | |
trucks = []; boats = []; motorbikes = []; bicycles = [] | |
game_state = :in_progress | |
on :key_down do |event| | |
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 | |
update do | |
if game_state == :lost | |
text ||= Text.new('YOU LOST, SORRY', x: 200, y: Window.height / 2, size: 72, color: 'red') | |
Window.set(background: 'gray') | |
elsif game_state == :won | |
text ||= Text.new('YOU WON, YAY', x: 200, y: Window.height / 2, size: 72, color: 'blue') | |
Window.set(background: 'gray') | |
else | |
if Window.frames % 110 == 0 | |
bicycles.push(Rectangle.new(width: GRID_SIZE * 1, height: GRID_SIZE, y: (GRID_SIZE * 7), x: 0 - (GRID_SIZE * 2), color: 'blue')) | |
end | |
if Window.frames % 110 == 0 | |
trucks.push(Rectangle.new(width: GRID_SIZE * 2, height: GRID_SIZE, y: (GRID_SIZE * 5), x: Window.width, color: 'purple')) | |
end | |
if Window.frames % 90 == 0 | |
boats.push(Rectangle.new(width: GRID_SIZE * 1.5, height: GRID_SIZE, y: (GRID_SIZE * 3), x: 0 - (GRID_SIZE * 2), color: 'orange')) | |
end | |
if Window.frames % 60 == 0 | |
motorbikes.push(Rectangle.new(width: GRID_SIZE * 0.8, height: GRID_SIZE, y: (GRID_SIZE * 1), x: Window.width, color: 'red')) | |
end | |
bicycles.each { |bicycle| bicycle.x += 2 } | |
trucks.each { |truck| truck.x -= 3 } | |
boats.each { |boat| boat.x += 4 } | |
motorbikes.each { |motorbike| motorbike.x -= 2 } | |
if (trucks + boats + motorbikes + bicycles).any? { |truck| truck.contains?(frogger.x + (frogger.width / 2), frogger.y + (frogger.height / 2))} | |
game_state = :lost | |
end | |
if frogger.y == 0 | |
game_state = :won | |
end | |
end | |
end | |
show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment