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?
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.
...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.
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().
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.
Search the forums for "timers".
Welcome, jetzfan.
(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.
(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.
(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.
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?
can someone show me some code to do this?
C or C++?
C
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 | |
9 | typedef struct card |
10 | { |
11 | bool flipped; |
12 | int type; |
13 | }CARD; |
14 | |
15 | int 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.
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!
There is a ton of ways to do it but here is my example:
1 | void 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.
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?!
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.
luvzjetz16, maybe you should post the code YOU have written so far. In your first post you say
So far I have created the game screen with all of the cards on it.
Could you post that code here?
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")
Use the code mockup tags please. Have you ever heard of arrays?
yeah i've heard of arrays. what are mockup tags?
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.
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:
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.
is this better?!
1 | #include <allegro.h> |
2 | |
3 | void init(); |
4 | void deinit(); |
5 | |
6 | int main() { |
7 | init(); |
8 | |
9 | //before the game page there are two other pages that I have no problem with |
10 | |
11 | //game page |
12 | install_mouse(); |
13 | show_mouse(screen); |
14 | int c; |
15 | |
16 | BITMAP *img3, *cursor, *cursor2, *cursor3, *cursor4, *cursor5, *cursor6; |
17 | |
18 | img3 = load_bmp("c:\\familyguylogo.bmp",NULL); |
19 | cursor = load_bmp("c:\\PETER.bmp",NULL); |
20 | cursor2 = load_bmp("c:\\LOIS.bmp",NULL); |
21 | //etc..... |
22 | |
23 | blit(img3, screen, 0,0,0,0,640,480); |
24 | stretch_blit(img3, screen, 0,0, img3->w, img3->h, 0,0, SCREEN_W, SCREEN_H); |
25 | |
26 | draw_sprite(screen, cursor, 50, 75); |
27 | draw_sprite(screen, cursor2, 50, 200); |
28 | draw_sprite(screen, cursor3, 195, 75); |
29 | draw_sprite(screen, cursor4, 195, 200); |
30 | draw_sprite(screen, cursor5, 337, 75); |
31 | draw_sprite(screen, cursor6, 337, 200); |
32 | draw_sprite(screen, cursor, 480, 75); |
33 | draw_sprite(screen, cursor3, 480, 200); |
34 | draw_sprite(screen, cursor3, 50, 325); |
35 | draw_sprite(screen, cursor4, 195, 325); |
36 | draw_sprite(screen, cursor5, 337, 325); |
37 | draw_sprite(screen, cursor6, 480, 325); |
38 | |
39 | for(c=0;c<2000000000;c++); |
40 | system("cls") |
Yes, nothing after system("cls") though? I don't know how you managed to get that to compile.
sorry it didnt copy and paste here it is:
1 | deinit(); |
2 | return 0; |
3 | } |
4 | END_OF_MAIN() |
5 | |
6 | void 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 | |
23 | void deinit() { |
24 | clear_keybuf(); |
25 | } |
this part is created for me when I opened an allegro C project in DEV C++
Read Simons post, he said it best. I'm not sure I can help you much more.
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.
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?
(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.
1 | if (card1_flipped == 0) |
2 | { |
3 | draw_sprite(screen,flipped,..,..); |
4 | } |
5 | else |
6 | { |
7 | draw_sprite(screen,card1,..,..); |
8 | } |
9 | if (card2_flipped == 0) |
10 | { |
11 | draw_sprite(screen,flipped,..,..); |
12 | } |
13 | else |
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.
The idea is to write functions to break up the work for you. You should have an initialization function, a draw function, and a function to get which card was clicked. Breaking it up this way makes it simpler for your main game loop. If you've done C for a year then you should know that. As far as Allegro goes please read up on many of its functions and look at all of the examples. Once you understand them then you can work on a game. You have the stuff on the screen already but you need to understand come simple concepts, for example you need a main game loop, once you initialize your cards then you go into a continual loop until the user exits the game (usually hitting the ESC key). You also need to understand double buffering (creating a separate off screen drawing space for drawing) which will allow you to do animations. Don't jump in so fast, make some simple double buffering apps, tie one of your bitmaps to the mouse movement for example. Move up from there.
@ Simon... dear god don't teach him to write a bunch of if statements like that, that's horrible coding techniques.
For one thing you are going to want randomize where the cards are. The way you have it with drawing the cards, its going to be the exact same game every time. It will be a true memory game. The player plays the game once and memorizes where all the cards were at and can beat it every time with no mistakes.
As for flipping to cards...
Here is some pseudo code:
if num_cards_clicked equals 2
:if the two cards match
::set flag "matched" to true for both cards
:num_cards_clicked set equal to 0
:end if
end if
For drawing purposes you would check to see if the flag "matched" was set to true, if it was then draw the actual card instead of the default face down card.
Each time a card is clicked there will be another flag set, something around the lines of "selected" if selected is true then this card will also be drawn face up.
The main draw loop will check to see if the flags "matched" or "selected" are set, if either one is set then it will draw the actual card, if neither are then it will draw a generic face-down card graphic.
For the clicking on a card section you will need to check if the card is not "matched" or "selected" if it isn't either of those then it can be "selected"
Once 2 cards have been selected and you are checking to see if they are matched... You are then going to want to set the "selected" flag on both back to false.
If you can't understand C programming syntax and logic you can PM me for my AIM sn. I'm not doing too much in the next couple of days >.<:-[
@ Simon... dear god don't teach him to write a bunch of if statements like that, that's horrible coding techniques.
It's just an example so he sees that the simple (in the sense of "not complicated") way isn't always the right one. And besides that, if I showed him some sophisticated code with "advanced" features like sub-functions arrays and structs he wouldn't have understood.
ok first off everyone...i'm a girl so you dont have to call me him or he...haha
and second....ok Matthew Dalrymple I get what you are saying...however I don't really know how to "click" ok a certain box. I've tried mouse_x, mouse_y but I dont think I was doin it right...what kind of C code would I need to actually click on a "card"....if I can get this then I think everything else would be alot easier for me!!
I've tried mouse_x, mouse_y but I dont think I was doin it right...what kind of C code would I need to actually click on a "card"
Each card should have the location of the top left corner (x and y) and size (width and height). The mouse cursor is "on-top" of a card when mouse_x is between x and x+width AND mouse_y is between y and y+height. The left mouse button is clicked when mouse_b&1 is true. In code (pseudocode):
1 | //... somewhere in the init stage ... |
2 | bool mouse_down = false; |
3 | |
4 | //... later in the main loop ... |
5 | if (mouse_b&1) { |
6 | // make sure only one click is registered |
7 | if (!mouse_down) { |
8 | mouse_down = true; |
9 | |
10 | // save mouse_x and mouse_y to temp variables just in case mouse moves |
11 | // while the following code is executing |
12 | int mx = mouse_x; |
13 | int my = mouse_y; |
14 | for (each card) { |
15 | if (mx > card<i>.x && mx < card<i>.x + card<i>.width && my > card<i>.y && my < card<i>.y + card<i>.height) { |
16 | // card<i> has been clicked, so go do something about it! |
17 | // ... |
18 | // ... |
19 | } |
20 | } |
21 | } |
22 | } |
23 | else { |
24 | mouse_down = false; |
25 | } |
The series of game tutorials on my homepage might prove useful to you.
http://www.reflectedgames.com/create.html
You really should get a C book and make sure you are perfectly comfortable with loops, if's, switches, structs, functions, pointers, dynamic memory allocation and the operators (+, -, =, ==, *, /, ++,-- etc). Some basic knowledge of the standard library would not hurt either. Make some dull "text-only" programs before you start playing with the beast that is graphical programing
But don't worry about it; C is a very simply language (those parts mentioned above is pretty much it, with the exception of function pointers and unions iirc), and im sure you already know most of the parts mentioned above.
Well, good luck and have fun!
another girl on A.cc... aren't there like 5 of them now? I hope most of the code given here helps you. As for Mirans post that is correct for selecting the card, again you will have to change your card structure to contain an x, y, width, height, and selected attribute. Those values should also be initialized in the init_cards function.
ok guys....everytime i run my program it tells me: 'bool' is undeclared. what exactly is bool? i know int, double, float, char....is bool like those or what is it? and how can i "declare" it so I can run my program?
Sorry, bool is a type found only in C++ but not C. In plain C you can replace bool with int, true with 1 and false with 0.
ok cool...that worked. ok now for the line:
for (each card) { if (mx > card<i>.x && mx < card<i>.x + card<i>.width && my > card<i>.y && my < card<i>.y + card<i>.height) {
am i supposed to put like
for (card[0]) { if (mx > card[0].x && mx < card[0].x + card[0].width && my > card[0].y && my < card[0].y + card[0].height) {
or what should i be putting in the for( );
or what should i be putting in the for( );
You really should read a C tutorial or book first. Assuming you have an array of cards defined as something like CARD cards[52]; then you would make your for loop like this:
int i; for (i=0; i<52; i++) { /* ...the rest of the code where you use i as an index into the array of cards... */ }
well thank you all for your help....and thanks for putting up with me!!!;D
From personal experience I disagree with reading books. I once tried to learn a language by reading a book and I hated it. What you should do is read through very simple programs that other people have made and then try and understand what some of it does. You won't understand it all at first. Then start making your own simple program and slowly take features from other programs until you are competent enough to write something on your own. Then you can start making more advanced stuff. That's much faster than reading books.
Miran said:
....
Hmmm, the op says she's a girl and all of a sudden Miran is helpful.
I see how it is around here....
People lie about their gender to get a better treatment?
I know I mixed C and C++, sorry about that. Bool is short for boolean, with two values, true and false. Use a for loop like I used in the init function:
for(int i = 0; i < NUM_CARDS * 2; i++) // do something with card<i>
You can use bool on C99, you just have to include stdbool.h or something like that.
Hmmm, the op says she's a girl and all of a sudden Miran is helpful.
I see how it is around here....
Sex change, anyone? Does Mian's avatar have a slight smooth look in the eye now?
@ James Stanley: Text books give you the what, why, how and the when not to explanations. They give an understanding behind the information that is given. Reading other people's code, unless it's really, really well documented, won't. But trawling through other's code is a great way to learn about [various] implementions, usages and, in cases, more advanced techniques.
Hmmm, the op says she's a girl and all of a sudden Miran is helpful.
I've been helpful from the very beginning.
...a slight smooth look in the eye now?
Huh?
Smooth as in suave.
For beginners, I'd recommend learning Java instead of C... But oh well.
For beginners, I'd recommend trying different language and learn the one they understand better than the others.
But ... Heh , well ;-)
cough Game Creation Articles..