Pong: Slow Down Game

Pong game SDK 2D

Page 1 of 1

5 Replies - 1998 Views - Last Post: 23 July 2009 - 02:21 PM Rate Topic: -----

#1 tdubs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-January 09

Pong: Slow Down Game

Posted 23 July 2009 - 12:14 PM

Hi my game is not working the way i want well i have the correct code for the while function but the loop is to fast. I'm trying to return the paddle to the original position to its half way point on the y axis. but when i do the loop is so fast i can't see the paddle return. I want to slow it down a bit. I tried the Sleep syntax but it's not working with the time.h file. Any ideas here is my code. (Look for the NEED WORK comment) Plus i tried it with system as well. by the way the game.h file includes the time.h file

// Beginning Game Programming, Second Edition
// Chapter 10
// Paddle_Game program source code file


#include "game.h"

//background image
LPDIRECT3DSURFACE9 back;

//sprite handler
LPD3DXSPRITE sprite_handler;

//ball sprite
LPDIRECT3DTEXTURE9 ball_image;
SPRITE ball;

//paddle sprite
LPDIRECT3DTEXTURE9 paddle_image;
SPRITE paddle;

//paddle sprite
LPDIRECT3DTEXTURE9 P2_image;
SPRITE P2;

//the wave sound
CSound *sound_bounce;
CSound *sound_hit;

//misc
long start = GetTickCount();
HRESULT result;


//initializes the game
int Game_Init(HWND hwnd)
{
	//set random number seed
	srand(time(NULL));

    //initialize mouse
    if (!Init_Mouse(hwnd))
    {
        MessageBox(hwnd, "Error initializing the mouse", "Error", MB_OK);
        return 0;
    }

    //initialize keyboard
    if (!Init_Keyboard(hwnd))
    {
        MessageBox(hwnd, "Error initializing the keyboard", "Error", MB_OK);
        return 0;
    }

    //create sprite handler object
    result = D3DXCreateSprite(d3ddev, &sprite_handler);
    if (result != D3D_OK)
        return 0;

    //load the background image
    back = LoadSurface("pongboard.png", NULL);
    if (back == NULL)
        return 0;

    //load the ball sprite
    ball_image = LoadTexture("ball.bmp", D3DCOLOR_XRGB(255,0,255));
    if (ball_image == NULL)
        return 0;

    //set the ball's properties**************************Change ball.movex and ball.movey to make move more horizontal//Tom
    ball.x = 400;
    ball.y = 200;
    ball.width = 12;
    ball.height = 12;
    ball.movex = 10;
    ball.movey = -10;

    //load the paddle sprite
    paddle_image = LoadTexture("paddle.bmp", D3DCOLOR_XRGB(255,0,255));
    if (paddle_image == NULL)
        return 0;

	//Load second paddle//S.Zielinski
	P2_image = LoadTexture("P2.bmp", D3DCOLOR_XRGB(255,0,255));
    if (paddle_image == NULL)
        return 0;

    //set paddle properties**********************Reverse paddle.width and paddle.height to match dimensions of sprite//Tom
    paddle.x = 760;
    paddle.y = SCREEN_HEIGHT - 300;
    paddle.width = 26;
    paddle.height = 90;

	//Set second paddle properties ****************************** S.Zielinski
	P2.x = 10;
    P2.y = SCREEN_HEIGHT - 300;
    P2.width = 26;
    P2.height = 90;

    //load bounce wave file
    sound_bounce = LoadSound("bounce.wav");
    if (sound_bounce == NULL)
        return 0;

    //load the hit wave file
    sound_hit = LoadSound("hit.wav");
    if (sound_hit == NULL)
        return 0;

    //return okay
    return 1;
}

int Collision(SPRITE sprite1, SPRITE sprite2)
{
    RECT rect1;
    rect1.left = sprite1.x+1;
    rect1.top = sprite1.y+1;
    rect1.right = sprite1.x + sprite1.width-1;
    rect1.bottom = sprite1.y + sprite1.height-1;

    RECT rect2;
    rect2.left = sprite2.x+1;
    rect2.top = sprite2.y+1;
    rect2.right = sprite2.x + sprite2.width-1;
    rect2.bottom = sprite2.y + sprite2.height-1;

    RECT dest;
    return IntersectRect(&dest, &rect1, &rect2);
}

//the main game loop
void Game_Run(HWND hwnd)
{
    //ball position vector
    D3DXVECTOR3 position(0,0,0);   

    //make sure the Direct3D device is valid
    if (d3ddev == NULL)
        return;

    //update mouse and keyboard
    Poll_Mouse();
    Poll_Keyboard();

    //after short delay, ready for next frame?
    //this keeps the game running at a steady frame rate
    if (GetTickCount() - start >= 30)
    {
        //reset timing
        start = GetTickCount();

        //move the ball sprite
        ball.x += ball.movex;
        ball.y += ball.movey;

        //bounce the ball at screen edges
        if (ball.x > SCREEN_WIDTH - ball.width)
        {
            ball.x -= ball.width;
            ball.movex *= -1;
            PlaySound(sound_bounce);
        }
        else if (ball.x < 0)
        {
            ball.x += ball.width;
            ball.movex *= -1;
            PlaySound(sound_bounce);
        }

        if (ball.y > SCREEN_HEIGHT - ball.height)
        {
            ball.y -= ball.height;
            ball.movey *= -1;
            PlaySound(sound_bounce);
        }
        else if (ball.y < 0)
        {
            ball.y += ball.height;
            ball.movey *= -1;
            PlaySound(sound_bounce);
        }

        //move the paddle*************************Changed x's to y's to recognize top and bottom of screen as limits//Rosalio
        paddle.y += Mouse_X();
        if (paddle.y > SCREEN_HEIGHT - paddle.height)
            paddle.y = SCREEN_HEIGHT - paddle.height;
        else if (paddle.y < 0)
            paddle.y = 0;
		
		//move second paddle ****************************** S.Zielinski
		P2.y += Mouse_X();
        if (P2.y > SCREEN_HEIGHT - P2.height)
            P2.y = SCREEN_HEIGHT - P2.height;
        else if (P2.y < 0)
            P2.y = 0;

        //check for left arrow*******************Sped up and up instead of left 
      //  if (Key_Down(DIK_UP))
        //    paddle.y -= 15;

        //check for right arrow******************Sped up and down instead of right
        //if (Key_Down(DIK_DOWN))
         //   paddle.y += 15;

		//check for W key for up
		if (Key_Down(DIK_W))
            P2.y -= 15;

		//check for S key for down
		if (Key_Down(DIK_S))
            P2.y += 15;

		//if (ball.x > 0 && ball.y > 250)         ///////////////NEED WORK 
		//	paddle.y = ball.y;

//		if(ball.x > 0 && ball.y < 250)
//			paddle.y = ball.y;
		//if(ball.x <400)
			
			while(paddle.y < 200 && ball.x < 400)
			{	
				paddle.y++;
				//Sleep(5000);        
				
			}
			while(paddle.y > 200 && ball.x < 400)
			{	
				paddle.y--;
				//system("Sleep 5000");
			}
		
		if (ball.x > 400) // check to see if ball went pass the center line on x 
		{
			
			

			if ( ball.y < 200)     // check to see if ball is in upper quadrant
			{
				 // assign paddle to ball 
				paddle.y = ball.y;
				
			}

			if ( ball.y > 200)		// check to see if ball is in lower quadrant 
			{
				
				paddle.y = ball.y;
			}
		//	if (CurrentPPos <= 300)
		
			
		}
		 

        //see if ball hit the paddle
        if (Collision(paddle, ball)|| Collision(P2, ball))
        {
			//**********************************Changed y's to x's to recognize side of paddle instead of top and sides//Rosalio
            ball.x -= ball.movex;
            ball.movex *= -1;
            PlaySound(sound_hit);
        }
		if (Collision(paddle, ball)|| Collision(P2, ball))
        {
			//**********************************Original collision for ball
            ball.y -= ball.movey;
            ball.movey *= -1;
            PlaySound(sound_hit);
        }
        
		
   }

    //start rendering
    if (d3ddev->BeginScene())
    {
        //erase the entire background
        d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);

        //start sprite handler
        sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);

        //draw the ball
        position.x = (float)ball.x;
        position.y = (float)ball.y;
        sprite_handler->Draw(
            ball_image, 
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));

        //draw the paddle
        position.x = (float)paddle.x;
        position.y = (float)paddle.y;
        sprite_handler->Draw(
            paddle_image,
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));

		//draw second paddle
		position.x = (float)P2.x;
        position.y = (float)P2.y;
        sprite_handler->Draw(
            P2_image,
            NULL,
            NULL,
            &position,
            D3DCOLOR_XRGB(255,255,255));


        //stop drawing
        sprite_handler->End();
       
        //stop rendering
        d3ddev->EndScene();
    }

    //display the back buffer on the screen
    d3ddev->Present(NULL, NULL, NULL, NULL);

    //check for mouse button (to exit program)
    if (Mouse_Button(0))
        PostMessage(hwnd, WM_DESTROY, 0, 0);

    //check for escape key (to exit program)
    if (Key_Down(DIK_ESCAPE))
        PostMessage(hwnd, WM_DESTROY, 0, 0);
}

//frees memory and cleans up before the game ends
void Game_End(HWND hwnd)
{

    if (ball_image != NULL)
        ball_image->Release();

    if (paddle_image != NULL)
        paddle_image->Release();

    if (back != NULL)
        back->Release();

    if (sprite_handler != NULL)
        sprite_handler->Release();

    //if (sound_bounce != NULL)
    //    delete sound_bounce;

    //if (sound_hit != NULL)
    //    delete sound_hit;


}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 23 July 2009 - 01:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pong: Slow Down Game

#2 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: Pong: Slow Down Game

Posted 23 July 2009 - 12:20 PM

TOO MUCH CODE TO HAVE TO REFORMAT BY HAND JUST TO READ IT!!!!

REPOST using indentation and tags!

Remove SOUND and other non-related code to reduce its size.

Have you heard of paddle acceleration! The longer you hold a paddle the faster it gets?

Also rip out your delta time using GetTickCount() it is too crude.
Use a delta time based upon floating-point 1.0 = 1 second.
And put a trap if delta time > 0.25 the delta time = 0.25
that way when single stepping in a debugger it won't scramble your time by making it TOO big due to the LONG delay.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5676
  • View blog
  • Posts: 22,537
  • Joined: 23-August 08

Re: Pong: Slow Down Game

Posted 23 July 2009 - 01:05 PM

I think I'll move this to to Game Programming
Was This Post Helpful? 0
  • +
  • -

#4 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: Pong: Slow Down Game

Posted 23 July 2009 - 01:19 PM

Okay, I was bored so started formating your code.

Several problems already!

Primarily
//   paddle.x  += Mouse_X();
//   paddle.y  += Mouse_Y();
  paddle.x  = Mouse_X();
   paddle.y  = Mouse_Y();


MOUSE X and Y are SCREEN POSITIONS!!!!!!
Not delta movements.
So you are adjusting your paddle positions by the screen positions.

Your collision detection. CALL your function CollisionDetection NOT Collision!!!!
You displace your sprite +1 left +1 down BUT You keep the rectangles of the two sprites the same size!

Why not keep rectangles in your sprite class so you don't have to keep spending wasted time converting from boxes to rectangles to do the comparison!

Also you are doing your collision detection OUTSIDE the screen! Do the math on paper and you will see what I'm talking about. They should bounce right at (0) and right at (WIDTH-1) or (HEIGHT-1).

This post has been edited by wildgoose: 23 July 2009 - 01:21 PM

Was This Post Helpful? 0
  • +
  • -

#5 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Pong: Slow Down Game

Posted 23 July 2009 - 01:57 PM

You'll want to calculate a delta value:
delta = timeNow - timeLast;



Then you move like so:
y += moveSpeed * delta / 1000.f;


Was This Post Helpful? 0
  • +
  • -

#6 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: Pong: Slow Down Game

Posted 23 July 2009 - 02:21 PM

Yes, that too! As e_barroga said. You need to tie the player motion and sprite motion into the time base per frame. Otherwise on a faster computer the game will run too quickly and not be playable, and on a slow computer, will move really slow.

In this way you can determine, "I want a paddle to move 30 pixels a second" so you calculate your time slice (delta time for that frame) and applie that ratio of time to the paddle movement speed to get the actual paddle movement, or sprite movement, etc.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1