本文介绍了我可以向一个方向移动圆圈,但不能向多个方向移动。帮助我plzzzzzz?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
int main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
setcolor(WHITE);

settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);

line(0,60,getmaxx(),60);

int Ball_x,Ball_y, direction=1;// 1 right, -1 left
Ball_x=getmaxx()/2;
Ball_y=getmaxy()/2;
circle(Ball_x,Ball_y,20);

while(1){                   // continuous loop as condition will always be true
        setcolor(WHITE);
        circle(Ball_x,Ball_y,20);
        delay(20);
        setcolor(BLACK);
        circle(Ball_x,Ball_y,20);
        delay(20);
        //      Code for moving of Ball
        if (direction==1)
           Ball_x+=3;
        else
           Ball_x-=3;
        if(Ball_x-20 <= 0)
           direction=1;
        if(Ball_x >= getmaxx()-20)
           direction=-1;
        }

getch();
}





[edit]已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

class Circle
{
  int x,y;
  int vx,vy;
  //.. ctor and whatever, here

public:
 void update_pos(int dt)
  {
    x += vx * dt;
    y += vy * dt;
    // check for boundaries hits here
  }

  void update_speed(int vx, int vy)
  {
    this->vx = vx; this->vy = vy;
  }
};


//      Code for moving of Ball

记住允许Y值而不是X!



为了更好的方法,创建一个带有三个参数的函数:当前值,相应的方向,以及最大值,让它返回新值并调用两次。这样,当你想让运动随机变大时,你不必在两个地方改变它。

remembering to allow for the Y values instead of X!

For a better method, create a function that takes three parameters: the current value, the appropriate direction, and the maximum value, have it return the new value and call it twice. That way, when you want to make the movement random sized, you don't have to change it in two places.


这篇关于我可以向一个方向移动圆圈,但不能向多个方向移动。帮助我plzzzzzz?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 03:12
查看更多