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!
Steve Terry
Member #1,989
March 2002
avatar

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.

___________________________________
[ 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

Matthew Dalrymple
Member #7,922
October 2006
avatar

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 >.<:-[

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

Simon Parzer
Member #3,330
March 2003
avatar

Quote:

@ 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.

luvzjetz16
Member #8,063
December 2006

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!!

miran
Member #2,407
June 2002

Quote:

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 ...
2bool mouse_down = false;
3 
4//... later in the main loop ...
5if (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}
23else {
24 mouse_down = false;
25}

--
sig used to be here

Richard Phipps
Member #1,632
November 2001
avatar

The series of game tutorials on my homepage might prove useful to you.
http://www.reflectedgames.com/create.html

Jonatan Hedborg
Member #4,886
July 2004
avatar

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!

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

Steve Terry
Member #1,989
March 2002
avatar

:o 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.

___________________________________
[ 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....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?

miran
Member #2,407
June 2002

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.

--
sig used to be here

luvzjetz16
Member #8,063
December 2006

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( );

miran
Member #2,407
June 2002

Quote:

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... */
}

--
sig used to be here

luvzjetz16
Member #8,063
December 2006

well thank you all for your help....and thanks for putting up with me!!!;D

James Stanley
Member #7,275
May 2006
avatar

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.

nonnus29
Member #2,606
August 2002
avatar

Quote:

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....

;)

Jonatan Hedborg
Member #4,886
July 2004
avatar

People lie about their gender to get a better treatment?

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

Steve Terry
Member #1,989
March 2002
avatar

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>

___________________________________
[ 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

BAF
Member #2,981
December 2002
avatar

You can use bool on C99, you just have to include stdbool.h or something like that.

HardTranceFan
Member #7,317
June 2006
avatar

nonnus29 said:


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.

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

miran
Member #2,407
June 2002

Quote:

Hmmm, the op says she's a girl and all of a sudden Miran is helpful.

I've been helpful from the very beginning. :)

Quote:

...a slight smooth look in the eye now?

Huh? ???

--
sig used to be here

HardTranceFan
Member #7,317
June 2006
avatar

Smooth as in suave.

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

Fladimir da Gorf
Member #1,565
October 2001
avatar

For beginners, I'd recommend learning Java instead of C... But oh well.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

GullRaDriel
Member #3,861
September 2003
avatar

For beginners, I'd recommend trying different language and learn the one they understand better than the others.

But ... Heh , well ;-)

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Richard Phipps
Member #1,632
November 2001
avatar

 1   2 


Go to: