Gosu - Thư viện game 2D cho Ruby - Cùng bắt đầu với game đơn giản - Phần 3
Bài đăng này đã không được cập nhật trong 6 năm
Và phần cuối cùng của game đơn giản này mình hướng dẫn các bạn chèn âm thanh khi phi thuyền chạm vào ngôi sao và đếm số sao thu được bằng phương thức score() nhé. Các bạn down file beep.wav này và save vào thư mục media nhé.
Load file âm thanh trong gosu
Gosu cung cấp hàm Sample() giúp load file âm thanh và mình để âm thanh trong class player.rb bằng đoạn code này @beep = Gosu::Sample.new("media/beep.wav") và khi phi thuyền chạm vào sao thì đoạn âm thanh kia sẽ được chạy @beep.play
Class player.rb
class Player
    def initialize
        @image = Gosu::Image.new("media/starfighter.bmp")
        @x = @y = @vel_x = @vel_y = @angle = 0.0
        @beep = Gosu::Sample.new("media/beep.wav") #Load file beep.wav trong thư mục media
    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 move
        @x += @vel_x
        @y += @vel_y
        @x %= 640
        @y %= 480
        @vel_x *= 0.95
        @vel_y *= 0.95
    end
    def draw
        @image.draw_rot(@x, @y, 1, @angle)
    end
    def collect_stars(stars)
        if stars.reject! { |star| Gosu::distance(@x, @y, star.x, star.y) < 35} then
            @beep.play  #Sau khi phi thuyền chạm vào ngôi sao thì file âm thanh sẽ được bật
        end
    end
end
Thu thập stars
Trong class Player mình thêm phương thức score để đếm số starts thu thập được và collect_start () để định nghĩa khoảng cách tương tác giữa player và star.
class Player
    def initialize
        @image = Gosu::Image.new("media/starfighter.bmp")
        @x = @y = @vel_x = @vel_y = @angle = 0.0
        @score = 0
        @beep = Gosu::Sample.new("media/beep.wav") #Load file beep.wav trong thư mục media
    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 move
        @x += @vel_x
        @y += @vel_y
        @x %= 640
        @y %= 480
        @vel_x *= 0.95
        @vel_y *= 0.95
    end
    def draw
        @image.draw_rot(@x, @y, 1, @angle)
    end
    def score
        @score
    end
    def collect_stars(stars)
        if stars.reject! { |star| Gosu::distance(@x, @y, star.x, star.y) < 35} then
            @score += 1
            @beep.play #Sau khi phi thuyền chạm vào ngôi sao thì file âm thanh sẽ được bật
        end
    end
end
Vậy là mình đã hoàn thành series hướng dẫn làm game đơn giản bằng thư viện gosu dành cho ruby rồi  Chúc các bạn thành công \m/
Tham khảo từ : http://memorandums.hatenablog.com/entry/2016/04/21/174834
Chúc các bạn thành công \m/
Tham khảo từ : http://memorandums.hatenablog.com/entry/2016/04/21/174834
All rights reserved
 
  
 