gosu 0.7.3-universal-darwin8.0 → 0.7.4-universal-darwin8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/examples/CptnRuby.rb +218 -0
  2. data/lib/gosu.bundle +0 -0
  3. metadata +3 -2
@@ -0,0 +1,218 @@
1
+ # Basically, the tutorial game taken to a jump'n'run perspective.
2
+
3
+ # Shows how to
4
+ # * implement jumping/gravity
5
+ # * implement scrolling
6
+ # * implement a simple tile-based map
7
+ # * load levels from primitive text files
8
+
9
+ # Some exercises, starting at the real basics:
10
+ # 0) understand the existing code!
11
+ # As shown in the tutorial:
12
+ # 1) change it use Gosu's Z-ordering
13
+ # 2) add gamepad support
14
+ # 3) add a score as in the tutorial game
15
+ # 4) similarly, add sound effects for various events
16
+ # Exploring this game's code and Gosu:
17
+ # 5) make the player wider, so he doesn't fall off edges as easily
18
+ # 6) add background music (check if playing in Window#update to implement
19
+ # looping)
20
+ # Getting tricky:
21
+ # 7) optimize Map#draw so only tiles on screen are drawn (needs modulo, a pen
22
+ # and paper to figure out)
23
+ # 8) add loading of next level when all gems are collected
24
+ # Enemies, a more sophisticated object system, weapons, title and credits
25
+ # screens...
26
+
27
+ require 'gosu'
28
+ include Gosu
29
+
30
+ module Tiles
31
+ Grass = 0
32
+ Earth = 1
33
+ end
34
+
35
+ # Collectible item.
36
+ class Gem
37
+ attr_reader :x, :y
38
+
39
+ def initialize(image, x, y)
40
+ @image = image
41
+ @x, @y = x, y
42
+ end
43
+
44
+ def draw(screen_x, screen_y)
45
+ # Draw, slowly rotating
46
+ @image.draw_rot(@x - screen_x, @y - screen_y, 0,
47
+ 25 * Math.sin(milliseconds / 133.7))
48
+ end
49
+ end
50
+
51
+ # Player class.
52
+ class CptnRuby
53
+ attr_reader :x, :y
54
+
55
+ def initialize(window, x, y)
56
+ @x, @y = x, y
57
+ @dir = :left
58
+ @vy = 0 # Vertical velocity
59
+ @map = window.map
60
+ # Load all animation frames
61
+ @standing, @walk1, @walk2, @jump =
62
+ *Image.load_tiles(window, "media/CptnRuby.png", 50, 50, false)
63
+ # This always points to the frame that is currently drawn.
64
+ # This is set in update, and used in draw.
65
+ @cur_image = @standing
66
+ end
67
+
68
+ def draw(screen_x, screen_y)
69
+ # Flip vertically when facing to the left.
70
+ if @dir == :left then
71
+ offs_x = -25
72
+ factor = 1.0
73
+ else
74
+ offs_x = 25
75
+ factor = -1.0
76
+ end
77
+ @cur_image.draw(@x - screen_x + offs_x, @y - screen_y - 49, 0, factor, 1.0)
78
+ end
79
+
80
+ # Could the object be placed at x + offs_x/y + offs_y without being stuck?
81
+ def would_fit(offs_x, offs_y)
82
+ # Check at the center/top and center/bottom for map collisions
83
+ not @map.solid?(@x + offs_x, @y + offs_y) and
84
+ not @map.solid?(@x + offs_x, @y + offs_y - 45)
85
+ end
86
+
87
+ def update(move_x)
88
+ # Select image depending on action
89
+ if (move_x == 0)
90
+ @cur_image = @standing
91
+ else
92
+ @cur_image = (milliseconds / 175 % 2 == 0) ? @walk1 : @walk2
93
+ end
94
+ if (@vy < 0)
95
+ @cur_image = @jump
96
+ end
97
+
98
+ # Directional walking, horizontal movement
99
+ if move_x > 0 then
100
+ @dir = :right
101
+ move_x.times { if would_fit(1, 0) then @x += 1 end }
102
+ end
103
+ if move_x < 0 then
104
+ @dir = :left
105
+ (-move_x).times { if would_fit(-1, 0) then @x -= 1 end }
106
+ end
107
+
108
+ # Acceleration/gravity
109
+ # By adding 1 each frame, and (ideally) adding vy to y, the player's
110
+ # jumping curve will be the parabole we want it to be.
111
+ @vy += 1
112
+ # Vertical movement
113
+ if @vy > 0 then
114
+ @vy.times { if would_fit(0, 1) then @y += 1 else @vy = 0 end }
115
+ end
116
+ if @vy < 0 then
117
+ (-@vy).times { if would_fit(0, -1) then @y -= 1 else @vy = 0 end }
118
+ end
119
+ end
120
+
121
+ def try_to_jump
122
+ if @map.solid?(@x, @y + 1) then
123
+ @vy = -20
124
+ end
125
+ end
126
+
127
+ def collect_gems(gems)
128
+ # Same as in the tutorial game.
129
+ gems.reject! do |c|
130
+ (c.x - @x).abs < 50 and (c.y - @y).abs < 50
131
+ end
132
+ end
133
+ end
134
+
135
+ # Map class holds and draws tiles and gems.
136
+ class Map
137
+ attr_reader :width, :height, :gems
138
+
139
+ def initialize(window, filename)
140
+ @tileset = Image.load_tiles(window, "media/CptnRuby Tileset.bmp", 50, 50, true)
141
+ @sky = Image.new(window, "media/Space.png", true)
142
+
143
+ gem_img = Image.new(window, "media/CptnRuby Gem.png", false)
144
+ @gems = []
145
+
146
+ lines = File.readlines(filename).map { |line| line.chop }
147
+ @height = lines.size
148
+ @width = lines[0].size
149
+ @tiles = Array.new(@width) do |x|
150
+ Array.new(@height) do |y|
151
+ case lines[y][x, 1]
152
+ when '"'
153
+ Tiles::Grass
154
+ when '#'
155
+ Tiles::Earth
156
+ when 'x'
157
+ @gems.push(Gem.new(gem_img, x * 50 + 25, y * 50 + 25))
158
+ nil
159
+ else
160
+ nil
161
+ end
162
+ end
163
+ end
164
+ end
165
+
166
+ def draw(screen_x, screen_y)
167
+ # Very primitive drawing function:
168
+ # Draws all the tiles, some off-screen, some on-screen.
169
+ @sky.draw(0, 0, 0)
170
+ @height.times do |y|
171
+ @width.times do |x|
172
+ tile = @tiles[x][y]
173
+ if tile
174
+ @tileset[tile].draw(x * 50 - screen_x, y * 50 - screen_y, 0)
175
+ end
176
+ end
177
+ end
178
+ @gems.each { |c| c.draw(screen_x, screen_y) }
179
+ end
180
+
181
+ # Solid at a given pixel position?
182
+ def solid?(x, y)
183
+ y < 0 || @tiles[x / 50][y / 50]
184
+ end
185
+ end
186
+
187
+ class Game < Window
188
+ attr_reader :map
189
+
190
+ def initialize
191
+ super(640, 480, false, 20)
192
+ self.caption = "Cptn. Ruby"
193
+ @map = Map.new(self, "media/CptnRuby Map.txt")
194
+ @cptn = CptnRuby.new(self, 400, 100)
195
+ # Scrolling is stored as the position of the top left corner of the screen.
196
+ @screen_x = @screen_y = 0
197
+ end
198
+ def update
199
+ move_x = 0
200
+ move_x -= 5 if button_down? Button::KbLeft
201
+ move_x += 5 if button_down? Button::KbRight
202
+ @cptn.update(move_x)
203
+ @cptn.collect_gems(@map.gems)
204
+ # Scrolling follows player
205
+ @screen_x = [[@cptn.x - 320, 0].max, @map.width * 50 - 640].min
206
+ @screen_y = [[@cptn.y - 240, 0].max, @map.height * 50 - 480].min
207
+ end
208
+ def draw
209
+ @map.draw @screen_x, @screen_y
210
+ @cptn.draw @screen_x, @screen_y
211
+ end
212
+ def button_down(id)
213
+ if id == Button::KbUp then @cptn.try_to_jump end
214
+ if id == Button::KbEscape then close end
215
+ end
216
+ end
217
+
218
+ Game.new.show
Binary file
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: gosu
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.7.3
7
- date: 2007-08-10 00:00:00 +02:00
6
+ version: 0.7.4
7
+ date: 2007-08-15 00:00:00 +02:00
8
8
  summary: 2D game development library.
9
9
  require_paths:
10
10
  - lib
@@ -42,6 +42,7 @@ files:
42
42
  - examples/media/Star.png
43
43
  - examples/media/Starfighter.bmp
44
44
  - examples/Tutorial.rb
45
+ - examples/CptnRuby.rb
45
46
  - lib/gosu.bundle
46
47
  - README
47
48
  - LICENSE