473,396 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

pong game,while loop statement solution

I am writing a pong game.but met some problem.
the ball function to control the scrolling ball,

void ball(int starty,int startx)
{
int di ,i;
int dj,j;
di = 1;
dj = 1;
i = starty;
j = startx;
int ch;
while(1)
{
mvaddstr(i,j,"O");
refresh();
usleep(100000);
i = i + di;
j = j + dj;

if(i >= LINES -1 || i < 0)
{
di = -di;
}
if(j >= COLS -1 || j < 0)
{
dj = -dj;
}
}
}

the question is when I want to invoke the ball()function in main()
function,
I can't go out from the while loop. If I didn't write the while loop
statement
I didn't how to let the ball srcolling all the time.

I had trid to use IPC to make them work,let ball() be invoked by child
process
but not work.

any help is apreciated.

Mar 24 '06 #1
7 3173
TB
DaVinci skrev:
I am writing a pong game.but met some problem.
the ball function to control the scrolling ball,

void ball(int starty,int startx)
{
int di ,i;
int dj,j;
di = 1;
dj = 1;
i = starty;
j = startx;
int ch;
while(1)
{
mvaddstr(i,j,"O");
refresh();
usleep(100000);
i = i + di;
j = j + dj;

if(i >= LINES -1 || i < 0)
{
di = -di;
}
if(j >= COLS -1 || j < 0)
{
dj = -dj;
}
}
}

the question is when I want to invoke the ball()function in main()
function,
I can't go out from the while loop. If I didn't write the while loop
statement
I didn't how to let the ball srcolling all the time.

I had trid to use IPC to make them work,let ball() be invoked by child
process
but not work.

any help is apreciated.


Simple game loop:

while( ! Game over () ) {
Read player input ();
Move the ball ();
Draw next frame ();
}

--
TB @ SWEDEN
Mar 24 '06 #2
DaVinci wrote :
mvaddstr(i,j,"O");


a ncurses-based pong !
Wouldn't using a video framebuffer or an opengl context be better ?
Mar 24 '06 #3
DaVinci wrote:
I am writing a pong game.but met some problem.
the ball function to control the scrolling ball,

void ball(int starty,int startx)
This is going to sound odd, but I think the biggest problem in your
program is right here. Functions *do* things, so it's most natural to
name them with verbs or verb phrases. "ball" is a noun, not a verb
(quit snickering, all you grammatical smart-alecks). Reading the
function name, it's difficult for me -- and evidently for you -- to
determine in any clear fashion what behavior this function is
responsible for.

I know, I know -- you're asking about looping, and I'm harping on about
naming conventions. But work with me a moment -- what is it that you
want this function to do? If you were going to rename it to a verb or
verb phrase, what would you call it? For example, would it be
moveBallOneStep()? Or perhaps moveBallForever()? Do you see how each
of these names carries an obvious implication with regard to the
looping logic? moveBallOneStep() obviously wants to just do one thing
once, each time it's called, and not contain a loop. So, you would put
the call to moveBallOneStep() *inside* a loop, rather than the loop
inside the function.

On the other hand, if you're going to moveBallForever(), then of course
you'll need a loop similar to the one you've written. The thing with
endless loops, though, is that they really hog the flow of control.
main() doesn't get a chance to do anything as long as you're inside the
body of moveBallForever() (or any other function called from main()).
If you stay in that body indefinitely, the flow of control will never
return to main(). Of course, you could always return after some
condition is reached, e.g. moveBallUntilGameEnds().

These aren't wonderful function names or anything, by the way -- just
what I chose for purposes of illustration. They are, however, verb
phrases.
{
int di ,i;
int dj,j;
di = 1;
dj = 1;
i = starty;
j = startx;
int ch;
No need to declare everything at the top like this -- declare things as
locally as possible, generally speaking. Also, save some vertical
space by initializing things as you declare them, rather than declaring
and then assigning. This is a good habit for efficiency, too, when you
come to work with more complex objects.

int i = starty;
int j = startx;

// (inside loop body)
int const di = 1;
int const dj = 1;

Looks like you're not using ch, so remove it.
I had trid to use IPC to make them work,let ball() be invoked by child
process
but not work.


Egad... that's way more convoluted. You need to understand the basic
event loop paradigm; it's very common. Concurrent programming via
mechanisms such as IPC is worlds more complex -- get solid on the
fundamentals first!

Luke

Mar 25 '06 #4

TB 写道:
DaVinci skrev:
I am writing a pong game.but met some problem.
the ball function to control the scrolling ball,

void ball(int starty,int startx)
{
int di ,i;
int dj,j;
di = 1;
dj = 1;
i = starty;
j = startx;
int ch;
while(1)
{
mvaddstr(i,j,"O");
refresh();
usleep(100000);
i = i + di;
j = j + dj;

if(i >= LINES -1 || i < 0)
{
di = -di;
}
if(j >= COLS -1 || j < 0)
{
dj = -dj;
}
}
}

the question is when I want to invoke the ball()function in main()
function,
I can't go out from the while loop. If I didn't write the while loop
statement
I didn't how to let the ball srcolling all the time.

I had trid to use IPC to make them work,let ball() be invoked by child
process
but not work.


any help is apreciated.

Simple game loop:

while( ! Game over () ) {
Read player input ();
Move the ball ();
Draw next frame ();
}

I had tried that.
I change the while loop to moveBallOneStep()
it looks like this:
But the ball can move only one step when I input one character.
If I didn't input anything,the ball will not move anymore.
what I want is the ball will move all the time,at least it looks like
moving all the time.

If using the while loop moveBallForever() I can't jmp out of the loop
all the same.

--
TB @ SWEDEN


Mar 25 '06 #5

Luke Meyers 写道:
DaVinci wrote:
I am writing a pong game.but met some problem.
the ball function to control the scrolling ball,

void ball(int starty,int startx)
This is going to sound odd, but I think the biggest problem in your
program is right here. Functions *do* things, so it's most natural to
name them with verbs or verb phrases. "ball" is a noun, not a verb
(quit snickering, all you grammatical smart-alecks). Reading the
function name, it's difficult for me -- and evidently for you -- to
determine in any clear fashion what behavior this function is
responsible for.

I know, I know -- you're asking about looping, and I'm harping on about
naming conventions. But work with me a moment -- what is it that you
want this function to do? If you were going to rename it to a verb or
verb phrase, what would you call it? For example, would it be
moveBallOneStep()? Or perhaps moveBallForever()? Do you see how each
of these names carries an obvious implication with regard to the
looping logic? moveBallOneStep() obviously wants to just do one thing
once, each time it's called, and not contain a loop. So, you would put
the call to moveBallOneStep() *inside* a loop, rather than the loop
inside the function.

On the other hand, if you're going to moveBallForever(), then of course
you'll need a loop similar to the one you've written. The thing with
endless loops, though, is that they really hog the flow of control.
main() doesn't get a chance to do anything as long as you're inside the
body of moveBallForever() (or any other function called from main()).
If you stay in that body indefinitely, the flow of control will never
return to main(). Of course, you could always return after some
condition is reached, e.g. moveBallUntilGameEnds().

These aren't wonderful function names or anything, by the way -- just
what I chose for purposes of illustration. They are, however, verb
phrases.
{
int di ,i;
int dj,j;
di = 1;
dj = 1;
i = starty;
j = startx;
int ch;


No need to declare everything at the top like this -- declare things as
locally as possible, generally speaking. Also, save some vertical
space by initializing things as you declare them, rather than declaring
and then assigning. This is a good habit for efficiency, too, when you
come to work with more complex objects.

int i = starty;
int j = startx;

// (inside loop body)
int const di = 1;
int const dj = 1;

I will chang the variable di,and di
di = -di;
dj = -dj;

so I can't use const Looks like you're not using ch, so remove it.
I had trid to use IPC to make them work,let ball() be invoked by child
process
but not work.


Egad... that's way more convoluted. You need to understand the basic
event loop paradigm; it's very common. Concurrent programming via
mechanisms such as IPC is worlds more complex -- get solid on the
fundamentals first!

Luke


Mar 25 '06 #6

loufoque wrote:
DaVinci wrote :
mvaddstr(i,j,"O");


a ncurses-based pong !
Wouldn't using a video framebuffer or an opengl context be better ?

NCURSES is more easy to use.

Mar 26 '06 #7
I think I have solove my problem .I ignore one important function.
nodelay(WINDOW* win,true),which will not wait for one
character--getch() return ERR if I didn't type anything yet.
so in the main()
for(ch = getch(); ch!='q' ;ch = getch() )
{
moveBallOneStep();//only one step not exist a while loop
switch(ch)
{
case 'j':
moveBoardUp();
case 'k':
moveBoardDown();
...
}

}

Mar 26 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Xad | last post by:
Hi, i'm looking for java code for a really simple, basic Pong game. As simple as possible - even just two paddles and a ball bouncing will do. Can anyone help? Thanks
0
by: junglist | last post by:
Hi, i have created a Pong game Application where two players control their pads using the keyboard. the problem is that one player is using the action keys VK_UP and VK_DOWN in keyPressed() and...
9
by: Ben | last post by:
I have two 'Do While Not' statements, that are getting information from the same recordset. If I comment out the first one I can get the results for the second one, and vice-versa. Why is this...
9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
11
by: Rene | last post by:
Quick question, what is the point for forcing the semicolon at the end of the while statement? See example below: x = 0; do { x = x + 1; }while (x < 3); What's the point of having the...
3
by: Brad | last post by:
Hi folks, I'm still fairly new to programming in python and programming in general. A friend of mine is in a CompSci 101 course and was working on a slider game when he encountered a problem. We...
8
by: Runtheball | last post by:
As a PHP learning exercise, I'm trying to program a "star trek" game. The game allows a list of commands, which are executed through a switch statement. The command below (case 8) is to 'scan an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.