Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » I'm new at this!

This thread is locked; no one can reply to it. rss feed Print
 1   2 
I'm new at this!
luvzjetz16
Member #8,063
December 2006

hello everyone! I am trying to make a memory match game. So far I have created the game screen with all of the cards on it. However I'm havin some trouble trying to figure out three things: (1) How do I created the reverse side of the card and make it flip to the other side when clicked? (2) How do you activate the mouse on the screen so you can click the cards? (3) And is there anyway I can display a timer that will count down and stop the game when it gets to zero?

miran
Member #2,407
June 2002

Quote:

How do I create the reverse side of the card

Well duh. You draw the back side in a bitmap editor. Then in-game instead of the face of the card you draw the back side.

Quote:

...and make it flip to the other side when clicked?

Check the values of mouse_x, mouse_y and mouse_b&1 and when they indicate a card has been clicked, play the flip animation. A simple way to do this is to progressively stretch the back side to a smaller size in one dimension and then when it reaches 0, progressively draw the face stretched to a bigger size until it reaches full size.

Quote:

How do you activate the mouse on the screen so you can click the cards?

install_mouse()
RTFM, the rest of the mouse routines section as well. You can have Allegro or the underlying system draw the mouse cursor for you, or you can draw it yourself with draw_sprite().

Quote:

And is there anyway I can display a timer that will count down and stop the game when it gets to zero?

Yes. RTFM, the sections about timers and text output.

--
sig used to be here

William Labbett
Member #4,486
March 2004
avatar

Search the forums for "timers".

nonnus29
Member #2,606
August 2002
avatar

Welcome, jetzfan.

Quote:

(1) How do I created the reverse side of the card and make it flip to the other side when clicked?

Sounds like you need to keep track of the card states somewhere; like have a int flipped_flag with possible values 0=facedown and 1=faceup.

Quote:

(2) How do you activate the mouse on the screen so you can click the cards?

See Miran's post; installmouse(); There should be a program in the examples that uses the mouse, you can check that to see how it's done.

Quote:

(3) And is there anyway I can display a timer that will count down and stop the game when it gets to zero?

Again, see the examples to see an example of using timers.

luvzjetz16
Member #8,063
December 2006

ok if i can get the cards to flip over...how can i make them stay facing up if they are a match then flip back over if they don't make? can someone show me some code to do this?

Simon Parzer
Member #3,330
March 2003
avatar

Quote:

can someone show me some code to do this?

C or C++?

luvzjetz16
Member #8,063
December 2006

C

Steve Terry
Member #1,989
March 2002
avatar

1#define CARD_DOG 0
2#define CARD_CAT 1
3#define CARD_ELEPHANT 2
4#define CARD_SEAL 3
5#define CARD_LION 4
6#define CARD_HORSE 5
7#define NUM_CARDS 6
8 
9typedef struct card
10{
11 bool flipped;
12 int type;
13}CARD;
14 
15int main(int argc, char **argv)
16{
17 int cards_flipped = 0;
18 CARD cards[NUM_CARDS * 2];
19 BITMAP *card_face[NUM_CARDS];
20 BITMAP *card_back;
21 card_back = load_bitmap("./card_back.bmp");
22 card_face[CARD_DOG] = load_bitmap("./dog.bmp");
23 card_face[CARD_CAT] = load_bitmap("./cat.bmp");
24 card_face[CARD_ELEPHANT] = load_bitmap("./elephant.bmp");
25 // etc.
26 init_cards(&cards); // intialize your deck with random card types and two of each and all face down
27 while(1)
28 {
29 // if(mouse_x && mouse_y inside card)
30 // {
31 // set card flipped flag to true
32 // cards_flipped++;
33 // }
34
35 for(int i = 0; i < NUM_CARDS * 2; i++)
36 {
37 if(cards<i>.flipped == true)
38 // draw card face up
39 else
40 // draw card face down
41 }
42 // if cards_flipped == 2
43 // delay 1 second
44 // check to see if they match type
45 // if match type
46 // leave flipped
47 // set cards_flipped = 0
48 // else
49 // set both cards to not flipped
50 }
51}

Hope that gives you an idea, I've commented out the code you are supposed to implement, but it should give you a framework to start with.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

luvzjetz16
Member #8,063
December 2006

thank you very much....but how do you do this:
init_cards(&cards); // intialize your deck with random card types and two of each and all face down

I'm very sorry...I'm not good at this at all...like I said I'm very new at this!

Steve Terry
Member #1,989
March 2002
avatar

There is a ton of ways to do it but here is my example:

1void init_cards(CARD *cards)
2{
3 // Initialize all types to -1 (no type)
4 for(int i = 0; i < NUM_CARDS * 2; i++)
5 {
6 cards<i>->type = -1;
7 cards<i>->flipped = false;
8 }
9
10 // Initialize all cards
11 for(int num_times = 0; num_times < 2; num_times++)
12 {
13 for(int type = 0; type < NUM_CARDS; type++)
14 {
15 int card;
16 do
17 {
18 card = rand() % NUM_CARDS; // select a random card to assign
19 }while(cards[card]->type == -1); // find an uninitialized card
20 cards[card]->type = type; // set card to the type (dog, cat, fish, whatever)
21 }
22 }
23}

Untested but I hope it gives you an idea, you go through each type of card and assign it to a random index of your array if it's not already taken. You have to do it twice because there are two of each card. This is just one way of doing it out of many.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

luvzjetz16
Member #8,063
December 2006

am I doing this the right way...or is there a more simplier way to do this. I feel like i'm going about this the wrong way?!

Steve Terry
Member #1,989
March 2002
avatar

Nothing in code happens magically, this is the way I know and it should work. If you want simpler than I suggest trying something else, get a good C book and read up on stuff before jumping into a graphical game environment. I'm not sure what you mean by simpler.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Simon Parzer
Member #3,330
March 2003
avatar

luvzjetz16, maybe you should post the code YOU have written so far. In your first post you say

Quote:

So far I have created the game screen with all of the cards on it.

Could you post that code here?

luvzjetz16
Member #8,063
December 2006

yep...here it is:

#include <allegro.h>

void init();
void deinit();

int main() {
init();

//before the game page there are two other pages that I have no problem with

//game page
install_mouse();
show_mouse(screen);
int c;

BITMAP *img3, *cursor, *cursor2, *cursor3, *cursor4, *cursor5, *cursor6;

img3 = load_bmp("c:\\familyguylogo.bmp",NULL);
cursor = load_bmp("c:\\PETER.bmp",NULL);
cursor2 = load_bmp("c:\\LOIS.bmp",NULL);
//etc.....

blit(img3, screen, 0,0,0,0,640,480);
stretch_blit(img3, screen, 0,0, img3->w, img3->h, 0,0, SCREEN_W, SCREEN_H);

draw_sprite(screen, cursor, 50, 75);
draw_sprite(screen, cursor2, 50, 200);
draw_sprite(screen, cursor3, 195, 75);
draw_sprite(screen, cursor4, 195, 200);
draw_sprite(screen, cursor5, 337, 75);
draw_sprite(screen, cursor6, 337, 200);
draw_sprite(screen, cursor, 480, 75);
draw_sprite(screen, cursor3, 480, 200);
draw_sprite(screen, cursor3, 50, 325);
draw_sprite(screen, cursor4, 195, 325);
draw_sprite(screen, cursor5, 337, 325);
draw_sprite(screen, cursor6, 480, 325);

for(c=0;c<2000000000;c++);
system("cls")

Steve Terry
Member #1,989
March 2002
avatar

Use the code mockup tags please. Have you ever heard of arrays?

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

luvzjetz16
Member #8,063
December 2006

yeah i've heard of arrays. what are mockup tags?

Steve Terry
Member #1,989
March 2002
avatar

If you notice my code is in a scrollable text box, use:
[CODE]
int c;
[/CODE]
in lower case to use code blocks in your post. There is a link called HTML Mockup Code at the top of the post text box you can click to see all of the available tags.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Simon Parzer
Member #3,330
March 2003
avatar

OK, let me guess - before C you have written your programs in BASIC?

My advice is to learn the language you are dealing with. You don't know about the basic features of C, so you are obviously stuck with no knowledge.
Just one example:
Instead of calling your variables cursor,cursor2,cursor3,cursor4,... you can do the following:

BITMAP *cursor[6];
cursor[0] = load_bmp("...bmp",NULL);
cursor[1] = load_bmp("...bmp",NULL);
...

That way you can say

for (c=0;c<6;c++)
{
  draw_sprite(screen,cursor[c],c*100,10);
}

or something like that.

And instead of

for(c=0;c<2000000000;c++);

you can say (in Allegro)

rest(2000);

which will pause your game for exactly 2 seconds (2000 1/1000s of a second).

Of course these are just basic concepts and they won't give you a memory game. You have a lot to learn. First try to cope with C, and then with Allegro.
[append]
On a second thought you could make a memory game by making use of the little programming knowledge you have, but I'm afraid you wouldn't learn anything. Trust me, I've seen huge games from newbies like you, some guys even managed to do (text-based) RPGs without using loops, because they just didn't know them.
Nevertheless I encourage you to learn at least the basics of C (;D pun) before continuing.

luvzjetz16
Member #8,063
December 2006

is this better?!

1#include <allegro.h>
2 
3void init();
4void deinit();
5 
6int main() {
7init();
8 
9//before the game page there are two other pages that I have no problem with
10 
11//game page
12install_mouse();
13show_mouse(screen);
14int c;
15 
16BITMAP *img3, *cursor, *cursor2, *cursor3, *cursor4, *cursor5, *cursor6;
17 
18img3 = load_bmp("c:\\familyguylogo.bmp",NULL);
19cursor = load_bmp("c:\\PETER.bmp",NULL);
20cursor2 = load_bmp("c:\\LOIS.bmp",NULL);
21//etc.....
22 
23blit(img3, screen, 0,0,0,0,640,480);
24stretch_blit(img3, screen, 0,0, img3->w, img3->h, 0,0, SCREEN_W, SCREEN_H);
25 
26draw_sprite(screen, cursor, 50, 75);
27draw_sprite(screen, cursor2, 50, 200);
28draw_sprite(screen, cursor3, 195, 75);
29draw_sprite(screen, cursor4, 195, 200);
30draw_sprite(screen, cursor5, 337, 75);
31draw_sprite(screen, cursor6, 337, 200);
32draw_sprite(screen, cursor, 480, 75);
33draw_sprite(screen, cursor3, 480, 200);
34draw_sprite(screen, cursor3, 50, 325);
35draw_sprite(screen, cursor4, 195, 325);
36draw_sprite(screen, cursor5, 337, 325);
37draw_sprite(screen, cursor6, 480, 325);
38 
39for(c=0;c<2000000000;c++);
40system("cls")

Steve Terry
Member #1,989
March 2002
avatar

Yes, nothing after system("cls") though? I don't know how you managed to get that to compile.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

luvzjetz16
Member #8,063
December 2006

sorry it didnt copy and paste here it is:

1 deinit();
2 return 0;
3}
4END_OF_MAIN()
5 
6void init() {
7 int depth, res;
8 allegro_init;
9 depth = desktop_color_depth();
10 if (depth == 0) depth = 32;
11 set_color_depth(depth);
12 res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
13 if (res != 0) {
14 allegro_message(allegro_error);
15 exit(-1);
16 }
17 
18 install_timer();
19 install_keyboard();
20 install_mouse();
21}
22 
23void deinit() {
24 clear_keybuf();
25}

this part is created for me when I opened an allegro C project in DEV C++

Steve Terry
Member #1,989
March 2002
avatar

Read Simons post, he said it best. I'm not sure I can help you much more.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

luvzjetz16
Member #8,063
December 2006

ok guys....i've used Dev C++ for the past semester in college. I do know how to use arrays and things to make a memory game, but I don't know how to make that same game with just BITMAPS and sprites.

nonnus29
Member #2,606
August 2002
avatar

Quote:

I do know how to use arrays and things to make a memory game, but I don't know how to make that same game with just BITMAPS and sprites.

You have to use both..... Make the game like you would with an array, then use allegro to give it a graphical interface. This is were the creativety comes into programming; to explain better would be to program it for you? ???

Simon Parzer
Member #3,330
March 2003
avatar

Quote:

(1) How do I created the reverse side of the card and make it flip to the other side when clicked?

To answer your first question, just make variables. So for each card you have a variable "flipped" that indicates if the card is flipped or not. When the user clicks on one card you set flipped to 0 instead of 1. In your drawing code you have an if, or better said, several ifs.

1if (card1_flipped == 0)
2{
3 draw_sprite(screen,flipped,..,..);
4}
5else
6{
7 draw_sprite(screen,card1,..,..);
8}
9if (card2_flipped == 0)
10{
11 draw_sprite(screen,flipped,..,..);
12}
13else
14{
15 draw_sprite(screen,card2,..,..);
16}

You would save a lot of code by using a combination of structs and arrays, but you obviously can do without them. By declaring ~50 different variables at the beginning of your code. I did that once, and it was no fun.

 1   2 


Go to: