【问题标题】:Create a loop that determines if the object is on the left or right side of the screen given the coordinates创建一个循环,确定对象是在给定坐标的屏幕左侧还是右侧
【发布时间】:2019-09-27 00:38:42
【问题描述】:

留在屏幕上!电子游戏中的动画就像电影中的动画一样——它是 逐个图像绘制(称为“帧”)。在游戏可以绘制框架之前,它需要更新 物体的位置基于它们的速度(除其他外)。这样做是相对的 简单:将速度添加到每帧对象的位置。 对于这个程序,假设我们想要跟踪一个对象并检测它是否离开了左侧或右侧 屏幕(即它的 X 位置小于 0 且大于屏幕宽度,例如 100)。 编写一个程序,询问用户对象的起始 X 和 Y 位置以及 开始 X 和 Y 速度,然后在每帧打印出它的位置,直到对象移出 屏幕。为这个程序设计(伪代码)和实现(源代码)。

这是我的问题。我已经开始了。创建我的变量,要求用户输入,读取用户输入等。现在我被困在从哪个循环开始......我不是在寻求帮助来作弊!只是帮助走上正确的轨道!!任何意见表示赞赏!

【问题讨论】:

  • 这不是一个合适的 SO 问题,或者根本不是一个真正的问题。其他人怎么知道“从哪个循环开始”?!真的,我的意思是,如何?请尝试编写一些代码,然后将其展示给观众...说出您的想法以及您的代码应该做什么!

标签: visual-c++ pseudocode


【解决方案1】:

这里

    // given this is the screen width `object`
     const screen = { x: window.innerWidth, y: window.innerHeight }
    // initial coordinates are chosen by the user 
    // as velocity is applied the'll change ofcourse
     let coordinates = {x: prompt('enter x-axis') , y: prompt('enter x-axis')}
    // game loop update
    update () {
        if (coordinates.x > screen. x){
            // right side 
        } else {
            // left side
        }
    }

【讨论】:

    【解决方案2】:

    如果您想要一个可视化 c++ 循环,请使用计时器,并将其设置为任何滴答速度。然后添加刻度功能(每次更新都会发生这种情况)这将是您的更新功能,因此将您的代码移到那里。您可能想在计时器滴答结束时添加:

    this->Refresh();
    

    这基本上是更新屏幕坐标

    其次,要检查屏幕的哪一侧以显示诸如 Visual c++ 之类的内容,您需要知道窗口的大小。

    假设我的窗口是 100X100(x 和 y)。 我们知道过去 x 50 是屏幕的左侧,在它之前是右侧。 所以你可以这样做

    if(x > 50){
    //do stuff to do with right side
    }else if(x < 50){
    //do stuff to do with left side
    }
    

    对于输入,您可以添加文本框。您可以编写 x 和 y,然后一旦发送按钮,输入将转换为 2 个 int(inputx 和 inputy)。在更新功能上,你可以这样:

    //inputx is 10, inputy is 20
    
    bool xdone = false;
    bool ydone = false;
    
    if(xdone == false){
    if(inputx < 0){ // checks if x is negative
    x = x - 1; //If the input was negative, it will start decreasing
    }else if(inputx > 0){// checks if x is positive
    x = x + 1;//If the input was positive, it will start increasing
    }
    }
    if(ydone == false){
    if(inputy < 0){ // checks if y is negative
    y = y - 1; //If the input was negative, it will start decreasing
    }else if(inputy > 0){// checks if y is positive
    y = y + 1;//If the input was positive, it will start increasing 
    }
    }
    if(inputx == x || inputy == y){
    this->[NAME OF TIMER]->Enabled = false;
    }
    
    if(inputx == x){
    xdone = true;
    }
    if(inputy == y){
    ydone = true;
    }
    x = inputx;
    y = inputy;
    this->Refresh();
    

    如果我对任何知道的人有任何错误,请随时纠正我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      相关资源
      最近更新 更多