Im sitting here with what I think is a very simple bug I just can not figure out.. Im trying to learn how to make games with Gosu gem with Ruby, but have hit a speed bump. Here is my code.
require "gosu"
class Hello < Gosu::Window
def initialize width = 800, height = 600, fullscreen = false
super
self.caption = "Ruby Practise"
#image = Gosu::Image.from_text self. "My text to print".
Gosu.default_font_name.
100
end
def button_down id
close if id == Gosu::KbEscape
end
def update
end
def draw
#image.draw 0, 0, 0
end
end
Hello.new.show
There is something wrong but I do not know what. I have spent at least 1 hour on it.. It complains on the String, here is the output from terminal.
hello.rb:8: syntax error, unexpected tSTRING_BEG
#image = Gosu::Image.from_text self. "My text to print".
^
hello.rb:10: syntax error, unexpected tINTEGER
I do not what I am doing wrong, do some one know? It is probably something really simple..
Use commas to separate function arguments, not dots:
#image = Gosu::Image.from_text self, "My text to print",
Gosu.default_font_name,
100
Related
I was trying to make a multiple-choice music player using Gosu but the picture and music Iwanted would not initialize despite the program running, it showed a black screen. The single block of codes works:
require 'gosu'
require './input_functions'
class MW < Gosu::Window
def initialize
super 200, 135
#beth = Gosu::Image.new("media/beth.jpg")
#song = Gosu::Song.new("media/3rdmovement.mp3")
#song.play
end
def draw
#beth.draw(0, 0)
end
end
window = MW.new
window.show
But adding the multiple choice elements would not work(note: read_integer_in_range is defined in input function, the name itself is self explanatory). Full code:
require 'gosu'
require './input_functions'
class MW < Gosu::Window
def initialize
super 200, 135
#beth = Gosu::Image.new("media/beth.jpg")
#dimitri = Gosu::Image.new("media/dimitri.png")
#vil = Gosu::Image.new("media/vilva.png")
#song = Gosu::Song.new("media/3rdmovement.mp3")
#song2=Gosu::Song.new("media/2ndwaltz.mp3")
#song3=Gosu::Song.new("media/1stseason.mp3")
read_integer_in_range( "What song you want play
1st Spring
2nd Waltz
3rd movement", 1, 3)
choice = gets.chomp.to_i()
case choice
when 1
#song3.play
#vil.draw(0, 0)
when 2
#song2.play
#dimitri.draw(0, 0)
when 3
#song.play
draw_beth()
end
end
end
def draw_beth
#beth.draw(0, 0)
end
window = MW.new
window.show
All of the Png/Jpg and mp3 file works just fine..
I tried separating the draw_beth to call it in case but it did not work. I hope some passing by could help me with this one
As I can see, you are creating a music player with GUI, and if you are doing so, you shouldn't use gets function, instead you should track for the cursor's position and return a test value; for example:
def update
#locs = [mouse_x, mouse_y]
#cursor_choice_album = mouse_over_button(mouse_x, mouse_y)
end
def needs_cursor?; true; end
def mouse_over_button(mouse_x, mouse_y)
if ((mouse_x > 100 and mouse_x < 500) and (mouse_y < 500 and mouse_y > 100))
return 1
end
then you can use the case condition in the "button down ID" function
So for practice with ruby, I am creating a library on top of ruby2d, but one problem.
I keep getting an error, I think setting up a window, here is the full error:
C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/ruby2d-0.11.3/lib/ruby2d/window.rb:476:in `exist?': no implicit conversion of nil into String (TypeError)
from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/ruby2d-0.11.3/lib/ruby2d/window.rb:476:in `add_controller_mappings'
from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/ruby2d-0.11.3/lib/ruby2d/window.rb:629:in `ext_show'
from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/ruby2d-0.11.3/lib/ruby2d/window.rb:629:in `show'
from C:/Users/Admin/Desktop/RubyProjects/RSGE/rsge_window.rb:19:in `mainloop'
from c:/Users/Admin/Desktop/RubyProjects/test.rb:4:in `<main>'
it shows that something is wrong in ruby2d (a graphics library for ruby)
here is the code for my test file that uses my library
require './RSGE/rsge'
root = Window.new(800, 800)
root.mainloop
and here is my window class:
require 'ruby2d'
require_relative 'rsge_colors'
class Window
def initialize (w, h)
#width = w.to_i
#height = h.to_i
#title = 'Blank Window'
#background = $WHITE
end
def config
set width: #width, height: #height
set background: #background
set title: #title
end
def mainloop
show
end
end
if your wondering what rsge_colors is, here it is
$RED = 'red'
$GREEN = 'green'
$BLUE = 'blue'
$ORANGE = 'orange'
$PURPLE = 'purple'
$YELLOW = 'yellow'
$WHITE = 'white'
$BLACK = 'black'
Why am I getting this error, and how can I fix it?
Thank you for any help you can give me!
Defining class Window looks like you're overriding Ruby2D::Window class.
I've never used this library, but looking the code for some of the games in the showcase, and also the "Get started" section, it looks like the dsl must be used in the root level of your ruby file.
In your case, it should be something like:
require 'ruby2d'
require_relative 'rsge_colors'
set width: 800, height: 800
set background: $WHITE
set title: 'Blank Window'
show
Probably there must be a way to encapsulate the code into classes or modules, but if you're just learning to use the gem, probably would be better to start with the basic stuff then move to refactor these type of things.
I have been trying to execute the following code using the command prompt. However, when I try to run it I get the following
C:>circle
C:>
Basically, nothing happens. However, I cannot figure out why this is happening as I am still new to Gosu and Ruby in general. Any help would be much appreciated.
require "rubygems"
require "gosu"
class Circle
attr_reader :columns, :rows
def initialize(radius)
#columns = #rows = radius * 2
img = Gosu::Image.new(Circle.new(50))
img.draw(200, 200, ZOrder::TOP, 0.5, 1.0, Gosu::Color::RED)
clear, solid = 0x00.chr, 0xff.chr
lower_half = (0...radius).map do |y|
x = Math.sqrt(radius ** 2 - y ** 2).round
right_half = "#{solid * x}#{clear * (radius - x)}"
right_half.reverse + right_half
end.join
alpha_channel = lower_half.reverse + lower_half
# Expand alpha bytes into RGBA color values.
#blob = alpha_channel.gsub(/./) { |alpha| solid * 3 + alpha }
end
def to_blob
#blob
end
end
There are 2 problems:
You are running file with class definition. What you need to run if you want to look at that blob is is Circle.new(10).to_blob - where 10 is radius. You can place it in separate file and require class.rb.
initialize method will be run during Circle.new()call. And you have Circle.new called in your class:
img = Gosu::Image.new(Circle.new(50))
So it will lead to infinite recursion and will cause SystemStackError. AFAIK, there is no built-in empty image constructor in gosu (maybe I am wrong).
I'm trying to create a document with Prawn Ruby PDF generator, but I'm facing the following problem:
The image below shows what structure I'm trying to do.
And this is the example code that tries to mimics my real scenario with the way that I'm trying to achieve this. The 2.times and (50.times.map { |i| i.to_s }.join("\n")) mimics dynamic data.
require 'prawn'
class MyPdf
def self.to_pdf(*args)
new(*args).to_pdf
end
def to_pdf
pdf.move_down 200
2.times do
pdf.bounding_box(
[0, pdf.cursor],
width: pdf.bounds.width
) do
pdf.text (50.times.map { |i| i.to_s }.join("\n"))
pdf.stroke_bounds
end
end
pdf
end
def pdf
#pdf ||= Prawn::Document.new(page_size: 'A4')
end
end
But I'm having a lot of trouble with the dynamic bounding box placing.
Do you people know a way to achieve this with or without bounding boxes?
You may be looking for span:
def to_pdf
pdf.move_down 200
2.times do
pdf.span(pdf.bounds.width) do
pdf.text (50.times.map { |i| i.to_s }.join("\n"))
pdf.stroke_bounds
end
end
end
I failed to do that with boundig boxes. May be this is not the best solution, but you can do this with tables :
data = []
500.times do |i|
data.push [i.to_s]
end
table(data, width: bounds.width) do |t|
t.cells.border_width = 0 # We don't want to see a table
t.before_rendering_page do |page|
page.row(0).border_top_width = 1
page.row(-1).border_bottom_width = 1
page.column(0).border_left_width = 1
page.column(-1).border_right_width = 1
end
end
source : http://prawnpdf.org/prawn-table-manual.pdf (page 17)
Check your margins to have the continuation on the top of the next page
I've been having trouble centering text in the Gosu library to the absolute middle of the screen.
require 'gosu'
class GameWindow < Gosu::Window
def initialize (width=800, height=600, fullscreen=false)
super
self.caption = 'Hello'
#message = Gosu::Image.from_text(
self, 'HELLO WORLD', Gosu.default_font_name, 45)
end
def draw
#message.draw(377.5,277.5,0)
end
end
window = GameWindow.new
window.show
My first approach was to take the height of the screen, subtract it by the height of the text 45, and then divide by 2. Now that seemed to work when aligning vertically.
However, horizontally is a different story...It seems to
be taking the top left corner of the text and centering it which I expected it to do, instead of the middle of the text.
Anyone got a formula for this ? I tried a whole bunch of things, and only came close.
class GameWindow < Gosu::Window
def initialize (width=800, height=600, fullscreen=false)
super
self.caption = 'Hello'
#message = Gosu::Image.from_text(
self, 'HELLO WORLD', Gosu.default_font_name, 45)
end
def draw
#message.draw(377.5,277.5,0)
end
end
Your #message is an instance of Gosu::Image
As far as I can see, the class has a method that allows you to align the image's rotational center to a specified point, draw_rot
Using draw_rot instead of draw should work for you once you've found the center of the frame.
I know this is an old question, but I was having this issue earlier today and came up with this solution.
def draw_centered_text(text, size, font)
centered_text = Gosu::Image.from_text(text, size, {:width => WIDTH, :align => :center, :font => font})
end
The above function converts the passed text to an image with a width equal to WIDTH (which in my case is a constant that stores the window width) and the text centred. You can then call the function like so:
draw_centered_text("Your text", 20, "Arial Bold").draw(0, 50, 0, 1, 1, Gosu::Color::WHITE)
You can replace 20 and 50 with any line height (font size) and y-position you want, just as you can change "Arial Bold" to "Arial" or any other font on your system. However, keep the 0 for the x-position (first parameter of draw()), since the centred text image is the same width as the window width.
See the links below for further information on from_text() and draw():
https://www.rubydoc.info/github/gosu/gosu/Gosu/Image#from_text-class_method
https://www.rubydoc.info/github/gosu/gosu/Gosu/Image#draw-instance_method
Better late than never...
No need to convert your text to an image. Just center the text using two parameters available on the Font.draw_text_rel method: rel_x and rel_y. See your code (modified a bit) below.
See: https://www.rubydoc.info/gems/gosu/Gosu%2FFont:draw_text_rel
require 'gosu'
class GameWindow < Gosu::Window
def initialize (width=800, height=600, fullscreen=false)
super
self.caption = 'Hello'
# #message = Gosu::Image.from_text(
# self, 'HELLO WORLD', Gosu.default_font_name, 45)
#font = Gosu::Font.new(45)
#message = "HELLO WORLD"
end
def draw
#font.draw_text_rel(#message, width / 2, height / 2, 1, rel_x = 0.5, rel_y = 0.5)
end
end
window = GameWindow.new
window.show