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