Closed dealingwith closed 3 years ago
Issue: the delay interferes with other things, like clicking the close window button. It will probably also affect moving when implemented later on. Of course, a delay of one second contributes to this problem; it will probably be less noticeable when delay is lower.
Oh right, we really need a really short tick and then only move tetrominos every N ticks
@jamespeilunli I got Ruby2D installed and running really quickly and it has its own tick
The window also manages the update loop, one of the few infinite loops in programming you’ll encounter that isn’t a mistake. Every window has a heartbeat, a loop that runs 60 times per second, or as close to it as the computer’s performance will allow. Using the update method, we can enter this loop and make the window come to life!
@jamespeilunli I have the existing tetris.rb and tetrominos.rb ported to Ruby2D. I'll have a PR up soon for you to review.
@jamespeilunli -- see https://github.com/MinecraftU/2021-computer-adventures/pull/15
So basically we don't have to manage our own ticks!
How do I use the update method?
def run
update do
end
end
inside my Game
class throws undefined method `update' for #<Game:0x000055b383351c18> (NoMethodError)
. If I use Ruby2D::update
it throws undefined method `update' for Ruby2D:Module (NoMethodError)
Aha! I put a $ in front of it, indicating global variable. But now it's giving me a syntax error. I think update
just doesn't work inside methods??? I'll just move it outside of the class.
No, it probably does not. Can you post the code your trying?
I first tried the code here
Then I tried
def run
$update do
end
end
and it threw
/home/jamesli/github_repos/2021-computer-adventures/tetris.rb:64: syntax error, unexpected `do', expecting `end'
$update do
/home/jamesli/github_repos/2021-computer-adventures/tetris.rb:72: syntax error, unexpected `end', expecting end-of-input
So then I just removed the run method and placed it outside of the class.
See this too; if it's not my fault, would this be problematic for OOP refactoring?
Look at this person who made a snake game. In the key-mappings section, you can see that he didn't put the key detection in a class. I guess I'll do something similar in this?
Yeah, I suggest we follow that pattern, except put our classes in their own files. Their final code is here: https://github.com/JoaoCardoso193/Snake/blob/master/snake.rb
Note to self: move methods about the tetromino into the
Tetromino
class instead of theGame
class. Move the draw method inTetromino
toGame
and change it so it displays the entire board.