Arama butonu
Bu konudaki kullanıcılar: 1 misafir
4
Cevap
528
Tıklama
0
Öne Çıkarma
C ile yazı tabanlı oyun nasıl yapılabilir
A
3 yıl
Çavuş
Konu Sahibi

Arkadaşlar merhaba c4droid ile C yazı tabanlı oyun yapmak istiyorum görsel olmayacak sadece yazı nasıl yapabilirim

Örnek kodu yazsanız ben devam ettirsem 1 - 2 satır



S
3 yıl
Yüzbaşı

#include <stdio.h>
int main()
{
printf("Bu bir oyundur.\n");
return 0;
}



< Bu ileti mobil sürüm kullanılarak atıldı >

M
3 yıl
Çavuş

rand() fonksiyonunu bolca kullanarak bir oyun yapabilirsiniz.Örneğin iki büyücü karakter var , rand() kullanarak farklı şiddette saldırılar yapıyorlar.Hayatta kalan kazanıyor vs.



G
3 yıl
Yarbay

Merhaba,

Forumu gezerken konunuzu gördüm. 1. sınıfta C programlama dersi alırken bize şu tarz bir ödev vermişlerdi, oldukça basit bir seviyede ama size örnek olması açısından atıyorum. Örnek ekran görüntüsü de var:
< Resime gitmek için tıklayın >


C Dosyası indirme linki: https://s4.dosya.tc/server13/74hui0/Homework_4.c.html

Aynı zamanda kodları buraya code içerisine de yapıştırıyorum.

Oyunu özetlemek gerekirse, basitçe bir oyuncumuz ve bir haritamız var. Oyuncumuz P ile gösteriliyor ve başlangıçta 8,8 pozisyonunda ve 100 cana sahip. Harita 15x15 büyüklüğünde hücrelerden oluşuyor. Haritada düşmanlar var, düşmanla karşılaşınca oyuncu savaşıyor ve hasar alıyor. Aynı zamanda Token'ler var. Bunları toplayıp can alabiliyorsun. Bu değerler random oluşturuluyor tabi. Her başlangıçta harita da öyle, sadece Player 8,8 noktasında. Can 0'a düşünce oyun bitiyor. Konsola komut olarak W,A,S,D girerek ileri, sol, geri, sağ ilerleyebiliyorsun. Q girince de oyundan çıkıyorsun. Çıkarken de final pozisyonları gösteriyor.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define SOM 15


//SOM -> Size Of Map constant


/*Structs Defined*/


struct Position {
    int x, y;
};


struct Player {
    int health;
    struct Position position;
};


struct Cell {
    int token, enemy;
};


/*A function to create enemy values randomly by rand and srand between 1 and 30*/


int randomEnemies(int seed) {
    srand(seed); // srand will generate random values to rand function by using 'seed' value
    int enemy = (rand() % 30) + 1; // create random values between 1 and 30
    // to do that, create a random number and get modulus of it with 30 so the value will be between 0 and 29
    // then add 1 to the value so the new value will be between 1 and 30
    return enemy; // return value
}


int randomTokens(int seed) {
    int token;
    srand(seed); // same logic as above
    // a loop is necessary in this solution
    // the randomly token value must not be 0
    // if the value is 0 then it must get another value
    // until the value is not 0
    do {
        token = (rand() % 16) - 5; // create a random value between 0 and 15
        // then add -5 so the new value will be between -5 and 10
    } while (token == 0);
    return token; // return value
}


// a function to generate random coordinate values like above
// then those values will return as pointers


void randomCoordinates(int *x, int *y, int seed) {
    srand(seed);
    *x = (rand() % SOM);
    *y = (rand() % SOM);
}


// a function to create the map
// the map will be a "Cell" 2D array struct which has sizes of "SOM"
// in this solution, I used 2 loops
// There will be line and there will be lines and columns so 2 loops necessary
// the upper limit of loops will be "SOM" because the map must be this size


void createMap(struct Cell map[SOM][SOM], int playerX, int playerY) {
    for (int i = 0; i < SOM; i++) {
        for (int j = 0; j < SOM; j++) {
            printf("|"); // for formatting


            if (j == playerX && i == playerY) // player's initial position
                printf("P"); // if the values equals then set player's icon to there
            else if (map[j][i].enemy == 0) // if there is no enemy, set a blank character
                printf(" ");
            else // otherwise mark as "E"nemy
                printf("E");


            if (map[j][i].token == 0) // like above, if there is no token set blank...
                printf(" ");
            else // otherwise mark as "T"oken
                printf("T");
        }
        printf("|\n"); // for formatting
    }
}


// a function to run player's movement and fight actions
// the switch case will set the keyboard settings, for movement or quitting from game


void movementScript(char move, struct Player *player, struct Cell map[SOM][SOM]) {
    switch (move) {
        case 'W':
            player->position.y -= 1; // it must be minus instead of plus because
            break; // when the value decreases the player will go upper, it must be opposite because of array values
        case 'S':
            player->position.y += 1; // same logic is valid for other moves
            break;
        case 'D':
            player->position.x += 1;
            break;
        case 'A':
            player->position.x -= 1;
            break;
        case 'Q': // quit
            return;
    }


    // check is there any token or enemy, then tell that to player


    if (map[player->position.x][player->position.y].token == 0 &&
        map[player->position.x][player->position.y].enemy == 0) {
        printf("You are now in an Empty Cell.\n");
    }


    // check is there any token, then add the value of token to player's health
    // notify the player then set the cell as "no token"


    if (map[player->position.x][player->position.y].token != 0) {
        player->health += map[player->position.x][player->position.y].token;
        printf("You collected a Token, Your Remaining Health: %d\n", player->health);
        map[player->position.x][player->position.y].token = 0;
    }


    // check is there any enemy, then fight with the enemy
    // after the fight set new health value and notify the player
    // then set the cell as "no enemy"


    if (map[player->position.x][player->position.y].enemy != 0) {
        player->health -= map[player->position.x][player->position.y].enemy;
        printf("You faced an Enemy, Your Remaining Health: %d\n", player->health);
        map[player->position.x][player->position.y].enemy = 0;
    }
}


int main() {


    struct Player player; // create struct player
    char userDecision; // user decision for movement or quit
    struct Cell map[SOM][SOM]; // create struct of map, cells
    int x, y, k; // x and y for coordinates, k for random value which will be used for
    // generate random seed value for srand, this is necessary in this solution
    // if I use directly time as seed value, everytime when the game runs it will generate same map
    // so it won't be a real "random" map of tokens and enemies
    player.health = 100; // set player health
    player.position.x = 8; // set player positions
    player.position.y = 8;


    srand(time(NULL)); // use time value for seed rand to generate a random number


    // generate random enemy and token values to set the map and cells


    for (int i = 0; i < SOM; i++) {
        for (int j = 0; j < SOM; j++) {
            map[i][j].enemy = 0;
            map[i][j].token = 0;
        }
    }


    // set those random enemies to map
    // there will be 30 enemies


    for (int i = 0; i < 30;) {
        k = rand(); // the k value is a random number to seed another rand function, the time will seed the k value
        // then this random k value will seed the coordinates and values of enemies so it will be totally random
        randomCoordinates(&x, &y, k); // random coordinate for an enemy which seeded by random k value
        if (map[x][y].enemy == 0) { // if there is no enemy in this random cell
            map[x][y].enemy = randomEnemies(k); // set the enemy which has a random fight power (seeded by k)
            i++; // then increase the enemy counter, it must be in "if" condition because if it cannot generate an enemy
            // it must not increase the counter, if it increases it may not generate 30 enemies, it can be less
        }
    }


    // set those random tokens to map
    // there will be 10 tokens
    // logic is same as above


    for (int i = 0; i < 10;) {
        k = rand(); // random k value to seed other rand functions which are in randomCoordinates and randomTokens functions
        randomCoordinates(&x, &y, k); // same logic as above
        if (map[x][y].token == 0) { // if there is no token in this random cell
            map[x][y].token = randomTokens(k); // set the token and its random value which is between -5 and 10
            i++; // increase token counter
        }


    }


    printf("Initial Positions:\n\n"); // notify the user
    createMap(map, player.position.x, player.position.y); // create starting map by using this func


    printf("\n\n"); // for formatting to get better visual


    do {
        printf("Enter a Move('W','A','S','D','Q'):"); // a loop for getting user commands and run those commands
        scanf(" %c", &userDecision); // get user decision
        movementScript(userDecision, &player, map); // execute user decision
        if (player.health <= 0) // check user's health
            printf("YOU ARE DEAD!\n"); // if it is below or equal 0 then "game over"
    } while (userDecision != 'Q' && player.health > 0); // the game will run until user has 0 or less health or user (Q)uit


    printf("\nFinal Positions:\n\n"); // notify the user
    createMap(map, player.position.x, player.position.y); // show final positions of map and user
    printf("\n"); // formatting
    system("PAUSE"); // pause the screen to see result


    return EXIT_SUCCESS; // exit successfully
}




A
3 yıl
Çavuş
Konu Sahibi

Çok teşekkür ederim



DH Mobil uygulaması ile devam edin. Mobil tarayıcınız ile mümkün olanların yanı sıra, birçok yeni ve faydalı özelliğe erişin. Gizle ve güncelleme çıkana kadar tekrar gösterme.