InitBG();
DrawBG();
int done=0;
while(done == 0)
{
int ctr;
int card; // stores the player's card values
int gameover = 0;
/* if equals 1, player went over 21, dealer wins
if equals 2, dealer went over 21, player wins
if equals 3, player has higher score, player wins
if equals 4, dealer has higher score, dealer wins
if equals 5, player and dealer are tied, neither win */
int score = 0;
int dscore = 0;
// shuffles deck, if neccessary
deck.rebuild();
for(ctr = 0; ctr < 2; ctr++)
{
card = deck.dealcard(); // deals a card to the player
player.getCard(card);
DrawScene();
card = deck.dealcard(); // deals a card to the deal
dealer.getCard(card);
DrawScene();
}
DrawScene();
score = player.getScore();
dscore = dealer.getScore();
while (( player.showHand() < 4 ) && ( endhand == 0 ) && gameover == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; endhand = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; endhand = 1;}
}
}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_y] )
{
card = deck.dealcard();
player.getCard(card);
DrawScene();
score = player.getScore();
if ( score > 21 ) { gameover = 1; }
dealerDraw(card, score, dscore, gameover);
}
if ( keys[SDLK_n] )
{
dealerDraw(card, score, dscore, gameover);
}
}
/*
if ( gameover == 0 )
{
if ( score > dscore )
{
gameover = 3;
}
if ( score < dscore )
{
gameover = 4;
}
if ( score == dscore )
{
gameover = 5;
}
}
endHand(gameover);
*/
DrawScene();
}
Blackjack using SDL, need help
I'm having a bit of trouble writing a blackjack game with SDL. I got it to deal out the cards at the beginning, but when I hit y to get another card, it deals out 4 cards. Can anyone help me figure this out? Here's my loop
[edited by - MetroidHunter on February 4, 2003 5:56:55 PM]
I don''t know SDL at all, so this is just a guess.
I''m willing to bet that your problem is with the following piece of code:
if ( keys[SDLK_y] )
{...}
you say that it draws 4 cards after you press y, so I think that when your program loops through again keys[SDLK_y] is still true. Try keys[SDLK_y] = 0; somewhere at the end of that piece of code.
I''m willing to bet that your problem is with the following piece of code:
if ( keys[SDLK_y] )
{...}
you say that it draws 4 cards after you press y, so I think that when your program loops through again keys[SDLK_y] is still true. Try keys[SDLK_y] = 0; somewhere at the end of that piece of code.
boohillie is right in that your error lies in how you handle that key.
Using the syntax keys = SDL_GetKeyState(NULL) tells SDL that you want to check to see if the key is being held down. Since you use this it deals four cards because it reads that the key being held down even if pressed only once.
If you want to test if the key was pressed once use the following:
Note: It makes your event handling code more managable to use a case statement instead of ifs.
Using the syntax keys = SDL_GetKeyState(NULL) tells SDL that you want to check to see if the key is being held down. Since you use this it deals four cards because it reads that the key being held down even if pressed only once.
If you want to test if the key was pressed once use the following:
if ( event.key.keysym.sym == SDLK_y ) { // your code that does whatever needs doing when ''y'' is pressed }
Note: It makes your event handling code more managable to use a case statement instead of ifs.
okay, I''ll see if that does it. Thanks for all your help, I appreciate it. ^_^
It''s still not working right... once you press y or n, it starts playing automatically
I changed the innards of your poll even while loop :
while ( SDL_PollEvent(&event) ) { // poll even if ( event.type == SDL_QUIT ) { done = 1; endhand = 1; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; endhand = 1;} if ( event.key.keysum.sym == SDLK_y) { card = deck.dealcard(); player.getCard(card); DrawScene(); score = player.getScore(); if ( score > 21 ) { gameover = 1; } dealerDraw(card, score, dscore, gameover); } // SDLK_Y if ( event.key.keysum.sym == SDLK_n) dealerDraw(card, score, dscore, gameover); } // keydown } // poll event
Let me know how the game turns out. I have just started work on platform game in SDL. e-mail me at f_hilljr@yahoo.com
It works okay... I haven''t figured out how to get it to pause after a hand ends, and I''m having trouble with a piece of code in my DrawScene() function. It is supposed to draw the score and everything to the screen, but when it updates, it just keeps writing over it, and it gets jumbled up.
any ideas on how to fix this?
void DrawScene(){ player.displayCards(50,screen); dealer.displayCards(250,screen); drawString(screen,font,50,450,"Cash: $%.2f",funds); drawString(screen,font,5,475,"Won: %d Lost: %d Tied: %d", won, lost, tied); drawString(screen,font,50,500,"Result of Last Hand: %s", result); SDL_Flip(screen);}
any ideas on how to fix this?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement