Skip to content

Instantly share code, notes, and snippets.

@krokrob
Created April 14, 2016 16:25
Show Gist options
  • Save krokrob/a985fdd0c8b98908914c6ba694f25828 to your computer and use it in GitHub Desktop.
Save krokrob/a985fdd0c8b98908914c6ba694f25828 to your computer and use it in GitHub Desktop.
class Player
attr_reader :score
def initialize
@image = Gosu::Image.new("media/xwing.png")
@x = @y = @vel_x = @vel_y = @angle = 0.0
@score = 0
end
def warp(x, y)
@x, @y = x, y
end
def turn_left
@angle -= 4.5
end
def turn_right
@angle += 4.5
end
def accelerate
@vel_x += Gosu::offset_x(@angle, 0.5)
@vel_y += Gosu::offset_y(@angle, 0.5)
end
def reverse
@vel_x += Gosu::offset_x(@angle, -0.5)
@vel_y += Gosu::offset_y(@angle, -0.5)
end
def move
@x += @vel_x
@y += @vel_y
if @x >= 640
@x = 640
elsif @x <= 0
@x = 0
end
if @y >= 480
@y = 480
elsif @y <= 0
@y = 0
end
@vel_x *= 0.95
@vel_y *= 0.95
end
def collect_stars(stars)
if stars.reject! {|star| Gosu::distance(@x, @y, star.x, star.y) < 35 } then
@score += 1
end
end
def draw
@image.draw_rot(@x, @y, 1, @angle)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment