jkcollision.ino 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <SPI.h>
  2. #include <GD.h>
  3. void readn(byte *dst, unsigned int addr, int c)
  4. {
  5. GD.__start(addr);
  6. while (c--)
  7. *dst++ = SPI.transfer(0);
  8. GD.__end();
  9. }
  10. static byte coll[256];
  11. static void load_coll()
  12. {
  13. while (GD.rd(VBLANK) == 0) // Wait until vblank
  14. ;
  15. while (GD.rd(VBLANK) == 1) // Wait until display
  16. ;
  17. while (GD.rd(VBLANK) == 0) // Wait until vblank
  18. ;
  19. readn(coll, COLLISION, sizeof(coll));
  20. }
  21. void setup()
  22. {
  23. int i;
  24. Serial.begin(1000000); // JCB
  25. GD.begin();
  26. GD.wr(JK_MODE, 1);
  27. GD.wr16(RAM_PAL, RGB(255,255,255));
  28. // Use the 4 palettes:
  29. // 0 pink, for J sprites
  30. // 1 green, for K sprites
  31. // 2 dark pink, J collisions
  32. // 3 dark green, K collisions
  33. for (i = 0; i < 256; i++) {
  34. GD.wr16(RAM_SPRPAL + (0 * 512) + (i << 1), RGB(255, 0, 255));
  35. GD.wr16(RAM_SPRPAL + (1 * 512) + (i << 1), RGB(0, 255, 0));
  36. GD.wr16(RAM_SPRPAL + (2 * 512) + (i << 1), RGB(100, 0, 100));
  37. GD.wr16(RAM_SPRPAL + (3 * 512) + (i << 1), RGB(0, 100, 0));
  38. }
  39. }
  40. byte spr;
  41. static void polar(float th, int r, byte jk)
  42. {
  43. // add 2 to the palette if this sprite is colliding
  44. byte colliding = coll[spr] != 0xff;
  45. GD.sprite(spr, 200 + int(r * sin(th)), 142 + int(r * cos(th)), 0, jk + (colliding ? 2 : 0), 0, jk);
  46. spr++;
  47. }
  48. void loop()
  49. {
  50. byte i;
  51. float th;
  52. spr = 0;
  53. // draw the J sprites (pink)
  54. for (i = 0; i < 5; i++) {
  55. th = (millis() / 3000.) + 2 * PI * i / 5;
  56. polar(th, 134, 0);
  57. }
  58. // draw the K sprites (green)
  59. randomSeed(4);
  60. for (i = 0; i < 17; i++) {
  61. th = (millis() / float(random(1000,3000))) + 2 * PI * i / 17;
  62. polar(th, 134, 1);
  63. }
  64. load_coll();
  65. }