Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* example code for cc65, for NES
- * construct some sprites / metasprites
- * using neslib
- * Doug Fraker 2018
- */
- #include "LIB/neslib.h"
- #include "LIB/nesdoug.h"
- #include "Sprites.h" // holds our metasprite data
- #define MAX_BULLETS 1000
- #pragma bss-name(push, "ZEROPAGE")
- // GLOBAL VARIABLES
- unsigned char y_position=0x40; // all share same Y, which increases every frame
- unsigned char x_position=0x88;
- unsigned char x_position2=0xa0;
- unsigned char x_position3=0xc0;
- unsigned char sprid; // remember the index into the sprite buffer
- unsigned char pad1;
- unsigned char b1index;
- unsigned char index;
- unsigned char shooting=22;
- unsigned char shoot2;
- const unsigned char text[]="Sprites";
- const unsigned char palette_bg[]={
- 0x0f, 0x00, 0x10, 0x30, // black, gray, lt gray, white
- 0,0,0,0,
- 0,0,0,0,
- 0,0,0,0
- };
- const unsigned char palette_sp[]={
- 0x0f, 0x17, 0x28, 0x28, // black, black, yellow
- 0,0,0,0,
- 0,0,0,0,
- 0,0,0,0
- };
- struct BoxGuy {
- unsigned char x;
- unsigned char y;
- unsigned char width;
- unsigned char height;
- };
- struct BoxGuy BoxGuy1 = {20,20,23,23};
- /* struct bullet {
- unsigned char x;
- unsigned char y;
- }
- */
- unsigned char bullet_x[64];
- unsigned char bullet_y[64];
- void movement(void);
- void main (void) {
- ppu_off(); // screen off
- // load the palettes
- pal_bg(palette_bg);
- pal_spr(palette_sp);
- // use the second set of tiles for sprites
- // both bg and sprite are set to 0 by default
- bank_spr(1);
- // load the text
- // vram_adr(NTADR_A(x,y));
- vram_adr(NTADR_A(7,14)); // set a start position for the text
- // vram_write draws the array to the screen
- vram_write(text,sizeof(text));
- ppu_on_all(); // turn on screen
- while (1){
- // infinite loop
- ppu_wait_nmi(); // wait till beginning of the frame
- // the sprites are pushed from a buffer to the OAM during nmi
- // clear all sprites from sprite buffer
- oam_clear();
- pad1 = pad_poll(0); // read the first controller
- // reset index into the sprite buffer
- sprid = 0;
- // push a single sprite
- // oam_spr(unsigned char x,unsigned char y,unsigned char chrnum,unsigned char attr,unsigned char sprid);
- // use tile #0, palette #0
- sprid = oam_spr(x_position, y_position, 0, 0, sprid);
- // push a metasprite
- // oam_meta_spr(unsigned char x,unsigned char y,unsigned char sprid,const unsigned char *data);
- sprid = oam_meta_spr(x_position2, y_position, sprid, metasprite);
- // and another
- sprid = oam_meta_spr(BoxGuy1.x, BoxGuy1.y, sprid, metasprite2);
- shoot2=pad1 & PAD_A;
- /*if (b1index>0){
- sprid = oam_spr(bullet_x[b1index], bullet_y[b1index], 0, 0, sprid);
- bullet_x[b1index]+=4;
- } */
- movement();
- ++x_position;
- x_position2+=2;// every frame, shift them down 1 pixel
- // note, y positions 0xef-0xff will not appear on the screen
- //SHOOTING CODE
- if (shooting==22){
- if(pad1 & PAD_A) {
- shooting=3;
- b1index+=1;
- bullet_x[b1index]=BoxGuy1.x;
- bullet_y[b1index]=BoxGuy1.y;
- }
- }
- //allow shooting after lifting the shoot key
- if (shoot2==0){shooting=22;}
- //move da bullets
- if (b1index>0){
- for(index=0;index<64;index++){
- sprid = oam_spr(bullet_x[index], bullet_y[index], 0, 0, sprid);
- bullet_x[index]+=4;
- }
- }
- }
- }
- void movement(void){
- if(pad1 & PAD_LEFT){
- BoxGuy1.x -= 1;
- }
- else if (pad1 & PAD_RIGHT){
- BoxGuy1.x += 1;
- }
- if(pad1 & PAD_UP){
- BoxGuy1.y -= 1;
- }
- else if (pad1 & PAD_DOWN){
- BoxGuy1.y += 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement