I want to be able to move 3*3 squares like the Rockman Executive.

Asked 2 years ago, Updated 2 years ago, 72 views

environment
Visual Studio 2019
DX Library C language, C++

#include "DxLib.h"

int Key [256]; // Store the number of frames pressed by the key

// update key entry status
int gpUpdateKey(){
    char tmpKey [256]; // Store the current key input state
    GetHitKeyStateAll(tmpKey); // Get all key input states

    for(inti=0;i<256;i++){
        if(tmpKey[i]!=0){//i key code has been pressed
            Key[i]++; // Add
        }
        else {// If not pressed
            Key[i] = 0; // 0
        }
    }
    return 0;
}

// The program starts with WinMain.
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, intnCmdShow)
{
    SetGraphMode (1300, 680, 32); // Specify window size
    ChangeWindowMode (TRUE);

    if(DxLib_Init()==-1) // DX library initialization process
    {
        return-1;// Exit immediately if an error occurs
    }

    // Array for key acquisition
    // char key [256];
    // 1.3x3 2D array
    int pos[][3] = {
        {0, 0, 0 },
        {0, 0, 0 },
        {0, 0, 0 }
    };

    int playerX=0;// X coordinates of the character
    int playerY=300;// Y table of characters
    // double a[2][2]; // array variables for movement restrictions
    intx = 0;
    inty = 0;

    // Array for storing graphic handles
    intgh[12];
    LoadDivGraph ("charall.png", 12, 3, 4, 49, 66, gh);
    DrawGraph(playerX,playerY,gh[8],FALSE); // Draw player image

    while(ScreenFlip()==0&&ProcessMessage()==0&gpUpdateKey()==0){

        int addition = 70;
        intb = 70;

        // be pressed to the right of the cursor key
        if(Key[KEY_INPUT_RIGHT]==1){

            playerX=playerX+addition; // Add playerX coordinates // If playerX2 is only left, it is not a for statement, so it only happens once. If playerX is playerX=playerX+addition, playerX is added again into playerX on the right side and can go to に to the right.
            // Output to screen
            ScreenFlip();
            // Clear Screen
            ClearDrawScreen();
            // Draw an image of the player

            DrawGraph (playerX, playerY,gh[2],true); // DrawGraph is a function capable of drawing gh[8], which is one of the arrays divided into 12 parts by LoadDivGraph.
        }

        Else {while(DrawGraph(DrawGraph(DrawGraph(playerX,playerY,gh[8],true)));}//Drawing of characters is set to gh[8] while being added by pressing the right.You can deny the conditions by adding else.

        if(Key[KEY_INPUT_UP]==1){
            playerY=playerY-b;// Add player Y coordinates

            // Output to screen
            ScreenFlip();
            // Clear Screen
            ClearDrawScreen();
            // Draw an image of the player
            DrawGraph(playerX,playerY,gh[5],true);
        }

        if(Key[KEY_INPUT_LEFT]==1){
            playerX=playerX-addition; // Add player X coordinates

            // Clear Screen
            ClearDrawScreen();
            // Draw an image of the player
            DrawGraph(playerX,playerY,gh[1],true);

        }

        if(Key[KEY_INPUT_DOWN] == 1) {
            playerY=playerY+b;// Add player Y coordinates

            // Output to screen
            ScreenFlip();
            // Clear Screen
            ClearDrawScreen();
            // Draw an image of the player
            DrawGraph(playerX,playerY,gh[11],true);
        }

    }

    DxLib_End(); // DX Library Usage Termination Process

    return 0;// End of software 

}

What do you want to do
For(y=0;y<3;y++) {
programs that allow you to move characters freely through 3*3 squares like a rockman executive. For(x=0;x<3;x++){ and I'm wondering how to add it to the function if(Key[KEY_INPUT_RIGHT]==1){}.

I am writing a program that I wrote in my own way, although I am mistaken below.

// The cursor key is pressed to the right.
if(Key[KEY_INPUT_RIGHT]==1){
    playerX=playerX+ addition; 
    double[1][1]=playerX;
    // Output to screen
    ScreenFlip();
    // Clear Screen
    ClearDrawScreen();
    // Draw an image of the player
    DrawGraph (double[1][1], playerY,gh[2], true);
}

A character is moved to coordinates [1][1] by a right key, and an image drawn in coordinates [1][1] becomes gh[2].It's a program that shows this.This is for (y=0;y<3;y++){
Some people think that adding for(x=0;x<3;x++){ might solve the problem.
I would like you to tell me why you can't put it like that in the above programs and what you need if you leave it as it is.This is to improve self-solving skills in the future.Thank you for your cooperation.

If you could write down all the codes you would like to do this time with the for statement and array, if you could explain in detail such as ( ) and {} what you would like to do, you would be able to solve yourself in the future.

c++ c cocos2d-x

2022-09-29 21:38

1 Answers

I'm sorry if I didn't understand your question, but is the following probably what I want to do?

 if(Key[KEY_INPUT_LEFT]==1){
  playerX=std::clamp(playerX+adding, 0*adding, 2*adding); // playerX moves between 0*adding and 2*adding
}

PlayerX and playerY already have the coordinates above 3*3 squares of characters.
Maybe that's enough, and I don't think it's necessary to put it in an array.
If you want to know where the player is on 3*3 squares, you can ask (playerX/addition).
If you want to use the 3*3 array, I think it would be effective to use the bool or int type instead of the double type to make it true (or 1) only where the player is.
Then, skip the if statement and do DrawGraph and ScreenFlip.
By the way, if you do ClearDrawScreen immediately after ScreenFlip, nothing will be displayed.As soon as I output it on the screen, it's cleared.


2022-09-29 21:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.