编写一个程序,其中精灵被绘制到屏幕上并每50ms向上移动一个像素。当我在函数中声明精灵和位图时,它会绘制但不会移动,当我在全局声明它时根本不会出现。我的代码看起来很正确但不会工作。
Sprite hero_sprite; //declared globally outside of main
Sprite * hero_sprite_pointer = &hero_sprite;
byte hero_bitmap [] = {
BYTE( 10100000 ),
BYTE( 11100000 ),
BYTE( 10100000 )
};
void setup_hero(Sprite hero_sprite, byte * hero_bitmap) {
init_sprite(hero_sprite_pointer, 46, 23, 3, 3, hero_bitmap);
draw_sprite(hero_sprite_pointer);
hero_sprite.dx = 1;
hero_sprite.dy = 0;
while (1) {
update_hero();
draw_sprite( hero_sprite_pointer );
_delay_ms( 50 );
refresh();
}
}
void update_hero(Sprite * hero_sprite) {
hero_sprite->x += hero_sprite->dx;
hero_sprite->y += hero_sprite->dy;
}
init sprite初始化正在绘制的精灵的原始位置。 draw sprite实际显示它。我已经测试过看看while循环是否正在运行,但它不会绘制精灵。
编辑:调用函数是
void init_sprite(
Sprite * sprite,
byte x,
byte y,
byte width,
byte height,
byte * bitmap
) {
sprite->x = x;
sprite->y = y;
sprite->width = width;
sprite->height = height;
sprite->bitmap = bitmap;
}
void draw_sprite( Sprite * sprite ) {
if ( !sprite->is_visible ) return;
// Index into the bitmap. This is updated as we traverse the
// pixels of the image.
int idx = 0;
for ( int row = 0; row < sprite->height; row++ ) {
float screen_y = sprite->y + row;
if ( screen_y < 0 ) continue;
if ( screen_y >= LCD_Y ) break;
int col = 0;
int bitmask = 1 << 7;
while ( col < sprite->width ) {
byte pixel = sprite->bitmap[idx] & bitmask;
float screen_x = sprite->x + col;
if ( (screen_x >= 0) && ( screen_x < LCD_X ) && pixel ) {
// Set pixel only if the bit is set. 0 is transparent.
set_pixel( screen_x, screen_y, 1 );
}
col++;
if ( col % 8 == 0 ) {
idx++;
bitmask = 1 << 7;
}
else {
bitmask >>= 1;
}
}
if ( sprite->width % 8 != 0 ) {
idx++;
}
}
}