乒乓球游戏帮助帮助? [英] Ping pong game help help help ?

查看:100
本文介绍了乒乓球游戏帮助帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以帮我修理代码,因为它不起作用!!



我试过的:



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
}

推荐答案

引用:

你可以帮我修改代码,因为它不起作用!!

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



不,我们不能。

我们不喜欢我不知道它应该做什么,我们不知道它做了什么!

我们不是来为你修理你的功课 - 我们在这里帮你解决问题。并且让它工作是你设置的任务的一部分,所以你应该自己调试它。



幸运的是,你通常会得到很多工具帮助你做到这一点,统称为调试器。从查看它发生的事情开始,然后弄清楚它不起作用实际意味着:你做了什么,你没想到,或者不这样做你做了什么?它什么时候做的?你怎么做才能做到这一点?

然后开始查看你的代码,并使用调试器在一个明智的地方插入一个断点 - 究竟在哪里取决于上面的what,when和what问题。

然后在调试器中运行您的代码。当它到达断点时,它会停止,让你控制。从那里,您可以查看变量,一次运行一行代码,以及非常有用的东西(确切地说,如何执行此操作将取决于他们的开发环境,但谷歌搜索调试器和您的编译器系统shoudl得到你一个加载信息)。



然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!


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!


引用:

你可以帮我修改代码,因为它不起作用!!

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



这不是提供信息。



当你不明白你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。



建议:拿一张纸,试着手工完成,你的程序应该使用相同的程序。


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.


这篇关于乒乓球游戏帮助帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆