Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Lost on making a simple rock-paper-scissors game

Author
Message
ryuhipowes
14
Years of Service
User Offline
Joined: 4th May 2010
Location:
Posted: 5th May 2010 02:08
I've been trying to make a game that will play a game of rock paper scissors against the computer but i do not know enough to get it close to working. I am trying to get the game to accept a user clicking on one of the sprite images for rock paper or scissors(each having a value assigned to them) and then playing against the computer which randomly chooses a value to play against. I want the program to be a best out of five, but i cannot even get it running to see if i can get it to work one time, yet alone five. I'm sure there are alot of holes in my code, but i could use some help.




#include "DarkGDK.h"
int choice1;
int choice2;
const int Rate = 30;
void setUp();
void displayIntro();
void DetermineWinner(int, int);
bool onSprite(int, int, int);
bool mouseFullClick(int &x, int &y);
void DisplayRPS()
{
dbLoadImage("paper.bmp",1);
dbLoadImage("rock.bmp",2);
dbLoadImage("scissors.bmp",3);

dbSprite(1,200,200,1);
dbSprite(2,300,200,2);
dbSprite(3,400,200,3);
}
int PlayerChoice()
{
int mouseX, mouseY;
int choice1;
if(mouseFullClick(mouseX,mouseY))
{
if(onSprite(1,mouseX,mouseY))
{
choice1 = 0;
}
if(onSprite(1,mouseX,mouseY))
{
choice1 = 1;
}
if(onSprite(1,mouseX,mouseY))
{
choice1 = 2;
}
}

return choice1;
}
int ComputerChoice()
{
int choice2;
dbRandomize(dbTimer());
choice2 = dbRND(2);

return choice2;


}
void DetermineWinner(int choice1, int choice2)
{
int cpupoints = 0;
int playerpoints = 0;
dbLoadImage("win.bmp",5);
dbLoadImage("lose.bmp",6);
dbLoadImage("tie.bmp",7);
if(choice1 ==0)
{
if(choice2 ==0)
{
dbPasteImage(7,300,300);
}
else if(choice2 == 1)
{
dbPasteImage(6,300,300);
cpupoints+1;
}
else if (choice2 == 2)
{
dbPasteImage(5,300,300);
playerpoints+1;
}
}
if(choice1 == 1)
{
if(choice2 ==0)
{
dbPasteImage(5,300,300);
playerpoints+1;
}
else if(choice2 == 1)
{
dbPasteImage(7,300,300);
}
else if (choice2 == 2)
{
dbPasteImage(6,300,300);
cpupoints+1;
}
}
if(choice1 == 2)
{
if(choice2 ==0)
{
dbPasteImage(6,300,300);
cpupoints+1;
}
else if(choice2 == 1)
{
dbPasteImage(5,300,300);
playerpoints+1;
}
else if (choice2 == 2)
{
dbPasteImage(7,300,300);
}
}
if(playerpoints > cpupoints)
{
dbPrint("You Win!");
}
else if(playerpoints < cpupoints)
{
dbPrint("You Lose");
}
if(playerpoints = cpupoints)
{
dbPrint("Its a Tie");
}

}
void DarkGDK()
{
bool stillPlaying = true;
setUp();
while(LoopGDK() && stillPlaying)
{
DisplayRPS();
PlayerChoice();
ComputerChoice();


DetermineWinner(choice1, choice2);


dbSync();
}
}
void setUp()
{
displayIntro();
dbSyncOn();
dbSyncRate(Rate);
}
void displayIntro()
{ dbLoadImage("title_screen.bmp",4);
dbPasteImage(4,0,0);
dbWaitKey();
}
bool mouseFullClick(int &x, int &y)
{
bool buttonClick = false;
if (dbMouseClick() ==1)
{
x = dbMouseX();
y = dbMouseY();
while(dbMouseClick() == 1)
{
}
buttonClick = true;
}
return buttonClick;
}
bool onSprite(int spriteNum, int pointX, int pointY)
{
bool insideSprite;
int upperX = dbSpriteX(spriteNum) - dbSpriteOffsetX(spriteNum);
int upperY = dbSpriteY(spriteNum) - dbSpriteOffsetY(spriteNum);
int lowerX = upperX + dbSpriteWidth(spriteNum);
int lowerY = upperY + dbSpriteHeight(spriteNum);

if(pointX >= upperX && pointY >= upperY && pointX <= lowerX && pointY <= lowerY)
{
insideSprite = true;
}
else
{
insideSprite = false;
}
return insideSprite;
}
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 7th May 2010 20:42 Edited at: 7th May 2010 21:40
It took two days until I had some time to look through your program. I'd like to give you some advice. The code is not bad as a first attempt, you obviously studied C++ but there are a few misunderstandings.

First a forum advice: next time, use the code button above the text area to enclose the program text in code tags, before and after. It will show a nice button to open and hide the code and it will also preserve the indentation, so it's easier to read. Now onto the programming advice.

You do not handle the return values of functions properly (for example choice1, choice2). The returned value of a function will not get into a global variable by itself. Your main game loop looks like this:



Although the PlayerChoice and ComputerChoice functions are executed, you don't do anything with their return value. You should do this instead:



See how the returned values are assigned to the global variables? Without these, the global choice1 and choice2 will always be zero (or in the worse case, heaven knows what value, because they are not even initialized).

DetermineWinner function: you made a mistake everywhere in the syntax of incrementing a variable. It is NOT:
but correctly:
so the equal sign is missing. Without that, it works like your function calls: executed but doesn't store the result back into the variable, so it doesn't increment anything.

In the same function, you made a syntax error in the equality check:
This must be double equal sign:
otherwise the playerpoints variable will be MADE equal to cpupoints, not only tested and the test will always be true...

The large nested "if" statement is not bad in itself, but a switch statement would be more elegant and readable.

PlayerChoice function: in all three tests, you give the same sprite number, so you are always testing the first sprite...

onSprite function: not bad, although I don't understand why you included the dbSpriteOffset if you haven't offset either of the sprites... But it would be simpler to use sprite collision. In games, it is customary to load a mouse cursor sprite which is moved together with the mouse and then you can use dbSpriteCollision to test whether this mouse sprite collided with any of the others. It returns the index of the clicked sprite and it is simpler than using coordinate calculations. But the coordinate method should work as well, as soon as you get the rest of the program working.

General game design: Do not reload images in every loop. You use dbLoadImage in functions which are called repeatedly from the main game loop, with the result that the program loads these images every time again from disk. It can even create a memory leak, I don't know the behaviour of Dark GDK if you reload an image over an existing one... You should load images once in the game setup and then use only the dbSprite commands to position the sprites where you need them. In fact, if the position of the sprites does not change, even the dbSprite command is enough once at the beginning because the screen update dbSync() does not erase sprites from the screen, as it erases text and drawing.

Likewise, the dbRandomize command should be called only once. Although with the timer as the seed, it will not repeat the random numbers, but it is really unnecessary to call it several times. Do this in the game setup.

If the dbRandomize command is replaced into the game setup part, actually one line can replace your whole ComputerChoice function:



displayIntro function: when the title screen is not necessary any more, the image could be deleted from memory. Also, it is good practice to delete the used images and sprites when the program is finished, although Windows should clean up after your program but it's good to get used to deleting the resources when they are no longer needed.

mouseFullClick function: I see you already know that the program should detect when the mouse button is released, because dbMouseClick returns true as long as the button is held down. This is a possible way to do it. I'd just like to mention that this way, the user can freeze the program by holding down the mouse button, because as long as the while loop is running, the screen is not updated. Usually a game wants to remain moving whatever the player is doing, so it would be better to use a static variable to keep track of the status of the mouse button and get rid of the while loop. I'll write an example next time if you are interested, but I guess this won't cause a problem for now.

These are the comments I can give you about the program. With just a little tweaking and correcting, you should be able to get it working nicely. Good luck and don't give up!

Login to post a reply

Server time is: 2024-07-07 01:36:22
Your offset time is: 2024-07-07 01:36:22