git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

commit 7a8459d07a69a923f850ccbf4720321418ccf2e8
parent 319a6bb1bbb57515458388d09e4cf444762aa5ea
Author: toxa <untoxa@mail.ru>
Date:   Mon, 19 Oct 2020 14:55:58 +0300

multiplayer SGB pong example

Diffstat:
Mgbdk-lib/examples/gb/Makefile12+++++++++---
Agbdk-lib/examples/gb/pong.c144+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 153 insertions(+), 3 deletions(-)

diff --git a/gbdk-lib/examples/gb/Makefile b/gbdk-lib/examples/gb/Makefile @@ -17,7 +17,8 @@ BINS = galaxy.gb \ bcd.gb \ wobble.gb \ crash.gb \ - multiplayer.gb + multiplayer.gb \ + pong.gb # sound.gb: Cant do bitfields OTHER = sound.gb \ @@ -109,4 +110,10 @@ ram_fn.gb: ram_fn.o # SGB multiplayer example multiplayer.gb: multiplayer.o - $(CC) -Wm-ys -o $@ $< - + $(CC) -Wm-ys -o $@ $< + +############################################################ +# SGB pong example + +pong.gb: pong.o + $(CC) -Wm-ys -o $@ $< diff --git a/gbdk-lib/examples/gb/pong.c b/gbdk-lib/examples/gb/pong.c @@ -0,0 +1,143 @@ +#include <gb/gb.h> +#include <gb/sgb.h> + +#include <stdio.h> +#include <gb/console.h> + +UINT8 sprite_data[] = { + 0x3C,0x3C,0x42,0x7E,0x99,0xFF,0xA9,0xFF,0x89,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C, + 0x3C,0x3C,0x42,0x7E,0xB9,0xFF,0x89,0xFF,0x91,0xFF,0xB9,0xFF,0x42,0x7E,0x3C,0x3C, + 0x3C,0x3C,0x42,0x7E,0x99,0xFF,0x89,0xFF,0x99,0xFF,0x89,0xFF,0x5A,0x7E,0x3C,0x3C, + 0x3C,0x3C,0x42,0x7E,0xA9,0xFF,0xA9,0xFF,0xB9,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C + +}; + +// initializes sprites for pad. every pad uses 3 sprites which id's are aligned by 4 +void init_pad(UINT8 n) { + set_sprite_tile(n << 2, n); + set_sprite_tile((n << 2) + 1, n); + set_sprite_tile((n << 2) + 2, n); +} + +// inline function for moving pads; code of this function will be inlined with the code of main() +inline void draw_pad(UINT8 n, UINT8 x, UINT8 y) { + move_sprite(n << 2, x, y); + move_sprite((n << 2) + 1, x, y + 8); + move_sprite((n << 2) + 2, x, y + 16); +} + +joypads_t joypads; + +// absolute Y coordinates of player 1 & 2 +UINT8 player1, player2; +UINT16 player1_score, player2_score; + +// player constraints +#define YMIN 28 +#define YMAX 100 +#define PLAYER1_X 16 +#define PLAYER2_X (UINT8)((20 * 8) - 8) + +// coordinates and speeds of ball +UINT8 ballX, ballY; +INT8 spd_ballX, spd_ballY; + +#define INITBALLX 80 + 4 +#define INITBALLY 64 + 8 + +const unsigned char HUD[] = " p1: %d p2: %d"; + +// main funxction +void main(void) { + // init palettes + BGP_REG = OBP0_REG = OBP1_REG = 0xE4; + + // load tile data into VRAM + set_sprite_data(0, 4, sprite_data); + + // init pad sprites + init_pad(0); + init_pad(1); + + // init ball sprite + set_sprite_tile(3, 2); + + // show bkg and sprites + SHOW_BKG; SHOW_SPRITES; + + // init 2 joypads + if (joypad_init(2, &joypads) != 2) { + printf(" This program must\n be executed on\n Super GameBoy"); + return; + } + + // init players + player1 = 64, player2 = 64; + player1_score = player2_score = 0; + + // draw score + printf(HUD, player1_score, player2_score); + + // init ball + ballX = INITBALLX, ballY = INITBALLY; + spd_ballX = 1, spd_ballY = 1; + + while(1) { + // poll joypads + joypad_ex(&joypads); + + // player 1 + if (joypads.joy0 & J_UP) { + player1 -= 2; + if (player1 < YMIN) player1 = YMIN; + } else if (joypads.joy0 & J_DOWN) { + player1 += 2; + if (player1 > YMAX) player1 = YMAX; + } + draw_pad(0, PLAYER1_X, player1); + + // player 2 + if (joypads.joy1 & J_UP) { + player2 -= 2; + if (player2 < YMIN) player2 = YMIN; + } else if (joypads.joy1 & J_DOWN) { + player2 += 2; + if (player2 > YMAX) player2 = YMAX; + } + draw_pad(1, PLAYER2_X, player2); + + // move ball + ballX += spd_ballX, ballY += spd_ballY; + // check bounce from limits + if ((ballY < YMIN) || (ballY > (YMAX + 24))) { + spd_ballY = -spd_ballY; + } + // check bounce from bats + if (ballX < (PLAYER1_X + 8)) { + if ((ballY > player1) && (ballY < (player1 + 24)) && (spd_ballX < 0)) + spd_ballX = -spd_ballX; + } else if (ballX > (PLAYER2_X - 8)) { + if ((ballY > player2) && (ballY < (player2 + 24)) && (spd_ballX > 0)) + spd_ballX = -spd_ballX; + } + // check player1 or 2 wins, update scores, start from center + if (ballX < PLAYER1_X) { + // player2 wins + ballX = INITBALLX, ballY = INITBALLY; + spd_ballX = -spd_ballX; + player2_score++; + gotoxy(0, 0); printf(HUD, player1_score, player2_score); + } else if (ballX > PLAYER2_X) { + // player1 wins + ballX = INITBALLX, ballY = INITBALLY; + spd_ballX = -spd_ballX; + player1_score++; + gotoxy(0, 0); printf(HUD, player1_score, player2_score); + } + // move ball sprite + move_sprite(3, ballX, ballY); + + // wait for VBlank to slow down everything + wait_vbl_done(); + } +} +

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.