/ / Ruby Gosu ako začať nový čas po kliknutí - ruby, game-physics, libgosu

Ruby Gosu ako začať nový čas na kliknutí - rubín, herná fyzika, libgosu

Snažím sa jednoducho presunúť obrázok po kliknutí zz jednej strany obrazovky na druhú. Ale nemôžem celkom prísť na to, ako pracovať s časom. V podstate musím začať hýbať loptou po Gosu :: KbReturn.

Akákoľvek pomoc by bola veľmi ocenená

require "gosu"
def media_path(file)
File.join(File.dirname(File.dirname(
__FILE__)), "media", file)
end
class Game < Gosu::Window

def initialize
@width = 800
@height = 600
super @width,@height,false
@background = Gosu::Image.new(
self, media_path("background.png"), false)
@ball = Gosu::Image.new(
self, media_path("ball.png"), false)
@time = Gosu.milliseconds/1000
@x = 500
@y = 500
@buttons_down = 0
@text = Gosu::Font.new(self, Gosu::default_font_name, 20)
end

def update
@time = Gosu.milliseconds/1000
end

def draw
@background.draw(0,0,0)
@ball.draw(@x,@y,0)
@text.draw(@time, 450, 10, 1, 1.5, 1.5, Gosu::Color::RED)
end
def move
if ((Gosu.milliseconds/1000) % 2) < 100 then @x+=5 end
end

def button_down(id)
move if id == Gosu::KbReturn
close if id ==Gosu::KbEscape
@buttons_down += 1
end
def button_up(id)
@buttons_down -= 1
end
end

Game.new.show

odpovede:

2 pre odpoveď č. 1

Najskôr máte obslužnú rutinu udalosti klávesnice na nesprávnom mieste. The update metóda slúži iba ako spätné volanie v období update_interval a mali by ste ju určite umiestniť button_down inštančná metóda Gosu :: Window.

Po druhé, ak zavoláte metódu move na aktualizáciu pozícií herných objektov, nemá zmysel to robiť v slučke. Mali by ste iba aktualizovať @x raz za hovor.

Do tretice vaše použitie @time premenná inštancie v move metóda nedáva zmysel. Ak potrebujete obmedziť pohyb až po uplynutí určitého času, stačí skontrolovať, či časovač prekročil konkrétny prírastok, napr. s celým číslom (s určitou toleranciou): if (Gosu.milliseconds % @increment) < @epsilon then.

aktualizácia: aktualizovať @x po dobu 10 sekúnd po stlačení klávesu Enter

class Game < Gosu::Window
def initialize
…
@trigger = false    # if trigger for delayed action was activated
@start_time = 0     # saved time when trigger was started
end

def update
…
if @trigger
if Gosu.milliseconds - @start_time < 10_000
@x += 1           # update x-position for 10 seconds
else
@trigger = false  # disable trigger after timeout elapsed
end
end
end

def button_down(key)
case key
when Gosu::KbReturn
@start_time=Gosu.milliseconds   # store current elapsed time
@trigger=true                   # enable trigger
when Gosu::KbEscape
…
end
end
end

0 pre odpoveď č. 2

pridaný

def update
@x+=1 # in this method it increments every game second
end

def move
@x = 0
while @x > 0
do somemthing
end
end

Jednoducho som nechápal, že metóda aktualizácie neustále opakuje