-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmain.c
90 lines (76 loc) · 2.64 KB
/
main.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
#include <gb/gb.h>
#include <stdio.h>
#include "MazeSprites.c"
#include "MazeMap.c"
#include "Bloke.c"
UINT8 playerlocation[2];
void performantdelay(UINT8 numloops){
UINT8 i;
for(i = 0; i < numloops; i++){
wait_vbl_done();
}
}
UBYTE canplayermove(INT8 movex, INT8 movey, UINT8 playerx, UINT8 playery){
// get background tile index
UINT8 indexX, indexY, tileindex;
if(movex<0 || movex == 0){
// check left edge of sprite
indexX = (UINT8)(((INT8)playerx + movex -8) / 8); // will always round down
}
else if(movex>0){
// check right edge of sprite
indexX = (playerx + movex - 1) / 8; // will always round down
}
if(movey<0 || movey == 0){
// check top edge of sprite
indexY = (playery + movey - 15) / 8; // will always round down
}
else if(movey>0){
// check bottom edge of sprite
indexY = (playery + movey - 8) / 8; // will always round down
}
// background map has 20 by 16 tiles
tileindex = 20 * indexY + indexX;
//printf("%u %u %u %u %u\n",(UINT16)(playerx + movex),(UINT16)(playery + movey),(UINT16)indexX,(UINT16)indexY,(UINT16)tileindex);
// check if index tileindex in background map is empty (0x00);
return MazeMap[tileindex] == 0;
}
void main(){
set_bkg_data(0, 4, MazeSprites);
set_bkg_tiles(0, 0, 20, 18, MazeMap);
set_sprite_data(0, 1, bloke); /* defines the sprite data */
set_sprite_tile(0,0); /* defines the tiles numbers */
playerlocation[0] = 16;
playerlocation[1] = 24;
move_sprite(0,playerlocation[0],playerlocation[1]);
SHOW_SPRITES;
SHOW_BKG;
DISPLAY_ON;
while(1){
if(joypad() & J_LEFT){
if(canplayermove(-1,0,playerlocation[0],playerlocation[1])){
playerlocation[0] -= 1;
move_sprite(0,playerlocation[0],playerlocation[1]);
}
}
else if(joypad() & J_RIGHT){
if(canplayermove(1,0,playerlocation[0],playerlocation[1])){
playerlocation[0] += 1;
move_sprite(0,playerlocation[0],playerlocation[1]);
}
}
else if(joypad() & J_UP){
if(canplayermove(0,-1,playerlocation[0],playerlocation[1])){
playerlocation[1] -= 1;
move_sprite(0,playerlocation[0],playerlocation[1]);
}
}
else if(joypad() & J_DOWN){
if(canplayermove(0,1,playerlocation[0],playerlocation[1])){
playerlocation[1] += 1;
move_sprite(0,playerlocation[0],playerlocation[1]);
}
}
performantdelay(2);
}
}