Octin
Octin

Reputation: 1

2D character movement, map updating, position updating

I'm a beginner programmer. How do i make the map update in this code. How do i make the players position update? I wanted to make him move by incrementing one of the values [x][y], but how do i do that? Is the code below a good start?

#include <windows.h>

int map[5][5]={
    {0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0}};

int m_print(int x) {
    int i, j;
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 5; j++) {
            printf("%d     ", map[i][j]);
        }
        printf("\n");
    }
}

struct player {
    int pos[i][j];
    int show;
    int movement;
};

int map_update(int x) {

}

int p_show(int x) {

}

main() {
    int game_running = true;
}

Upvotes: 0

Views: 884

Answers (1)

Greg
Greg

Reputation: 706

First of all I have absolutely no idea about what you are trying to accompish. However I saw that you are a begginer with C and I thought of assembling a very quick example on something hopefully similar on what you are trying to do.

Some notes on the things that I had to fix from your code:

  1. int pos[i][j]; in the player struct makes no sense, if you need an array as a struct parameter, setup a pointer there and make sure to allocate it somewhere in your code to fit your needs. I replaced it with some int parameters for keeping track of the old and current player position
  2. The main function needs to run something
  3. By default there are no true/false values in C. Not sure about your compiler and compilation flags, but from linux I did some typedefs for a bool macro (which is actually handled as an int) just to show you how typedefs can be used

The code that I made does some very basic stuff. At first I allocate a player using the struct that we defined. Then I setup his position to 0,0 and also set map[0][0]=1 to indicate that the player is there.

I setup a while loop with a very simple input method that scanfs for input characters from the console. The accepted characters are w,a,s,d. These are passed to the move_player method which based on the input updates the position of the player struct. Then the map_update method gets a player struct as an argument and updates the map accordingly to show the player's new position. Note that both methods accept struct pointers as arguments. Using pointers we just pass the reference (a memory address of the struct) and not the actual value (a copy of all the data of the struct). Doing otherwise, we would not be able to update the player data as easily.

Finally, I print out the player's position and the map just to show to the user where he is at.

Round 32
Move Player [w,a,s,d]: a
Player Position [4,2]
Game Map
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 1 0 0 

Now I know that you're probably overwhelmed with information but I found it a very good start for a new C programmer like you to start with so I decided to help (PS: Compiling this answer took longer than writing/updating the code). I'm probably not doing everything as efficiently as possible, but for a case like this I think it has sufficient stuff to keep you occupied for a while. Have fun!

Here is the code

//#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

typedef int bool;
#define TRUE  1
#define FALSE 0


int map[5][5]={
    {0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0}};

int m_print() {
    int i, j;
    printf("Game Map\n");
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 5; j++) {
            printf("%d ", map[i][j]);
        }
        printf("\n");
    }
}

struct player {
    int old_pos_x;
    int old_pos_y;
    int pos_x;
    int pos_y;
    int show;
    int movement;
};

void map_update(struct player *p) {
    map[p->old_pos_x][p->old_pos_y] -= 1;
    map[p->pos_x][p->pos_y] += 1;
}

void move_player(struct player *p, char move){
    //Save current player state
    p->old_pos_x = p->pos_x;
    p->old_pos_y = p->pos_y;

    switch(move) {
        case 'w':
            p->pos_x -= 1;
            break;
        case 's':
            p->pos_x += 1;
            break;
        case 'd':
            p->pos_y += 1;
            break;
        case 'a':
            p->pos_y -= 1;
            break;
    }
    //Fit position to map

    //Rotate right
    p->pos_x %= 5;
    p->pos_y %= 5;

    if (p->pos_x < 0)
        p->pos_x = 4;
    if (p->pos_y < 0)
        p->pos_y = 4;
}

void p_show(struct player *p) {
    //Report Player position
    printf("Player Position [%d,%d]\n", p->pos_x, p->pos_y);    
}

int main() {
    bool game_running = TRUE;

    //Initialize Game
    struct player p;
    //Set player properties
    p.pos_x = 0;
    p.pos_y = 0;

    //Manually add player to map
    map[0][0] = 1;

    int counter = 0;
    char button;
    while (TRUE) {
        printf("Round %d\n", counter);
        //Get input
        printf("Move Player [w,a,s,d]: ");
        scanf(" %c", &button);

        //Update player position
        move_player(&p, button);

        //Update map
        map_update(&p);

        //Report Player
        p_show(&p);
        //Show map
        m_print();
        counter++;
    }

    return 0;
}

Upvotes: 2

Related Questions