我怎么不覆盖连接四的游戏板? [英] How do I not overwrite my game board for connect four?

查看:62
本文介绍了我怎么不覆盖连接四的游戏板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建不会循环并不断调用绘图功能的代码,以便在轮到他们时,任何玩家的棋子都不会覆盖棋盘?



这阻止我创建步骤并开发我的游戏块位置以及用户交互。



请记住,这些代码中的一些已被注释掉用于调试目的。



此外,是否有人知道如何通过使用鼠标事件来合并用户的选择。



谢谢







How do I create code that won't loop and constantly call the draw function so that the board is not overwritten by whichever player's pieces when it is their turn?

This is preventing me from creating steps and developing my game piece placement, as well as the user interaction.

Keep in mind that some of this code is commented out for debugging purposes.

Also, does anybody know how to incorporate a user's selection as to where to place their piece through the use of mouse events.

Thank you



#include "ofApp.h"
#include "../GamePieces.h"

//--------------------------------------------------------------
void ofApp::setup()
{
    ofSetCircleResolution(90);
    draw();
drawBoard();
//    myImage.loadImage("beatles.jpg");

//    mySound.loadSound("cat.mp3");
 //   mySound.play();

   playGame();
   cout<<"\n\nIN SETUP\n\n";

 //   p1.set(10.0, 20.0);
 //   p2.set(100, 300);

//    printf("\np1:  x: %f y: %f", p1.x, p1.y);
//    cout << "\np2:  x: " << p2.x << "  y: " << p2.y;
}

//--------------------------------------------------------------
void ofApp::update()
{

}

//--------------------------------------------------------------
void ofApp::draw()
{
//    ofSetColor(255);
//    myImage.draw(0,0, 200, 150);


   ofColor myColor(255, 0, 0);
   ofSetColor(myColor);
   drawBoard();
//placePiece(currPlayer,gameBoard);

//   ofPushMatrix();
//      ofTranslate(100, 200);
//      ofLine(p1, p2);
//   ofPopMatrix();

   ofSetColor(0, 0, 255);
   ofCircle(mouseX, mouseY,70);
}

void ofApp::drawBoard()
{
    int i,n,j;
    n=8;
    for(i=1; i    {
            p1.set(i*150,150);
            p2.set(i*150,1050);
            ofLine(p1, p2);
    }

    for(i=1; i    {
            p1.set(150,i*150);
            p2.set(1050,i*150);
            ofLine(p1, p2);
    }

if(currPlayer!=1&&currPlayer!=2) currPlayer=0;
    for(i=1;i<7;i++)
   {
        for(j=1;j<7;j++)
       {
        gameBoard[i-1][j-1]=GamePieces();
        gameBoard[i-1][j-1].setGamePieces(i*150+75,j*150+75,70,currPlayer);

        if(gameBoard[i-1][j-1].player==0) ofSetColor(0, 255, 0);
        else if(gameBoard[i-1][j-1].player==1) ofSetColor(255,0, 0);
        else if(gameBoard[i-1][j-1].player==2) ofSetColor(0, 0,255);

        ofCircle(gameBoard[i-1][j-1].cx,gameBoard[i-1][j-1].cy,gameBoard[i-1][j-1].r);

//cout<<"\n"<<gameBoard[i][j].cx<<" "<<gameBoard[i][j].cy<<" PLAYER "<<gameBoard[i][j].player;

       }
   }

}

void ofApp::placePiece(int currentPlayer,GamePieces gameBoard[6][6])
{
 //   int available[6]={0,0,0,0,0,0};
    int correctSelection=0;
//    while(correctSelection==0)
//    {

    cout<<"\nPlease Select a column to drop your game piece";
    cin>>mouseX;

    if(mouseX<1||mouseX>6)
    {
        cout<<"\nINVALID SELECTION!!";
        cout<<"\nPlease Select a column to drop your game piece";
        cin>>mouseX;
    }


/*while(correctSelection==0)
{
    if(mouseX<150||mouseX>1050)
    {
        cout<<"\nINVALID SELECTION!!";
        cout<<"\nPlease Select a column to drop your game piece";
        cin>>mouseX;
    }
*/    else if(available[mouseX]==6)//150]==6)
    {
        cout<<"\nThis column is FULL!!";
        cout<<"\nPlease Select a column to drop your game piece";
        cin>>mouseX;
    }
    else
    {
        int avail=available[mouseX];//150];
//available[avail]++;
        gameBoard[mouseX][available[avail]].setGamePieces(mouseX,available[avail],60,currPlayer);

        mouseX=mouseX*150+75;
        mouseY=975-available[mouseX]*150;
        available[mouseX/150]++;

        available[avail]=avail++;
        correctSelection++;
    }
//}
 if (correctSelection==1) //draw();
    update();
}
//}


void ofApp::playGame()
{
   // draw();
    cout<<"\n\nin playGame\n\n";
    int play=0;
    int turns=1;
    int selected;
//    currPlayer=1;
    while(play==0)
    {

currPlayer=(currPlayer)%2+1; //NEEDS TO BE ALTERED
//placePiece(currPlayer,gameBoard);
//placePiece(currPlayer,gameBoard);
 //       cout<<"Player 1 Please select a column to place your game piece";
 //       cin>>selected;
 //   mouseX=ofGetMouseX();
 //       mousePressed(mouseX,mouseY,mouseButton);


        if(currPlayer==1)
        {
            ofSetColor(0, 0, 255);
        }
        else if(currPlayer==2)
        {
            ofSetColor(255, 0, 0);
        }
        else ofSetColor(0,255,0);

        ofCircle(mouseX, mouseY, 75);
        mousePressed(mouseX,mouseY,mouseButton);
//       placePiece(currPlayer,gameBoard);

    turns++;

            if(turns==37) play++;

   //     }

  //  update();
    }
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key)
{

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key)
{

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y)
{
    mouseX = x;
    mouseY = y;
}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button)
{

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button)
{
    mouseX = x;
    mouseY = y;
    mouseButton=button;

    cout<<"\nbutton : "<<mouseButton;
    cout<<"\n  X : "<<mouseX;
    cout<<"\n  Y: "<<mouseY;
    cout<<"\n  x placement : "<<mouseX/150*150+75;
}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button)
{

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h)
{

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg)
{

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo)
{

}

推荐答案

您只能使用 InvalidateRect [ ^ ]并且仅指定要重绘的窗口区域。至于使用鼠标,您只需要捕获鼠标点击或拖动操作。这些都在MSDN文档中描述。
You can update parts of your application's window only by using InvalidateRect[^] and specifiying only the area of the Window to be redrawn. As to using the mouse, you just need to capture mouse clicks or drag operations. It's all described in the MSDN documentation.


这篇关于我怎么不覆盖连接四的游戏板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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