Advertisement
Guest User

Untitled

a guest
Sep 30th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | None | 0 0
  1. /*  example code for cc65, for NES
  2.  *  construct some sprites / metasprites
  3.  *  using neslib
  4.  *  Doug Fraker 2018
  5.  */
  6.  
  7.  
  8.  
  9. #include "LIB/neslib.h"
  10. #include "LIB/nesdoug.h"
  11. #include "Sprites.h" // holds our metasprite data
  12.  
  13. #define MAX_BULLETS 1000
  14.  
  15. #pragma bss-name(push, "ZEROPAGE")
  16.  
  17. // GLOBAL VARIABLES
  18. unsigned char y_position=0x40; // all share same Y, which increases every frame
  19. unsigned char x_position=0x88;
  20. unsigned char x_position2=0xa0;
  21. unsigned char x_position3=0xc0;
  22. unsigned char sprid; // remember the index into the sprite buffer
  23. unsigned char pad1;
  24. unsigned char b1index;
  25. unsigned char index;
  26. unsigned char shooting=22;
  27. unsigned char shoot2;
  28.  
  29.  
  30.  
  31. const unsigned char text[]="Sprites";
  32.  
  33. const unsigned char palette_bg[]={
  34. 0x0f, 0x00, 0x10, 0x30, // black, gray, lt gray, white
  35. 0,0,0,0,
  36. 0,0,0,0,
  37. 0,0,0,0
  38. };
  39.  
  40. const unsigned char palette_sp[]={
  41. 0x0f, 0x17, 0x28, 0x28, // black, black, yellow
  42. 0,0,0,0,
  43. 0,0,0,0,
  44. 0,0,0,0
  45. };
  46.  
  47. struct BoxGuy {
  48.     unsigned char x;
  49.     unsigned char y;
  50.     unsigned char width;
  51.     unsigned char height;
  52. };
  53.  
  54. struct BoxGuy BoxGuy1 = {20,20,23,23};
  55.  
  56.  
  57.  
  58. /* struct bullet {
  59.     unsigned char x;
  60.     unsigned char y;
  61.    
  62. }
  63. */
  64.  
  65. unsigned char bullet_x[64];
  66. unsigned char bullet_y[64];
  67.    
  68. void movement(void);       
  69.  
  70.  
  71. void main (void) {
  72.    
  73.     ppu_off(); // screen off
  74.    
  75.     // load the palettes
  76.     pal_bg(palette_bg);
  77.     pal_spr(palette_sp);
  78.  
  79.     // use the second set of tiles for sprites
  80.     // both bg and sprite are set to 0 by default
  81.     bank_spr(1);
  82.  
  83.     // load the text
  84.     // vram_adr(NTADR_A(x,y));
  85.     vram_adr(NTADR_A(7,14)); // set a start position for the text
  86.    
  87.     // vram_write draws the array to the screen
  88.     vram_write(text,sizeof(text));
  89.    
  90.     ppu_on_all(); // turn on screen
  91.    
  92.    
  93.    
  94.     while (1){
  95.         // infinite loop
  96.  
  97.         ppu_wait_nmi(); // wait till beginning of the frame
  98.         // the sprites are pushed from a buffer to the OAM during nmi
  99.        
  100.         // clear all sprites from sprite buffer
  101.         oam_clear();
  102.        
  103.         pad1 = pad_poll(0); // read the first controller
  104.        
  105.         // reset index into the sprite buffer
  106.         sprid = 0;
  107.        
  108.         // push a single sprite
  109.         // oam_spr(unsigned char x,unsigned char y,unsigned char chrnum,unsigned char attr,unsigned char sprid);
  110.         // use tile #0, palette #0
  111.         sprid = oam_spr(x_position, y_position, 0, 0, sprid);
  112.        
  113.         // push a metasprite
  114.         // oam_meta_spr(unsigned char x,unsigned char y,unsigned char sprid,const unsigned char *data);
  115.         sprid = oam_meta_spr(x_position2, y_position, sprid, metasprite);
  116.        
  117.         // and another
  118.         sprid = oam_meta_spr(BoxGuy1.x, BoxGuy1.y, sprid, metasprite2);
  119.        
  120.         shoot2=pad1 & PAD_A;
  121.        
  122.         /*if (b1index>0){
  123.             sprid = oam_spr(bullet_x[b1index], bullet_y[b1index], 0, 0, sprid);
  124.             bullet_x[b1index]+=4;
  125.             } */
  126.            
  127.            
  128.                
  129.         movement();
  130.         ++x_position;
  131.         x_position2+=2;// every frame, shift them down 1 pixel
  132.         // note, y positions 0xef-0xff will not appear on the screen
  133.        
  134.         //SHOOTING CODE
  135.         if (shooting==22){
  136.         if(pad1 & PAD_A) {
  137.             shooting=3;
  138.             b1index+=1;
  139.                 bullet_x[b1index]=BoxGuy1.x;
  140.                 bullet_y[b1index]=BoxGuy1.y;
  141.         }
  142.    
  143. }
  144. //allow shooting after lifting the shoot key
  145.  if (shoot2==0){shooting=22;}
  146.  
  147.  //move da bullets
  148.  if (b1index>0){
  149.         for(index=0;index<64;index++){
  150.            
  151.             sprid = oam_spr(bullet_x[index], bullet_y[index], 0, 0, sprid);
  152.             bullet_x[index]+=4;
  153.             }
  154.             }
  155. }
  156. }
  157.  
  158.  
  159.  
  160.    
  161. void movement(void){
  162.     if(pad1 & PAD_LEFT){
  163.         BoxGuy1.x -= 1;
  164.     }
  165.     else if (pad1 & PAD_RIGHT){
  166.         BoxGuy1.x += 1;
  167.     }
  168.     if(pad1 & PAD_UP){
  169.         BoxGuy1.y -= 1;
  170.     }
  171.     else if (pad1 & PAD_DOWN){
  172.         BoxGuy1.y += 1;
  173.     }
  174.        
  175.        
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement