• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Moving the ball slower.

## Recommended Posts

SSJCORY    100
I have my code and it is going to be pong. I made it so when you press up it releases the ball. But when It goes it goes so fast you can''t see it how do i slow it down. Here is the code:
#define WIN32_LEAN_AND_MEAN
#include<windows.h>
HDC hdc;
bool launched = false;
class CGame{
public:
CGame(){
Gameover = false;
}
bool Gameover;
}Game;

private:
int x;
int y;
int xchange;
int ychange;
public:
x = 320;
y = 435;
xchange = 0;
ychange = 0;
}
int getX(){
return x;
}
int getY(){
return y;
}
void setX(int newx){
x = newx;
}
void setY(int newy){
y = newy;
}
int getXChange(){
return xchange;
}
int getYChange(){
return ychange;
}
void setXChange(int change){
xchange = change;
}
void setYChange(int change){
ychange = change;
}
x += xchange;
y += ychange;
xchange = 0;
ychange = 0;
}
paddle.left = x - 20;
paddle.right = x + 20;
paddle.top = y + 5;
paddle.bottom = y - 5;
}
class CBall{
private:
int x;
int y;
int xchange;
int ychange;
public:
CBall(){
x = 320;
y = 435;
xchange = 0;
ychange = 0;
}
int getX(){
return x;
}
int getY(){
return y;
}
void setX(int newx){
x = newx;
}
void setY(int newy){
y = newy;
}
int getXChange(){
return xchange;
}
int getYChange(){
return ychange;
}
void setXChange(int change){
xchange = change;
}
void setYChange(int change){
ychange = change;
}
void moveBall(){
if(x + xchange > 0){
x += xchange;
}
if(y + ychange > 0){
y += ychange;
}
if(xchange + x < 0)
x += (xchange * -1);
if(ychange + y < 0){
y += (ychange * -1);
}
}
void drawBall(HBRUSH brush){
SelectObject(hdc,(HPEN)GetStockObject(BLACK_PEN));
SelectObject(hdc,brush);
Ellipse(hdc,x,y,x+10,y+10);
}
void launch(){
setYChange(-5);
launched = true;
}
}ball;
void Collision(){
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_DESTROY:
Game.Gameover = true;
break;
case WM_KEYDOWN:
switch(wParam){
case 37:
if(launched == false){
}
break;
case 38:
SelectObject(hdc,(HPEN)GetStockObject(WHITE_PEN));
SelectObject(hdc,(HBRUSH)GetStockObject(WHITE_BRUSH));
Ellipse(hdc,ball.getX(),ball.getY(),ball.getX()+10,ball.getY()+10);
ball.launch();
break;
case 39:
if(launched == false){
}
break;
}
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hp,PSTR CmdLine,int iCmd){
WNDCLASS wc;
HWND hwnd;
MSG msg;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "Class";
wc.style = NULL;
RegisterClass(&wc);
hwnd = CreateWindow("Class","GalagaMe",WS_OVERLAPPEDWINDOW,0,0,640,480,NULL,NULL,hInstance,NULL);
hdc = GetDC(hwnd);
ShowWindow(hwnd,iCmd);
UpdateWindow(hwnd);
while(!Game.Gameover){
if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(launched == true){
ball.moveBall();
ball.drawBall((HBRUSH)GetStockObject(BLACK_BRUSH));
}
Collision();
}
MessageBox(NULL,"Pretty cool eh?","GAMEOVER                 ",MB_OK);
return 0;
}

Thanks, Cory
Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

##### Share on other sites
Nytegard    839
You have pretty much 3 options that I know of for the moment. 2 Aren''t preferable (one of those is really not preferable).

Either:

A) Move by a smaller amount. Older games used to be based on the machine when drawing, but as you know, faster machines see things too quickly.

B) Put a timer in and only update the position on the timer event. This still isn''t too good incase you missed a timer, as you''ll get severe slowdown.

C) Put in a timer and update the position of the ball depending on how much time has gone by.

IE: If your ball travels 4 pixels per second, and the time thats gone by is 3.2 seconds, update it 12 pixels.

##### Share on other sites
SSJCORY    100
Any idea how to make a timer?

Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

##### Share on other sites
GeekPlusPlus    124
There are two popular ways to make a timer.

The one I see more often seems to be GetTickCount() which will give you the number of ticks since (I believe it''s when the computer was started).

The method I prefer is to use timeGetTime()

To use timeGetTime() you will need to link winmm.lib to your project. Also it''s best if you do timeBeginPeriod(1) in your initilization and timeEndPeriod(1) in your un-initilizing.

between these when you call timeGetTime() it will return a tick count that is VERY accurate. Where from what I have heard GetTickCount() can be anywehre from 10ms to 55ms off. But, with gettickcount you wno''t need to link winmm.lib, so it''s up to you.

If you are wondering how to use them here''s something... I don''t have any code with me right now so I''ll do my best here.
//you''ll have to fill in the ...s in yourself... WinMain(...){timeBeginPeriod(1)while(...){    if(timeGetTime() - LastTime >= TimeDifferenceYouWant)    {      LastTime = timeGetTime();      RunYourCode();    }}timeEndPeriod()}

So you can see it won''t run the iff until the time difference you want has passed. This is pretty much how you''d go about using the tick counter too.

- Newb Programmer: Geek++

##### Share on other sites
PhilVaz    144
<< I made it so when you press up it releases the ball. But when It goes it goes so fast you can''t see it how do I slow it down. >>

LaMothe''s method takes two lines (a GetTickCount and a While) and one variable (a DWORD). What you want to do is make the game go 30 frames per second. That way you can move the ball like 2 to 5 pixels a frame and it should be a good speed. The paddle can be updated with the mouse so paddle will move as fast as the mouse. To set your game at 30 frames per second, inside your message loop....

// get initial tick count to keep game speed constant
DWORD start_tick = GetTickCount();

// is there a message in queue, if so get it
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{ etc.... }

GameMain(); // call game processing function (updates 1 frame)

// wait until we hit correct game speed frame rate
while ((GetTickCount() - start_tick) < GAME_SPEED);

That''s it. The semi-colon means an empty while loop. Set GAME_SPEED to 33 and your game will go about 30 frames per second, no matter how fast the machine. It''s all in LaMothe''s Tricks, volume 1.

Phil P