Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
could you help me to fix the code because it doesn't work !!

What I have tried:

#include <graphics.h>


#define RACKETLEN 50
#define RACKETWIDTH 5

static int racket1_x = 20;
static int racket1_y = 280;
static int racket2_x = 770-RACKETWIDTH;
static int racket2_y = 280;
static int ball_x= 100, ball_y = 45;
static int ballSize = 10;
static int RacketColor = 15;
static int BallGNDColor = 0;
static int BallColor = 15;
int dirY = 0;int dirX = 0;

void drawBoard(int color)
{
	setcolor(color);
	rectangle (10,30, 780, 580);
}

void DrawLine(int color, int x0, int y0, int x1, int y1,int thickness) {

  while(thickness>=1)
  {
      int tx0,ty0,tx1,ty1;
      tx0=x0;tx1=x1;ty0=y0;ty1=y1;
      int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
      int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
      int err = (dx>dy ? dx : -dy)/2, e2;

      for(;;){
        putpixel(x0, y0, color);
        //put_pixel(g,x0,y0);
        if (x0==x1 && y0==y1) break;
        e2 = err;
        if (e2 >-dx) { err -= dy; x0 += sx; }
        if (e2 < dy) { err += dx; y0 += sy; }
      }
      x0=tx0+1;
      y0=ty0;
      x1=tx1+1;
      y1=ty1;
      thickness--;
  }
}

void drawRackets(int color)
{
	DrawLine(color,racket1_x,racket1_y,racket1_x,racket1_y+RACKETLEN,RACKETWIDTH);
	DrawLine(color,racket2_x,racket2_y,racket2_x,racket2_y+RACKETLEN,RACKETWIDTH);
}

void drawBall(int x, int y, int ballSize, int color)
{
    DrawLine(color,x,y,x,y+ballSize,ballSize);

}

void drawTop()
{
    setcolor(4);
    outtextxy(30, 5, "Pong Game !");
    outtextxy(380, 5, "0 - 0");
    outtextxy(680, 5, "SCORE: ");
}

void initGame()
{
    initwindow(800, 600);
	cleardevice();
	drawBoard(1);
	drawRackets(RacketColor);
	drawTop();
}

void move_racket(int *x, int *y, int dir)
{
    drawRackets(0);
	if (dir == 1)  	/* LEFT */
		*y = *y - 5;
	else		/* RIGHT */
		*y = *y + 5;
	drawRackets(RacketColor);
}

void move_ball()
{
    if(ball_y>=580-ballSize){
        dirY = 0;}
    if(ball_y<=31){
        dirY = 1;}

  if(ball_x>=780-ballSize)
    {
    dirX=0;}
  if (ball_x<=31){
    dirX =1;}

	drawBall(ball_x,ball_y,ballSize,BallGNDColor);

    if(dirY == 1){
        ball_y++;}
    else{
        ball_y--;}

    if(dirX == 1){
        ball_x++;}
    else{
        ball_x--;}


	drawBall(ball_x,ball_y,ballSize,BallColor);
}



void playGame()
{
    char c;
    initGame();
    while(1) {
        if (kbhit()) {
            c = getch();
	    if (c == 'w')    /* 'a' character */
	    	move_racket(&racket1_x,&racket1_y,1);
        if (c == 's')
            move_racket(&racket1_x,&racket1_y,0);
	    if (c == 119)   /* 'w' character */
	    	move_racket(&racket2_x,&racket2_y,0);
	    // setpos(15,15);
	    // putint(c);
        }
       delay(1);
       if (ball_x < 800 && ball_y < 600) {
		move_ball();
       }
        // char* line;
        //line = readline();
        //println("");
        //println("You entered:");
        //println(line);
        //free(line);
    }
}


main(int argc, char *argv[])
{
    playGame();
    //while(1);
	//while(!kbhit()) delay(1);				// pause screen
}
Posted
Updated 4-Mar-17 22:25pm

Quote:
could you help me to fix the code because it doesn't work !!

No, we can't.
We don't know what it should do, we don't know what it does do!
We aren't here to fix your homework for you - we're here to help you solve your problems. And "getting it working" is part of the task you have been set, so you are expected to debug it yourself.

Fortunately, you normally get a lot of tools to help you do that, collectively called a "debugger". Start by looking at what it happening, and working out what "it doesn't work" actually means: what does it do that you didn't expect, or not do that you did? When does it do it? What do you do to make it do that?
Then start looking at your code, and use the debugger to insert a breakpoint somewhere sensible - exactly where will depend on the "what", "when", and "what" questions above.
Then run your code in the debugger. when it reaches the breakpoint, it will stop, and let you take control. From there, you can look at variables, run your code one line at a time, and such like very useful things (exactly how you do this will depend on they development environment, but googling "debugger" and your compiler system shoudl get you a load of info).

Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Quote:
could you help me to fix the code because it doesn't work !!

This is not informative.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900