MinecraftU / 2021-computer-adventures

Repository for our 2021 Computer Adventures Ruby project!
0 stars 0 forks source link

Create a game tick #12

Closed dealingwith closed 3 years ago

jamespeilunli commented 3 years ago

Note to self: move methods about the tetromino into the Tetromino class instead of the Game class. Move the draw method in Tetromino to Game and change it so it displays the entire board.

jamespeilunli commented 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.

dealingwith commented 3 years ago

Oh right, we really need a really short tick and then only move tetrominos every N ticks

dealingwith commented 3 years ago

@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!

dealingwith commented 3 years ago

@jamespeilunli I have the existing tetris.rb and tetrominos.rb ported to Ruby2D. I'll have a PR up soon for you to review.

dealingwith commented 3 years ago

@jamespeilunli -- see https://github.com/MinecraftU/2021-computer-adventures/pull/15

dealingwith commented 3 years ago

So basically we don't have to manage our own ticks!

jamespeilunli commented 3 years ago

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)

jamespeilunli commented 3 years ago

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.

dealingwith commented 3 years ago

No, it probably does not. Can you post the code your trying?

jamespeilunli commented 3 years ago

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?

jamespeilunli commented 3 years ago

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?

dealingwith commented 3 years ago

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