Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Rotating Bitmap when arrow keys are pressed.

This thread is locked; no one can reply to it. rss feed Print
Rotating Bitmap when arrow keys are pressed.
Pyreal
Member #15,537
March 2014

Hello, Working on a Homework assignment that is asking me to flip a bitmap image when the right and left arrow keys are pressed. with my current code when I press the arrow key it draws a new image as well as keeping my original. How would I go about telling my image to stay rotated while pressing said key?

Code is below

#SelectExpand
1ALLEGRO_BITMAP *catImage; 2 Sprite *cat; 3 ALLEGRO_BITMAP *bananaImage; 4 Sprite *bananas1; 5 ALLEGRO_FONT *font; 6 int score; 7 8 void initalizeSprites(){ 9 catImage = al_load_bitmap("cat.png"); 10 cat = new Sprite(catImage, 100, 100); 11 font = al_load_ttf_font("ARIAL.TTF", 12, 0); 12 13 bananaImage = al_load_bitmap("bananas.gif"); 14 bananas1 = new Sprite(bananaImage, 10, 10); 15 } 16 17 void cleanup() { 18 delete cat; 19 al_destroy_bitmap(catImage); 20 delete bananas1; 21 al_destroy_bitmap(bananaImage); 22 al_destroy_font(font); 23 } 24 25 void paintFrame() { 26 cat->paintComponent(); 27 bananas1->paintComponent(); 28 al_draw_text(font, 29 al_map_rgb(0, 0, 0), 30 cat->x + 30, cat->y - 30, 31 ALLEGRO_ALIGN_LEFT, 32 "meow"); 33 34 35 <b> if (isLeftArrowPressed()) 36 { 37 cat->x = cat->x - 10; 38 al_draw_bitmap(catImage, 100, 100, ALLEGRO_FLIP_HORIZONTAL); 39 } 40 if (isRightArrowPressed()) 41 { 42 cat->x = cat->x + 10; 43 al_draw_bitmap(catImage, 100, 100, ALLEGRO_FLIP_HORIZONTAL); 44 }</b> 45 46 if (isDownArrowPressed()) 47 { 48 cat->y = cat->y + 10; 49 50 } 51 if (isUpArrowPressed()) 52 { 53 cat->y = cat->y - 10; 54 // Right side of board knock back 55 } 56 if (cat->x + cat->width >= gameboard->getWidth()) 57 { 58 cat->x = cat->x - cat->height; 59 } 60 if (cat->x <= 0) 61 { 62 cat->x = cat->x + cat->height; 63 } 64 if (cat->y <= 0) 65 { 66 cat->y = cat->y + cat->height; 67 } 68 if (cat->y + cat->height >= gameboard->getHeight()) 69 { 70 cat->y = cat->y - cat->height; 71 } 72 if (cat->intersects(bananas1) && bananas1->visible) { 73 bananas1->visible = false; 74 score++;

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Pyreal
Member #15,537
March 2014

al_clear_to_color turns my entire screen black when the key is pressed. should this be used outside my if statement used for movement?

if (isLeftArrowPressed()) { cat->x = cat->x - 10; al_draw_bitmap(catImage, 100, 100, ALLEGRO_FLIP_HORIZONTAL);

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Pyreal said:

al_clear_to_color turns my entire screen black when the key is pressed. should this be used outside my if statement used for movement?

That's what clear to color does. If you don't want that to happen when they key is pressed, then yes it should be used outside your if statement. You'll want to do it once at the beginning of your paintFrame function.

You can use <code>code goes here...</code> tags to show your code on the forum.

Also, you generally want to separate your logic code (like movement and key presses) from your drawing code.

Pyreal
Member #15,537
March 2014

Thanks for the replies.

does al_flip_display(); need the image within it's brackets to know which image to rotate? I want the image to invert to look like it's point left instead of right when the left arrow key is pressed.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

al_flip_display() doesn't know anything about anything you've drawn. It merely shows whatever you've drawn on the back buffer on to the screen.

To flip an image, use the flag ALLEGRO_FLIP_HORIZONTAL in your al_draw_bitmap call. Pass 0 for the flags when you don't want it flipped.

Go to: