Pong program, moving the ball, assistance needed

I'm sorry if this is in the wrong forum, but here it is. I've decided to make pong as my first BIG program, not trying to get into anything to deep with DarkGDK, which I did with Allegro...Bleh, that was a mess. Any way, i'm focusing on making the ball move on its own right now, here is the code I'm using:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void MoveBall ( ) // y<-=33 y+=>440  x<-=25  x+= > 615
{
 if ( dbSpaceKey ( ) )
 {
  BallMovement = 1;
  if ( BallMovement = 1 )
  {
   IsBallY = 1;

   if ( IsBallY = 1 )
   {
    BallY -= BALL_SPEED;
   
   if ( BallY <= 33 )
   {
	  BallY = 33;
	  IsBallY = 0;
   }
   }
   else
   {
    IsBallY = 0;

	if ( IsBallY = 0 )
	{
     BallY += BALL_SPEED;
	 if ( BallY >= 440 )
	 {
      BallY = 440;
	 }
	}
   } 
 }
}
 BallMovement = 0;
}
I've noticed some stuff that doesn't work right when I test it. For one, when I press space it goes for a little bit and stops, at the correct point for the first angle, but does not continue to the next, is their anything I could do differently, or an entirely new way to write this movement code? I can post the entire Main.cpp code if you need it.

Thanks in advance,
Iacoopa
All your comparisons of equality use the assignment operator (=) instead of the equality comparison operator (==). You're very probably overwriting variables.
Hmmm, I will try that.
Okay, tried that, got this
1
2
3
4
5
6
7
8
9
10
11
12
13
1
1>Compiling...
1>Main.cpp
1>c:\documents and settings\joe\desktop\c++ projects\dark gdk - 2d game1\dark gdk - 2d game1\main.cpp(75) : warning C4553: '==' : operator has no effect; did you intend '='?
1>c:\documents and settings\joe\desktop\c++ projects\dark gdk - 2d game1\dark gdk - 2d game1\main.cpp(78) : warning C4553: '==' : operator has no effect; did you intend '='?
1>c:\documents and settings\joe\desktop\c++ projects\dark gdk - 2d game1\dark gdk - 2d game1\main.cpp(86) : warning C4553: '==' : operator has no effect; did you intend '='?
1>c:\documents and settings\joe\desktop\c++ projects\dark gdk - 2d game1\dark gdk - 2d game1\main.cpp(87) : warning C4553: '==' : operator has no effect; did you intend '='?
1>c:\documents and settings\joe\desktop\c++ projects\dark gdk - 2d game1\dark gdk - 2d game1\main.cpp(92) : warning C4553: '==' : operator has no effect; did you intend '='?
1>Linking...
1>LINK : Debug\Dark GDK - 2D Game1.exe not found or not built by the last incremental link; performing full link
1>Embedding manifest...
1>Build log was saved at "file://c:\Documents and Settings\Joe\Desktop\C++ Projects\Dark GDK - 2D Game1\Dark GDK - 2D Game1\Debug\BuildLog.htm"
1>Dark GDK - 2D Game1 - 0 error(s), 5 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Last edited on
Those aren't really errors, but they are rather weird warnings.
How are those variables declared?
I know they arent errors, and the variables are declared after the includes and before the main function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "DarkGDK.h"

const int BAT_SPEED = 10;
const int P1_BAT_POS_Y = 435;
const int BALL_SPEED = 3;
const int BALL_SIZE = 5;
int BallY = 50;
int BallX = 50;
int BallMovement;
int IsBallX;
int IsBallY;

// y<-=33 y+=>440  x<-=25  x+= > 615



int DoPlayer1Movement ( int bat_pos_x );
void DrawPlayer1Bat ( int bat_pos_x );
void DrawBall (  );
void MoveBall (  );

void DarkGDK ( void )
I was about to say "I am stumped", but now I understand what's going on:
Look at the comparison on line 6. Now look at what you're doing on line 5. The comparison will always be true, that's what the compiler was warning about. You've done this in several other parts (one for each warning, actually).
So, Im not quite sure I understand what you're saying, but wherever there's a lone equal sign it's always that number?
This is what I'm saying:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
Let's say somewhere there's a variable declared as 'int a'.
'a' at this point has a certain value, which is unknown.
If you do this:
*/
a=5;
//Then immediately check:
if (a==5){
	/*
	The execution flow will invariable follow the 'true' path, because 'a' will
	always equal 5, because just before the comparison you made it so.
	*/
}

//Now let's look at your code:
//From line 5:
BallMovement = 1;
if ( BallMovement == 1 ){ //Will always be true.
	IsBallY = 1;
	if ( IsBallY == 1 ){ //Also.
		BallY -= BALL_SPEED;
		if ( BallY <= 33 ){
			BallY = 33;
			IsBallY = 0;
		}
	}else{
		IsBallY = 0;
		if ( IsBallY = 0 ){ //Again.
			BallY += BALL_SPEED;
			if ( BallY >= 440 ){
				BallY = 440;
			}
		}
	} 
}
Ahh, that makes sense
Okay, thanks to you and some source code I found I finally made it move, using a structure so, here's the code. Everything else should be easy :D thanks a bunch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "DarkGDK.h"

const int BAT_SPEED = 10;
const int P1_BAT_POS_Y = 435;
const int BALL_SPEED = 3;
const int BALL_SIZE = 5;
struct BallMovement
{
	int movement_state;
	int pos_x;
	int pos_y;
	int dir_x;
	int dir_y;
};
struct BallMovement MoveBall( int ball_pos_x, int ball_pos_y, int ball_dir_x, int ball_dir_y, int game_state );

// y<-=33 y+=>440  x<-=25  x+= > 615



int DoPlayer1Movement ( int bat_pos_x );
void DrawPlayer1Bat ( int bat_pos_x );
void DrawBall( int ball_pos_x, int ball_pos_y );


void DarkGDK ( void )
{
	dbSyncOn ( );
	dbSyncRate ( 60 );
	int p1_bat_pos_x = 270;
	int ball_movement_state = 0;
	int ball_dir_x = dbRnd( 2 ); 
	int ball_dir_y = dbRnd( 2 );
	int ball_pos_x = 310;
	int ball_pos_y = 220;
	struct BallMovement Ball;
	while ( LoopGDK ( ) )
	{
	 dbCLS ( );
	 dbText ( 280, 10, "Pong!!" );
	 p1_bat_pos_x = DoPlayer1Movement ( p1_bat_pos_x );
	 DrawPlayer1Bat ( p1_bat_pos_x );
	 Ball = MoveBall( ball_pos_x, ball_pos_y, ball_dir_x, ball_dir_y, ball_movement_state );
		ball_movement_state = Ball.movement_state;
		ball_pos_x = Ball.pos_x;
		ball_pos_y = Ball.pos_y;
		ball_dir_x = Ball.dir_x;
		ball_dir_y = Ball.dir_y;
		DrawBall( ball_pos_x, ball_pos_y );

	 

	
	 dbSync ( );
    }
   }


void DrawPlayer1Bat ( int bat_pos_x )
{
 dbBox ( bat_pos_x, P1_BAT_POS_Y, bat_pos_x + 100, P1_BAT_POS_Y + 10 );
}

int DoPlayer1Movement ( int bat_pos_x )
{
	
	 if (dbLeftKey ( ) )
	{
		bat_pos_x -=BAT_SPEED;
			if ( bat_pos_x < 20 )
				bat_pos_x = 20;
	}
	else if ( dbRightKey ( ) )
	{
		bat_pos_x += BAT_SPEED;
		if ( bat_pos_x > 520 )
			bat_pos_x = 520;
	}
	
	return bat_pos_x;
}

struct BallMovement MoveBall( int ball_pos_x, int ball_pos_y, int ball_dir_x, int ball_dir_y, int ball_movement_state )
{
	BallMovement Ball;
	
	if ( dbSpaceKey( ) )
	{ ball_movement_state = 1; }

	if ( ball_movement_state )
	{
		if ( ball_dir_x )
		{ 
			ball_pos_x += BALL_SPEED; 
			if ( ball_pos_x > 615 )
			{ 
				ball_pos_x = 615; 
				ball_dir_x = 0;
			}
		}
		else
		{ 
			ball_pos_x -= BALL_SPEED; 
			if ( ball_pos_x < 25 )
			{ 
				ball_pos_x = 25; 
				ball_dir_x = 1;
			}
		}
		if ( ball_dir_y )
		{ 
			ball_pos_y += BALL_SPEED; 
			if ( ball_pos_y > 440 )
			{ 
				ball_pos_y = 440; 
				ball_dir_y = 0;
			}
		}
		else
		{ 
			ball_pos_y -= BALL_SPEED;
			if ( ball_pos_y < 33 )
			{ 
				ball_pos_y = 33; 
				ball_dir_y = 1;
			}
		}
	}
	
	Ball.pos_x = ball_pos_x;
	Ball.pos_y = ball_pos_y;
	Ball.dir_x = ball_dir_x;
	Ball.dir_y = ball_dir_y;
	Ball.movement_state = ball_movement_state;

	return Ball;
}

void DrawBall( int ball_pos_x, int ball_pos_y )
{
	dbCircle( ball_pos_x, ball_pos_y, BALL_SIZE );	
}
Topic archived. No new replies allowed.