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 / Scoring System

Author
Message
Oster200
13
Years of Service
User Offline
Joined: 21st Jan 2011
Location:
Posted: 30th Apr 2011 00:16
Okay well i just quickly went back to my pong game and i wanted to add a scoring system this is usally easy well i first checked it in the while loop and it worked fine. here is the code that i tested in the while loop

i changed the scoring system to this

then this is my while loop now

but when the ball touches the side it deletes like it supposed to score goes up once and then goes don exatly same time i hope i made sense of this.
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 30th Apr 2011 00:45
This
should probably be


Also, move the dbCLS() and dbSync() back out to the LoopGDK() loop (you currently have dbSync() commented out - uncomment it and put the dbCLS() after that) and remove them from the player.score()

You probably don't want to dbDeleteSprite(ballID) and should instead use dbHideSprite(ballID). I'm assuming that ball.reset() re-creates the ballID sprite? If so, have it position the sprite where you want it and re-show the sprite. Creating resources is slow - and doing it every frame will kill your performance.

Beyond that, without seeing the rest of the code, there really isn't much more that I can suggest.

Regards,
JTK
Oster200
13
Years of Service
User Offline
Joined: 21st Jan 2011
Location:
Posted: 30th Apr 2011 06:20 Edited at: 30th Apr 2011 06:27
Sorry the code button is not working i dont know if it just me or what but i tried to fix it twice both failed most likly it is me but any ways...

Main.cpp

#include"darkGDK.h"
#include"ball.h"
#include"player1.h"
#include"player2.h"

void DarkGDK(void)
{
dbSyncOn();
dbSyncRate(60);
dbDisableEscapeKey();

dbSetImageColorKey(255,0,255);

dbLoadImage("background.bmp",1);
dbLoadImage("paddle.bmp",2);
dbLoadImage("ball.bmp",3);
dbLoadImage("topbottomline.bmp",4);
dbLoadImage("leftrightline.bmp",5);
dbLoadImage("middleline.bmp",6);


dbSprite(1,0,0,1);//background
dbHideSprite(1);
dbSprite(2,100,220,2);//player1 paddle
dbSprite(3,540,220,2);//player2 paddle


//ball == id 4

dbSprite(10,0,0,4);//line runs at top left to right
dbSprite(11,0,0,5);//line runs top to bottom on left side
dbSprite(12,635,0,5);//line top to bottom right side
dbSprite(13,0,475,4);// line runs left to right at the bottom
dbSprite(14,317,0,6);//middle line

dbText(100,100,"hello");


player1 player1;
player2 player2;
ball ball;
char text[12];
int score = 0;

while(LoopGDK())
{

dbPasteImage(1,0,0);
player1.score();
player1.movement();
player1.hittingtheball();
player2.movement();
player2.hittingtheball();

ball.reset();
ball.movement();
//ball.goal_check();
ball.collision();

if(dbEscapeKey() == 1)
{
break;
}
dbSync();
dbCLS();
}

for( int i = 1; i < 10; i++)
{
dbDeleteImage(i);

dbDeleteSprite(i);
}

return;
}


player.h
class player1
{
public:
player1();
~player1();

int movement();
int hittingtheball();
int score();
private:
};

player1.cpp
#include"darkGDK.h"
#include"player1.h"

#define plr1 2
#define ballID 4

player1::player1()
{

}
player1::~player1()
{

}


int player1::movement()
{
if(dbKeyState(17) == 1 && dbSpriteCollision(plr1,10) == 0)
{
dbMoveSprite(plr1,3);
}
if(dbKeyState(31) == 1 && dbSpriteCollision(plr1,13) == 0)
{
dbMoveSprite(plr1,- 3);
}
return(0);
}

int player1::score()
{
char text[12];
int score = 0;
if((dbSpriteX(ballID) < 0 && dbSpriteExist(ballID))) //||dbSpriteX(ballID) > 640) && dbSpriteExist(ballID))
{
score += 1;
dbDeleteSprite(ballID);
//score += 1;
//dbWait(200);
}



sprintf(text, "Score: %i", score);
dbText(10, 10, text);


return(0);
}



int player1::hittingtheball()
{
if(dbSpriteCollision(plr1,ballID) == 1)
{
if((dbSpriteHeight(plr1) / 2) + dbSpriteY(plr1) > (dbSpriteHeight(ballID) / 2) + dbSpriteY(ballID))
{
dbRotateSprite(ballID,45);
}
if((dbSpriteHeight(plr1) / 2) + dbSpriteY(plr1) < (dbSpriteHeight(ballID) / 2) + dbSpriteY(ballID))
{
dbRotateSprite(ballID,135);
}
if((dbSpriteHeight(plr1) / 2) + dbSpriteY(plr1) == (dbSpriteHeight(ballID) / 2) + dbSpriteY(ballID))
{
dbRotateSprite(ballID,90);
}
}

return(0);
}

player2.h
class player2
{
public:
player2();
~player2();

int movement();
int hittingtheball();
private:
};

player2.cpp
#include"darkGDK.h"
#include"player2.h"

#define plr2 3
#define ballID 4

player2::player2()
{

}
player2::~player2()
{

}

int player2::movement()
{
if(dbKeyState(23) == 1 && dbSpriteCollision(plr2,10) == 0)
{
dbMoveSprite(plr2,3);
}
if(dbKeyState(37) == 1 && dbSpriteCollision(plr2,13) == 0)
{
dbMoveSprite(plr2,- 3);
}
return(0);
}

int player2::hittingtheball()
{
if(dbSpriteCollision(plr2,ballID) == 1)
{
if((dbSpriteHeight(plr2) / 2) + dbSpriteY(plr2) > (dbSpriteHeight(ballID) / 2) + dbSpriteY(ballID))
{
dbRotateSprite(ballID,315);
}
if((dbSpriteHeight(plr2) / 2) + dbSpriteY(plr2) < (dbSpriteHeight(ballID) / 2) + dbSpriteY(ballID))
{
dbRotateSprite(ballID,225);
}
if((dbSpriteHeight(plr2) / 2) + dbSpriteY(plr2) == (dbSpriteHeight(ballID) / 2) + dbSpriteY(ballID))
{
dbRotateSprite(ballID,270);
}
}
return(0);
}

ball.h
#pragma once
//extern int plr1_score;
//extern int plr2_score;
//extern int g_iPlayerScore;
class ball
{
public:
ball();
~ball();

int movement();
int collision();
int reset();
int scores();
//int goal_check();


private:
};

#include"darkGDK.h"
#include"ball.h"
#include"text.h"

#define ballID 4


int plr1_score=0;
int plr2_score=0;
ball::ball()
{

}

ball::~ball()
{

}

int ball::reset()
{
if(dbSpaceKey() == 1 && dbSpriteExist(ballID) == 0)
{
dbSprite(ballID,400,240,3);
dbOffsetSprite(ballID,10,10);
dbRotateSprite(ballID,270);
}
return(0);
}

int ball::movement()
{
if(dbSpriteExist(ballID)==1)
{
dbMoveSprite(ballID,7);
}
return(0);
}

/*int ball::goal_check()
{
if((dbSpriteX(ballID) < 0 && dbSpriteExist(ballID))) //||dbSpriteX(ballID) > 640) && dbSpriteExist(ballID))
{
dbDeleteSprite(ballID);
PongTextClear();
char czScore[20];
sprintf ( czScore , "%d" , plr1_score );
PongText ( 0 , 730 , 32 , czScore , false , false );
g_iPlayerScore += 100;

}
else
if((dbSpriteX(ballID) > 640 && dbSpriteExist(ballID)))//||dbSpriteX(ballID) > 640) && dbSpriteExist(ballID))
{
dbDeleteSprite(ballID);
PongTextClear();
char czScore2[20];
sprintf ( czScore2 , "%d" , plr2_score );
PongText ( 0 , 730 , 32 , czScore2 , false , false );
}
return(0);
}*/

int ball::collision()
{

if(dbSpriteCollision(ballID,10)==1)
{
if(dbSpriteAngle(ballID) == 45)
{
dbRotateSprite(ballID,135);
}
if(dbSpriteAngle(ballID) == 315)
{
dbRotateSprite(ballID,225);
}
}
if(dbSpriteCollision(ballID,13)==1)
{
if(dbSpriteAngle(ballID) == 225)
{
dbRotateSprite(ballID,315);
}
if(dbSpriteAngle(ballID) == 135)
{
dbRotateSprite(ballID,45);
}
}
return(0);
}

Login to post a reply

Server time is: 2024-11-19 10:30:54
Your offset time is: 2024-11-19 10:30:54