C++ 3D Yılan Oyunu

Kurtbeyi

Misafir Editör
Katılım
5 Şubat 2022
Mesajlar
501
Tepkime puanı
44
Konum
istanbul
Cinsiyet
  1. Bay
Takım
Fenerbahçe
Kod:
//--> By lavara123
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <gl/glut.h>

#define UP 1
#define Down 2
#define LEFT 3
#define RIGHT 4

// Durum degiskenleri
GLint   lvl      = 1;
GLint   points = 0;
GLint   size  = 0;
GLbyte  gameOver = true;
GLbyte  EnableLight = true;    

// Yılan değişkenleri
GLint   bodyPos[2][100] = {{}};
GLint   _x       = 5;
GLint   _z       = 10;
GLint   _oldX[2] = {};
GLint   _oldZ[2] = {};
GLbyte  direction  = 0;

// Yem degiskenleri
GLint   _bx      = 0;
GLint   _bz      = 0;

// Ekran degiskenleri
GLint   _w       = 800;
GLint   _h       = 550;
GLint   _Giw     = 0;
GLint   _Gih     = 0;
GLint   _Gfw     = 150;
GLint   _Gfh     = 150;

//Kamera açısı için değişkenler
static GLfloat view_rotx=45.0F ;
static GLfloat view_roty=0.0F ;
static GLfloat view_rotz=0.0F ;

static GLfloat headRotation=90.0F ;
static GLfloat zoom=-300.0F ;

// FPS kontrol değişkenleri
DWORD   g_dwLastFPS = 0;
int        g_nFPS = 0, g_nFrames = 0;

char lavara123[9] = {'l','v','r','1','3','2','a','a','a'};

//aydınlatma sistemi
**** initLight()
{
    //ortam ışığı
    GLfloat ambientColor[] = {0.3f, 0.4f, 0.8f, 1.0f}; 
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

    //konum ışığı
    GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; 
    GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; 
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

    //yön ışığı
    GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; 
    //yönünden gelen (-1, 0.5, 0.5)
    GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
    glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
    glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
}

char lavra123[] = {lavara123[0],
    lavara123[6],
    lavara123[1],
    lavara123[7],
    lavara123[2],
    lavara123[8],
    lavara123[3],
    lavara123[5],
    lavara123[4]};
//ilk yapılandırmalar
**** Initialize(****) 
{
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.7f, 0.9f, 1.0f, 1.0f); //arkaplan rengi: Gök Mavisi
    if(EnableLight){
        glEnable(GL_COLOR_MATERIAL); //Renk etkinleştirme
        glEnable(GL_LIGHTING); //aydınlatma etkinleştir
        glEnable(GL_LIGHT0); //ışığını etkin: #0
        glEnable(GL_LIGHT1); //ışığını etkin: #1
        glEnable(GL_NORMALIZE); //otomatik normalleştirme
    }
}

//Pencere Boyutlandırma
**** resize (int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (double)w / (double)h, 1, 800.0);
}

**** Write(char *string){//ekranda dize yazdır
    while(*string)
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *string++);
}
//bu işlev açılara göre dönen nesneyi doyurur
**** ManipulateViewAngle() {
    glRotatef(view_rotx,1.0,0.0,0.0);//X eksenini döndür
    glRotatef(view_roty,0.0,1.0,0.0);//Y eksenini döndür
    glRotatef(view_rotz,0.0,0.0,1.0);//Z eksenini döndür
}

//bu fonksiyon yılan boyutunu ve yerini sıfırlar...
**** Reset(){
    _x       = 5;
    _z       = 10;
    direction  = 0;
    lvl      = 1;
    points = 0;
    size  = 0;
    gameOver = false;
    view_rotx=45.0F ;
    view_roty=0.0F ;
    view_rotz=0.0F ;
    headRotation=90.0F ;
    zoom=-300.0F ;
}

//Ana Menü
**** WelcomeScreen(){
    char tmp_str[40];

    glColor3f(0, 0, 1);
    glRasterPos2f(0, 10);
    Write("3D Yilan Oyununa Hosgeldiniz.");

    glColor3f(1, 0, 0);
    glRasterPos2f(0, 20);
    Write(lavra123);
    
    glColor3f(0, 0, 1);
    glRasterPos2f(0, 0);
    Write("Baslatmak Icin 'b' Tusuna Basiniz");
}

**** DrawSnake(){
    int  i;

    //kafasının çizimi
    glPushMatrix();
    ManipulateViewAngle();
        //->.
        glPushMatrix();
        glColor3f(0.6f, 0.7f, 0.8f);
        glTranslatef(75.0, -16.0, 75.0);
        glScalef(155,5.0,155);
        glutSolidCube(1);
        glPopMatrix();
    //kafa çizimleri
    glColor3f(1.0,1.0,0.0);//renk: sarı
    glTranslatef(_x,-10.0,_z);//_x & _z'ye göre yer ver
    glScalef(0.5,0.5,0.5);
    glutSolidSphere(10,20,20);//bir baş çizme işlemi
    glRotatef(headRotation, 0.0, 1.0, 0.0);
    glColor3f(1.0,1.0,0.0);
    glTranslatef(0,0.0,6.0);
    glScalef(0.8,1.0,1.0);
    glutSolidCone(10,10,20,20);
    glColor3f(0,0,0);
    glTranslatef(-4.0,10.0,0.0);
    glScalef(0.3,0.3,0.3);
    glutSolidSphere(10,20,20);
    glTranslatef(26.0,0.0,0.0);
    glutSolidSphere(10,20,20);
    glPopMatrix();

    //vücut çizimi
    for(i=0; i<size; i++){//döngü -> boyutu ata -> vücudu temsil eden daireleri çiz
        glPushMatrix();
        ManipulateViewAngle();
        glTranslatef(bodyPos[0][i],-10.0,bodyPos[1][i]);//daire bulma işilemi
        glColor3f(1.0,1.0,0.6);//renk: kırmızı
        glScalef(0.5,0.5,0.5);
        glutSolidSphere(7,20,20);
        glPopMatrix();
    }
}

**** DrawFood()
{
    //yem çizimleri
    glPushMatrix();
    ManipulateViewAngle();
    glTranslatef(_bx,-10.0,_bz);
    glColor3f(0.8, 0.4, 0.4);
    glScalef(0.5,0.5,0.5);
    glutSolidSphere(7,20,20);
    glPopMatrix();
}

**** GameStatus(){

    char tmp_str[40];

    // skor durumunu yazdırma
    glColor3f(0, 0, 0);
    glRasterPos2f(5, 50);

    sprintf(tmp_str, "Level: %d Puan: %d", lvl, points);
    Write(tmp_str);
}

//yem kordinatları için rastgele sayı
int RandomNumber(int high, int low)
{
    return (rand() % (high-low))+low;
}

//yeni yem oluşturma
**** newFood(){
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
    _bx = RandomNumber(_Gfw-_Giw, _Giw+10);
    _bz = RandomNumber(_Gfh-_Gih, _Gih+10);
}

//FPS vb. işlemler
**** getFPS(){
    char tmp_str[40]; 

    if( GetTickCount() - g_dwLastFPS >= 1000 )//->
    {
        g_dwLastFPS = GetTickCount();                    // zaman değişkenini güncelle
        g_nFPS = g_nFrames;                        // FPS'yi kaydet
        g_nFrames = 0;                            // FPS sayacını sıfırla
    }
    g_nFrames++;

    glRasterPos2f(75, 50);
    sprintf(tmp_str, "FPS: %d", g_nFPS);
    Write(tmp_str);
    
    glRasterPos2f(50, 60);
    sprintf(tmp_str, "Pos X: %d Pos Z: %d", _x, _z);
    Write(tmp_str);
}

//çarpışmaları kontrol eden fonksiyon
bool collision(){
    int i;

    for(i=0; i<size; i++){
        if((bodyPos[0][i] == _x && bodyPos[1][i] == _z) || 
        ((bodyPos[0][i] >= _x) && (bodyPos[0][i] <= _x + 5) && (bodyPos[1][i] >= _z) && (bodyPos[1][i] <= _z + 5)) ||
        ((bodyPos[0][i] <= _x) && (bodyPos[0][i] >= _x - 5) && (bodyPos[1][i] <= _z) && (bodyPos[1][i] >= _z - 5)) ||
        ((bodyPos[0][i] <= _x) && (bodyPos[0][i] >= _x - 5) && (bodyPos[1][i] >= _z) && (bodyPos[1][i] <= _z + 5)) ||
        ((bodyPos[0][i] >= _x) && (bodyPos[0][i] <= _x + 5) && (bodyPos[1][i] <= _z) && (bodyPos[1][i] >= _z - 5)))
        return true;
    }
    return false;
}

//hareket fonksiyonu(klavyede basılan tuşa göre) 
**** Run(int value){
    int i;

    _oldX[1] = _x;
    _oldZ[1] = _z;
    switch(direction){
      case RIGHT://sağ yön tuşu basılırsa
          headRotation =90;
          _x += 6;    
          if(_x > _Gfw-2) _x = _Giw-1;//eğer yılan Gfw-2 sınırını geçerse yılanın kordinatını _Giw-1 sınırına eşitle(kısaca duvardan geçme)
          break;
      case LEFT://sol yön tuşu basılırsa
          headRotation =-90;
          _x -= 6;    
         if(_x < 0) _x = _Gfw-2;//eğer yılan 0 konumunu geçerse yılanın kordinatını _Gfw-2'ye eşitle
          break;
      case UP://yukarı yön tuşu basılırsa
          headRotation =0;
          _z += 6;    
          if(_z > _Gfh-2) _z = _Gih-1;//_z noktası büyükse Gfh-2'den: _z noktasını _Gih-1'e eşitle
          break;
      case Down://aşağı yön tuşu basılırsa
          headRotation =180;
          _z -= 6;    
          if(_z < 2) _z = _Gfh-2;//_z noktası küçükse 2'den: _z noktasını Gfh-2'ye eşitle
          break;
    }

    //Collisoin fonksiyonunu kontrol et eğer çarpma oldu ise: gameOver'e aktif et
    if(collision()) gameOver = true;

    //yılanın yemi yediğini kontrol et (X ve Y'nin kontrolu)
    // -->
    if((_x == _bx && _z == _bz) || 
    ((_x >= _bx) && (_x <= _bx + 4) && (_z >= _bz) && (_z <= _bz + 4)) ||
    ((_x <= _bx) && (_x >= _bx - 4) && (_z <= _bz) && (_z >= _bz - 4)) ||
    ((_x <= _bx) && (_x >= _bx - 4) && (_z >= _bz) && (_z <= _bz + 4)) ||
    ((_x >= _bx) && (_x <= _bx + 4) && (_z <= _bz) && (_z >= _bz - 4))){
        points++;
        if(points < 100) size++;
        if(points%5 == 0 && lvl < 15) lvl++;
        newFood();
    }

    for(i = 0; i<size; i++){// vücut parçalarının pozisyonlarını kaydet
        _oldX[0]       = _oldX[1];
        _oldZ[0]       = _oldZ[1];
        _oldX[1]       = bodyPos[0][i];
        _oldZ[1]       = bodyPos[1][i];
        bodyPos[0][i] = _oldX[0];
        bodyPos[1][i] = _oldZ[0];
    }

    //sayacı ayarla
    glutTimerFunc(130-lvl*4, Run, 0);                    
}

**** Display(****){//ekran- fonksiyonu çiz 
    // net ekran
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    if(EnableLight) initLight();

    glTranslatef (-60.0, 40.0, zoom);
    //gameOver true veya false kontrol et oyunu ona göre şekillendir"
    if(!gameOver){
        GameStatus();
        DrawFood();
        DrawSnake();
    }
    else
    {
        WelcomeScreen();
    }
 
    getFPS();

    // ekranı güncellyen
    glutPostRedisplay();
    glutSwapBuffers();
}

**** Special(int key, int x, int y){
    switch(key){
    case GLUT_KEY_RIGHT     :
        if(direction != LEFT)
            direction = RIGHT;
        break;
    case GLUT_KEY_LEFT      :
        if(direction != RIGHT)
            direction = LEFT;
        break;
    case GLUT_KEY_UP        :
        if(direction != UP)
            direction = Down;
        break;
    case GLUT_KEY_DOWN      :
        if(direction != Down)
            direction = UP;
        break;
    }
}

**** keyboard (unsigned char key, int x, int y)
{
    switch (key) {
      //klavye kontrol(pozisyon vs. için)
      case 'x'    ://Grönland'da Antartika'ya döndür.
          view_rotx +=2 ;
          glutPostRedisplay();
          break;
      case 'X'    ://Antartika'dan Grönland'a döndür.
          view_rotx -=2 ;
          glutPostRedisplay();
          break;
      case 'y'    ://ekvator dünüşü +
          view_roty +=2 ;
          glutPostRedisplay();
          break;
      case 'Y'    ://ekvator dünüşü -
          view_roty -=2 ;
          glutPostRedisplay();
          break;
      case 'z'    ://sol tarafı sabit tut sağ tarafı yukarı kaldır
          view_rotz +=2 ;
          glutPostRedisplay();
          break;
      case 'Z'    ://sol tarafı sabit tut sağ tarafı aşağı indir
          view_rotz -=2 ;
          glutPostRedisplay();
          break;
      case 'a'    ://yılana bir daire ekle
          size++ ;
          glutPostRedisplay();
          break;
      case 'A'    ://yılandan bir daire kaldır
          size-- ;
          glutPostRedisplay();
          break;
      case '+'    ://kamerayı yakınlaştırır
          zoom++ ;
          glutPostRedisplay();
          break;
      case '-'://kamerayı uzaklaştırır
          zoom-- ;
          glutPostRedisplay();
          break;
      case 'b'    :
      case 'B'      :
          Reset() ;
          glutPostRedisplay();
          break;

      case 27: //ESC tuşu:
          exit(0);//çıkış
          break;
      default:
          break;
    }
}

int main(****){
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(_w,_h);
    glutInitWindowPosition(80,80);
    glutCreateWindow("3D Yılan Oyunu - (lavara123)");
    glutSpecialFunc(Special);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(Display);
    glutReshapeFunc(resize);

    newFood();
    Run(0);
    
    Initialize();
    glutMainLoop();
}
 
Benzer konular Forum Tarih
Kurtbeyi C ve C++ 2 198
Kurtbeyi C ve C++ 1 196
Kurtbeyi C ve C++ 2 178
Kurtbeyi C ve C++ 1 197
Kurtbeyi C ve C++ 1 158

Benzer konular

İçerik sağlayıcı "paylaşım" sitelerinden biri olan Harbimekan.Com Forum, Eğlence ve Güncel Paylaşım Platformu Adresimizde 5651 Sayılı Kanun’un 8. Maddesine ve T.C.K’nın 125. Maddesine göre TÜM ÜYELERİMİZ yaptıkları paylaşımlardan sorumludur. Harbimekan.Com sitesindeki konular yada mesajlar hakkında yapılacak tüm hukuksal Şikayetler için info@harbimekan.com yada iletişim sayfası üzerinden iletişime geçilmesi halinde ilgili kanunlar ve yönetmelikler çerçevesinde en geç 3 Gün (72 Saat) içerisinde Forum yönetimi olarak tarafımızdan gereken işlemler yapılacaktır.

Bu Site, Bilim ve Sağlık Haber Ajansı Üyesidir.

Yığıntı - 8kez - kaynak mağazam - Uğur Ağdaş