Bouncing ball demo for the Gameduino

Dependencies:   Gameduino mbed

Committer:
TheChrisyd
Date:
Fri Dec 21 13:46:16 2012 +0000
Revision:
1:620429301ada
Parent:
0:a0257f241e9e
updated Gameduino library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TheChrisyd 0:a0257f241e9e 1 #include "mbed.h"
TheChrisyd 0:a0257f241e9e 2 #include "GD.h"
TheChrisyd 0:a0257f241e9e 3 #include "shield.h"
TheChrisyd 0:a0257f241e9e 4 #include "ball.h"
TheChrisyd 0:a0257f241e9e 5
TheChrisyd 0:a0257f241e9e 6 GDClass GD(ARD_MOSI, ARD_MISO, ARD_SCK, ARD_D9, USBTX, USBRX) ;
TheChrisyd 0:a0257f241e9e 7
TheChrisyd 0:a0257f241e9e 8 void setup() {
TheChrisyd 0:a0257f241e9e 9 GD.begin();
TheChrisyd 0:a0257f241e9e 10
TheChrisyd 0:a0257f241e9e 11 // Background image
TheChrisyd 0:a0257f241e9e 12 GD.copy(RAM_PIC, bg_pic, sizeof(bg_pic));
TheChrisyd 0:a0257f241e9e 13 GD.copy(RAM_CHR, bg_chr, sizeof(bg_chr));
TheChrisyd 0:a0257f241e9e 14 GD.copy(RAM_PAL, bg_pal, sizeof(bg_pal));
TheChrisyd 0:a0257f241e9e 15
TheChrisyd 0:a0257f241e9e 16 // Sprite graphics
TheChrisyd 0:a0257f241e9e 17 GD.uncompress(RAM_SPRIMG, ball);
TheChrisyd 0:a0257f241e9e 18
TheChrisyd 0:a0257f241e9e 19 // Palettes 0 and 1 are for the ball itself,
TheChrisyd 0:a0257f241e9e 20 // and palette 2 is the shadow. Set it to
TheChrisyd 0:a0257f241e9e 21 // all gray.
TheChrisyd 0:a0257f241e9e 22 int i;
TheChrisyd 0:a0257f241e9e 23 for (i = 0; i < 256; i++)
TheChrisyd 0:a0257f241e9e 24 GD.wr16(RAM_SPRPAL + (2 * (512 + i)), RGB(64, 64, 64));
TheChrisyd 0:a0257f241e9e 25
TheChrisyd 0:a0257f241e9e 26 // Set color 255 to transparent in all three palettes
TheChrisyd 0:a0257f241e9e 27 GD.wr16(RAM_SPRPAL + 2 * 0xff, TRANSPARENT);
TheChrisyd 0:a0257f241e9e 28 GD.wr16(RAM_SPRPAL + 2 * 0x1ff, TRANSPARENT);
TheChrisyd 0:a0257f241e9e 29 GD.wr16(RAM_SPRPAL + 2 * 0x2ff, TRANSPARENT);
TheChrisyd 0:a0257f241e9e 30 }
TheChrisyd 0:a0257f241e9e 31
TheChrisyd 0:a0257f241e9e 32 #define RADIUS (112 / 2) // radius of the ball, in pixels
TheChrisyd 0:a0257f241e9e 33
TheChrisyd 0:a0257f241e9e 34 #define YBASE (300 - RADIUS)
TheChrisyd 0:a0257f241e9e 35
TheChrisyd 0:a0257f241e9e 36 int main() {
TheChrisyd 0:a0257f241e9e 37 setup();
TheChrisyd 0:a0257f241e9e 38 while (1) {
TheChrisyd 0:a0257f241e9e 39 int x = 200, y = RADIUS; // ball position
TheChrisyd 0:a0257f241e9e 40 int xv = 2, yv = 0; // ball velocity
TheChrisyd 0:a0257f241e9e 41
TheChrisyd 0:a0257f241e9e 42 int r; // frame counter
TheChrisyd 0:a0257f241e9e 43 for (r = 0; ; r++) {
TheChrisyd 0:a0257f241e9e 44 GD.__wstartspr((r & 1) ? 256 : 0); // write sprites to other frame
TheChrisyd 0:a0257f241e9e 45 draw_ball(x + 15, y + 15, 2); // draw shadow using palette 2
TheChrisyd 0:a0257f241e9e 46 draw_ball(x, y, r & 1); // draw ball using palette 0 or 1
TheChrisyd 0:a0257f241e9e 47 GD.__end();
TheChrisyd 0:a0257f241e9e 48
TheChrisyd 0:a0257f241e9e 49 // paint the new palette
TheChrisyd 0:a0257f241e9e 50 uint16_t palette = RAM_SPRPAL + 512 * (r & 1);
TheChrisyd 0:a0257f241e9e 51 byte li;
TheChrisyd 0:a0257f241e9e 52 for (li = 0; li < 7; li++) {
TheChrisyd 0:a0257f241e9e 53 byte liv = 0x90 + 0x10 * li; // brightness goes 0x90, 0xa0, etc
TheChrisyd 0:a0257f241e9e 54 uint16_t red = RGB(liv, 0, 0);
TheChrisyd 0:a0257f241e9e 55 uint16_t white = RGB(liv, liv, liv);
TheChrisyd 0:a0257f241e9e 56 byte i;
TheChrisyd 0:a0257f241e9e 57 for (i = 0; i < 32; i++) { // palette cycling using 'r'
TheChrisyd 0:a0257f241e9e 58 GD.wr16(palette, ((i + r) & 16) ? red : white);
TheChrisyd 0:a0257f241e9e 59 palette += 2;
TheChrisyd 0:a0257f241e9e 60 }
TheChrisyd 0:a0257f241e9e 61 }
TheChrisyd 0:a0257f241e9e 62
TheChrisyd 0:a0257f241e9e 63 // bounce the ball around
TheChrisyd 0:a0257f241e9e 64 x += xv;
TheChrisyd 0:a0257f241e9e 65 if ((x < RADIUS) || (x > (400 - RADIUS)))
TheChrisyd 0:a0257f241e9e 66 xv = -xv;
TheChrisyd 0:a0257f241e9e 67 y += yv;
TheChrisyd 0:a0257f241e9e 68 if ((yv > 0) && (y > YBASE)) {
TheChrisyd 0:a0257f241e9e 69 y = YBASE - (y - YBASE); // reflect in YBASE
TheChrisyd 0:a0257f241e9e 70 yv = -yv; // reverse Y velocity
TheChrisyd 0:a0257f241e9e 71 }
TheChrisyd 0:a0257f241e9e 72 if (0 == (r & 3))
TheChrisyd 0:a0257f241e9e 73 yv++; // gravity
TheChrisyd 0:a0257f241e9e 74
TheChrisyd 0:a0257f241e9e 75 // swap frames
TheChrisyd 0:a0257f241e9e 76 GD.waitvblank();
TheChrisyd 0:a0257f241e9e 77 GD.wr(SPR_PAGE, (r & 1));
TheChrisyd 0:a0257f241e9e 78 }
TheChrisyd 0:a0257f241e9e 79 }
TheChrisyd 0:a0257f241e9e 80 }