-
Notifications
You must be signed in to change notification settings - Fork 23
/
example4.c
112 lines (78 loc) · 1.95 KB
/
example4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//this example shows metasprite use, two pads polling,
//and simple collision detection that changes palette
#include "neslib.h"
//variables
static unsigned char i;
static unsigned char pad,spr;
static unsigned char touch;
static unsigned char frame;
//two players coords
static unsigned char cat_x[2];
static unsigned char cat_y[2];
//first player metasprite, data structure explained in neslib.h
const unsigned char metaCat1[]={
0, 0, 0x50, 0,
8, 0, 0x51, 0,
16, 0, 0x52, 0,
0, 8, 0x60, 0,
8, 8, 0x61, 0,
16, 8, 0x62, 0,
0, 16, 0x70, 0,
8, 16, 0x71, 0,
16, 16, 0x72, 0,
128
};
//second player metasprite, the only difference is palette number
const unsigned char metaCat2[]={
0, 0, 0x50, 1,
8, 0, 0x51, 1,
16, 0, 0x52, 1,
0, 8, 0x60, 1,
8, 8, 0x61, 1,
16, 8, 0x62, 1,
0, 16, 0x70, 1,
8, 16, 0x71, 1,
16, 16, 0x72, 1,
128
};
void main(void)
{
ppu_on_all();//enable rendering
//set initial coords
cat_x[0]=52;
cat_y[0]=100;
cat_x[1]=180;
cat_y[1]=100;
//init other vars
touch=0;//collision flag
frame=0;//frame counter
//now the main loop
while(1)
{
ppu_wait_frame();//wait for next TV frame
//flashing color for touch
i=frame&1?0x30:0x2a;
pal_col(17,touch?i:0x21);//set first sprite color
pal_col(21,touch?i:0x26);//set second sprite color
//process players
spr=0;
for(i=0;i<2;++i)
{
//display metasprite
spr=oam_meta_spr(cat_x[i],cat_y[i],spr,!i?metaCat1:metaCat2);
//poll pad and change coordinates
pad=pad_poll(i);
if(pad&PAD_LEFT &&cat_x[i]> 0) cat_x[i]-=2;
if(pad&PAD_RIGHT&&cat_x[i]<232) cat_x[i]+=2;
if(pad&PAD_UP &&cat_y[i]> 0) cat_y[i]-=2;
if(pad&PAD_DOWN &&cat_y[i]<212) cat_y[i]+=2;
}
//check for collision for a smaller bounding box
//metasprite is 24x24, collision box is 20x20
if(!(cat_x[0]+22< cat_x[1]+2 ||
cat_x[0]+ 2>=cat_x[1]+22||
cat_y[0]+22< cat_y[1]+2 ||
cat_y[0]+ 2>=cat_y[1]+22)) touch=1; else touch=0;
frame++;
}
}