Now that this is clear, try to draw the ball higher up the screen. So, knowing this, the bottom right corner of the rectangle has a position of ( x + width - 1, y + height - 1). Just like the screen, the X and ypassed as parameters are the position of the top left corner of our rectangle. Gb.display.drawRect(int x, int y, int width, int height) While we are on the topic of screen and positioning, let's take a closer look at how the gb.display.drawRect() function works. Also note that even if you know this, you should not use 80 instead of gb.display.width(), because it is less implicit and is a sort of light obfuscation. You can check the width by doing gb.display.print(gb.display.width()) in your code. Note: the screen's size is 80 pixels wide by 64 pixels tall. And the same thing goes for the lowest pixel. Well the same thing happens with you console's screen! The rightmost pixel has an x coordinate equal to gb.display.width() - 1. Here you can easily see that the forth pixel has a coordinate of 3, not 4. Let me put it this way: if you had a screen with only four pixels, and the the first pixel's coordinate was 0, what would be the coordinate of the forth (last) pixel? Now why did we subtract 1 to get the opposite corner? Well it has to do with the fact that the first pixel has a value of 0 on both axes. This means that the opposite corner (bottom right) is at ( gb.display.width() - 1, gb.display.height() - 1). Not only that, but the Y values get bigger when you move downwards (the X values grow when moving to the right, just like a regular graph). But for screens, this isn't the case.Ĭoordinates on the Gamebuino META's screen, just like all other screens, have their origin in the upper left corner. When you were taught coordinates, you probably saw something like this where the center is where X and Y are equal to zero, and that X and Y get bigger when you go to the up and to the right. Gb.display.fillRect(positionX, 32, 4, 4) Now, look at this code: it makes the ball move forward on it's own! #include While we are at it, let's add another variable called speedX which keeps track of the speed of our ball along the X axis. Moving Forwardįor our bouncing ball, we would need a new variable called positionX, which, like you guessed, stores the X (or horizontal) position of our ball. BOUNCING BALLS GAME SOURCE CODE CODEThis is why we will now code a bouncing ball. So we are not far from having a Pong paddle )But a Pong game without a ball is not very fun. So in summary,** you made a rectangle move by pressing buttons**. Finaly, we used a function to draw rectangles to the screen. We also looked at if statements and conditions to interact with the player. You learned to use variables of type int. In the previous workshop, you made a tally counter. Completed the Hello World and Tally counter workshops. Completed the Gamebuino META setup workshop. BOUNCING BALLS GAME SOURCE CODE FULLWe are approaching a full game! Bouncing all over the place This time, it is going to be a lot more dynamic.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |