-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplatformer.rb
executable file
·72 lines (61 loc) · 1.63 KB
/
platformer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'rubygems'
require 'gosu'
require 'hunter'
require 'power_up'
include Gosu
require 'player'
require 'map'
module Tiles
Grass = 0
Earth = 1
end
class Platformer < Gosu::Window
attr_reader :map
def initialize
super(640, 480, false)
self.caption = "Platformer"
@sky = Image.new(self, "images/ninja-skull2.png", true)
@map = Map.new(self, "map.txt")
@player = Player.new(self, 400, 100)
# The scrolling position is stored as top left corner of the screen.
@camera_x = @camera_y = 0
@hunters = []
@start_time = Time.now
@font = Gosu::Font.new(self, Gosu::default_font_name, 60)
@score = 0
@power_up = PowerUp.new(self)
end
def update
@score = @score + 1
if Time.now > (@start_time+3)
@start_time = Time.now
@hunters << Hunter.new(self, @player)
end
move_x = 0
move_x -= 5 if button_down? KbLeft
move_x += 5 if button_down? KbRight
@player.update(move_x) unless @player.touch?(@hunters)
@hunters.each {|hunter| hunter.chase}
# Scrolling follows player
@camera_x = [[@player.x - 320, 0].max, @map.width * 50 - 640].min
@camera_y = [[@player.y - 240, 0].max, @map.height * 50 - 480].min
end
def draw
@power_up.draw
@font.draw("The score is: #{@score}", 20,20,5)
@sky.draw 0, 0, 0
translate(-@camera_x, -@camera_y) do
@map.draw
@player.draw
@hunters.each {|hunter| hunter.draw}
end
end
def button_down(id)
if id == KbUp then @player.try_to_jump end
if id == KbEscape then close end
end
end
game = Platformer.new
game.show