2 Gamebuino podstawy

Pong

Podstawowe polecenia
(kod na końcu)
Dokumentacja poleceń KODU dla META
Dokumentacja polecen KODU Grafika dla META
Dokumentacja poleceeń KODU dla Clasic
Dokumentacja polecen KODU dla Arduino



gb.begin();
uruchamia Gamebuino, aktywowany w setup. Bez argumentów.


gb.update();
makes sure that the Gamebuino Meta runs at a constant framerate. It will return true whenever a next frame is ready to render. Default framerate: 25FPS, settable via gb.setFrameRate()
It also takes care of updating buttons, sound, sending out the screen, etc.
Returns:
bool: true if it is time to render the next frame
czyli kod
#include <Gamebuino-Meta.h>
void setup() {
  gb.begin();
  gb.setFrameRate(50);
}


void loop() {
  while(!gb.update());
   gb.display.clear();
  // this code will run at a steady framerate (default: 25 FPS)
}
czeka do końca ramki.


gb.setFrameRate(fps);
ustawia framerate 25/50...


gb.display.clear();
Clears the display buffer (erase everything) and set the text cursor back to 0,0 (top left corner). If you need the display to be cleared at each frame, set gb.display.persistence to true (it will give better image stability than if you do it manually).
Nie chce kompilować "persistence" ???


Buttons:
BUTTON_A
BUTTON_B
BUTTON_MENU (used to be BUTTON_C)
BUTTON_UP
BUTTON_RIGHT
BUTTON_DOWN
BUTTON_LEFT
gb.buttons.held(BUTTON, ms);
 returns true once the button has been held for time amount of frames.


gb.buttons.repeat(BUTTON, ms); 
returns true if the button has been held down for period amount of frames, repetitively.
A period of 0 will check if the button is pressed down in this instant
czyli kod
// Update paddle positions
  if (gb.buttons.repeat(BUTTON_UP, 0)) {
    paddle1_posY = paddle1_posY - 1;
  }
przy stałym trzymaniu UP co ramkę zmniejszy wartość zmiennej o 1.


Display informacje: 
1.8", 80*64px RGB 16bit color display (or 160*128px, indexed 16 colors). 25FPS refresh rate by default, up to 50FPS. Graphic functions can be used on the display, lights or images.
For example Graphics::fill() fills with one color. You can do the following:
gb.display.fill() to fill the display with one color.
gb.lights.fill() to set all the lights to the same color.
myImage.fill() to fill your Image with one color.
gb.tft 
You can use graphic functions directly to the screen and bypass the gb.display buffer by using gb.tft (not advised!).
Entries with an asterix (*) are not available for gb.tft.


gb.display.init(160, 128, ColorMode::index);
ustawia wysoką rozdzielczość
patrz przykładowa gra LINK    (uzywa bezposredniego dostepu do ekranu gb.tft +FixedPoints library  )


gb.display.height();
returns the height of the display / image / light / whatever
tu poniewaz po gd.display dotyczy ekranu

width returns the width of the display / image / light / whatever


gb.display.fillRect(x,y,w,h);
draws a filled rectangle to the screen/image/whatever, starting from (x, y) and with a certain width and height.
// draw a green rectangle
gb.display.setColor(GREEN);
gb.display.fillRect(10, 10, 20, 5);


gb.display.setCursor(x,y);
sets both the x-position and the y-position of the text cursors at the same time.


gb.display.print("tekst lub zmienna");
drukuje na ekranie







Kod PONGa z opisanymi poleceniami:

#include <Gamebuino-Meta.h>

// ball attributes
int ball_posX = 20;
int ball_posY = 20;
int ball_speedX = 1;
int ball_speedY = 1;
int ball_size = 4;

// paddle1 attributes
int paddle1_posX = 10;
int paddle1_posY = 30;

// paddle2 attributes
int paddle2_posX = gb.display.width() - 13;
int paddle2_posY = 30;

// Dimension of both paddles
int paddle_height = 10;
int paddle_width = 3;


// Scores
int score1;  // Player 1 score
int score2;  // Player 2 score

void setup() {
  gb.begin();                                         //gb.begin()
}

void loop() {
  while(!gb.update());
  gb.display.clear();

  // Update paddle positions
  if (gb.buttons.repeat(BUTTON_UP, 0)) {
    paddle1_posY = paddle1_posY - 1;
  }
  if (gb.buttons.repeat(BUTTON_DOWN, 0)) {
    paddle1_posY = paddle1_posY + 1;
  }
  if (gb.buttons.repeat(BUTTON_B, 0)) {
    paddle2_posY = paddle2_posY - 1;
  }
  if (gb.buttons.repeat(BUTTON_A, 0)) {
    paddle2_posY = paddle2_posY + 1;
  }

  // Update ball position
  ball_posX = ball_posX + ball_speedX;
  ball_posY = ball_posY + ball_speedY;

  // Collisions with walls
  if (ball_posY < 0) {
    ball_speedY = 1;
  }
  if (ball_posY > gb.display.height() - ball_size) {
    ball_speedY = -1;
  }
  if (ball_posX > gb.display.width() - ball_size) {
    ball_speedX = -1;
  }

  // Ball-paddle1 collisions
  if ( (ball_posX == paddle1_posX + paddle_width)
    && (ball_posY + ball_size >= paddle1_posY) 
    && (ball_posY <= paddle1_posY + paddle_height) ) {
    ball_speedX = 1;
  }
  // Ball-paddle2 collisions
  if ( (ball_posX + ball_size == paddle2_posX)
    && (ball_posY + ball_size >= paddle2_posY) 
    && (ball_posY <= paddle2_posY + paddle_height) ) {
    ball_speedX = -1;
  }

  // Check if the ball left the screen
  if (ball_posX < 0) {
    // Reset ball
    ball_posX = 20;
    ball_posY = 20;
    ball_speedX = 1;
    ball_speedY = 1;
    // Increment player2's score
    score2 = score2 + 1;
  }
  if (ball_posX > gb.display.width()) {
    // Reset ball
    ball_posX = 20;
    ball_posY = 20;
    ball_speedX = 1;
    ball_speedY = 1;
    // Increment player1's score
    score1 = score1 + 1;
  }

  // Display ball
  gb.display.fillRect(ball_posX, ball_posY, ball_size, ball_size);
  // Display paddle1
  gb.display.fillRect(paddle1_posX, paddle1_posY, paddle_width, paddle_height);
  // Display paddle2
  gb.display.fillRect(paddle2_posX, paddle2_posY, paddle_width, paddle_height);

  // Display scores
  gb.display.setCursor(35, 5);
  gb.display.print(score1);
  gb.display.setCursor(42, 5);
  gb.display.print(score2);
}

.