Skip to content

Instantly share code, notes, and snippets.

@chadjemmett
Created April 4, 2016 21:14
Show Gist options
  • Save chadjemmett/84757eacb63fc7b56203d6efc9f14222 to your computer and use it in GitHub Desktop.
Save chadjemmett/84757eacb63fc7b56203d6efc9f14222 to your computer and use it in GitHub Desktop.
require 'gosu'
TICKS_PER_JUMP = 50
FONT_COLOR = 0xff_ffff00
class GameWindow < Gosu::Window
def initialize
super 640, 480
self.caption = "Runner"
@floor = [Floor.new(50, 400), Floor.new(350, 400), Floor.new(650, 400)]
@player = Player.new(75, 200)
@test_text = Gosu::Font.new(20)
@countdown = 0
end
def button_down(id=Gosu::KbSpace)
if id == Gosu::KbSpace and @countdown == 0
@countdown = TICKS_PER_JUMP
@player.jump
end
end
def update
@floor[0].move
@floor[1].move
@floor[2].move
@player.y += 5 unless @floor[0].solid?(@player.x, @player.y) || @floor[1].solid?(@player.x, @player.y) || @floor[2].solid?(@player.x, @player.y)
if @countdown > 0
@countdown -= 1
end
=begin
if Gosu::button_down(Gosu::KbSpace) and @countdown >= 0
@countdown = TICKS_PER_JUMP
@player.jump
end
=end
end
def draw
#player x coordinates are always the same.
#@test_text.draw("Floor X => #{@floor.x} Floor Y => #{@floor.y}", 40, 40, FONT_COLOR)
#@test_text.draw("Player X => #{@player.x} Player Y => #{@player.y}", 40, 60, FONT_COLOR)
#@test_text.draw("Floor X => #{(@floor.x .. (@floor.x + @floor.end_of_floor))} Floor Y => #{@floor.y}", 40, 80, FONT_COLOR)
#@test_text.draw("Floor X => #{(@floor.x .. (@floor.x + @floor.end_of_floor)).include?(@player.x)} Floor Y => #{@floor.y}", 40, 100, FONT_COLOR)
@test_text.draw("Countdown => #{@countdown}", 40, 100, FONT_COLOR)
@floor[0].draw
@floor[1].draw
@floor[2].draw
@player.draw
end
end
class Floor
attr_accessor :x, :y, :icon, :text, :end_of_floor
def initialize(x, y)
Gosu::Font.new(20)
@x = x
@y = y
@icon = Gosu::Font.new(20)
@text = "_________________________________________________________________________________________"
#this is the perfect length of the floor "__________________"
@end_of_floor = self.icon.text_width(self.text)
end
def draw
self.icon.draw(@text, @x, @y, FONT_COLOR)
end
def move
if @x <= -(@icon.text_width(@text))
@x = 640
else
@x -= 3
end
end
def solid?(player_x, player_y)
(@x ..(@x + @end_of_floor)).include?(player_x) && player_y == @y
end
end
class Player
attr_accessor :x, :y
def initialize(x, y)
@icon = Gosu::Font.new(20)
@x = x
@y = y
end
def jump
@y -= 20
end
def draw
@icon.draw("@", @x, @y, FONT_COLOR)
end
end
window = GameWindow.new
window.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment