Türkçe: Merhaba arkadaşlar, Youtube kanalınin içeriklerini anlayarak ve yorumlayarak tekrar kodluyorum. İlgisini çekebilecek arkadaşlar için kod ve örnek resmi buraya koyuyorum.
English : Hi guys, I am re-coding the contents of the Youtube channel FamTrinli by understanding and commenting. I will post the code and the example picture here for those who might be interested in.
French : Salut les gars, je recode le contenu de la chaîne Youtube qui s'appele FamTrinli en comprenant et en commentant. Je publierai le code et l'image d'exemple ici pour ceux qui pourraient être intéressés.
English : Hi guys, I am re-coding the contents of the Youtube channel FamTrinli by understanding and commenting. I will post the code and the example picture here for those who might be interested in.
French : Salut les gars, je recode le contenu de la chaîne Youtube qui s'appele FamTrinli en comprenant et en commentant. Je publierai le code et l'image d'exemple ici pour ceux qui pourraient être intéressés.
Bağlantıları görmek için lütfen
Giriş Yap
C++:
#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;
int N = 30, M = 20;
int size = 16;
int width = size * N;
int height = size * M;
int dir, num = 4;
struct Snake
{
int x, y;
}s[100]; /*100 adet x ve y kordinatlarını içeren struct üretiyoruz*/
struct Fruct /*Yem Kordinatları*/
{
int x, y;
}f;
void Tick()
{
/// <summary>
/// Yılanın kuyruğunun onu takip etmesi için eşitliyoruz. Örnek : Yılan'ın X'i eşittir Yılan'ın X'i - 1; Eğer bu olmaz ise
/// ilk yılan parçası yalnız hareket eder, diğerleri ilk doğulan yerde kalır.
/// </summary>
for (int i = num; i > 0; --i)
{
s[i].x = s[i - 1].x;
s[i].y = s[i - 1].y;
}
if (dir == 0) s[0].y += 1; // Yukarı
if (dir == 1) s[0].x -= 1; // Sol
if (dir == 2) s[0].x += 1; // Sağ
if (dir == 3) s[0].y -= 1; // Aşağı
if ((s[0].x == f.x) && (s[0].y == f.y)) /*YILAN VE YEM KORDİNATLARI EŞİT İSE RANDOM YEM ÜRETİYORUZ VE YILAN KUYRUK SAYISINI ARTTIRIYORUZ*/
{
num++;
f.x = rand() % N;
f.y = rand() % M;
}
if (s[0].x > N) s[0].x = 0; if (s[0].x < 0) s[0].x = N; /*Yılanın ekrandan taşması durumunda diğer taraftan çıkmasını sağlıyoruz; yukarıdan çıkarsa aşağıdan, aşağıdan çıkarsa yukarıdan girecek oyuna yılan*/
if (s[0].y > M) s[0].y = 0; if (s[0].y < 0) s[0].y = M;
for (int i = 1; i < num; i++)
{
if (s[0].x == s[i].x && s[0].y == s[i].y)
num = i;
/*Yılan kendi kuyruğuna çarpar ise */
}
}
int main()
{
srand(time(0));
// Oyun Ekranını oluşturuyoruz ve önceden belirlediğimiz boyutları ekran boyutu olarak atıyoruz.
RenderWindow window(VideoMode(width, height), "Snake!");
window.setFramerateLimit(60);
// Görüntüyü ekrana yansıtalım..
Texture t1, t2;
t1.loadFromFile("images/white.png");
t2.loadFromFile("images/red.png");
Sprite sprite1(t1);
Sprite sprite2(t2);
Clock clock;
float timer = 0, delay = 0.1;
f.x = 10; /*Yem başlangıç değeri*/
f.y = 10;
// SFML Oyun döngüsünü oluşturalım
while (window.isOpen())
{
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
if (Keyboard::isKeyPressed(Keyboard::Left)) dir = 1;
if (Keyboard::isKeyPressed(Keyboard::Right)) dir = 2;
if (Keyboard::isKeyPressed(Keyboard::Up)) dir = 3;
if (Keyboard::isKeyPressed(Keyboard::Down)) dir = 0;
if (timer > delay) /*Oyun çok hızlı olmasın diye gecikme koyuyoruz.*/
{
timer = 0;
Tick();
}
////// Oyunu Ekrana Çizelim //////
window.clear();
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
sprite1.setPosition(i * size, j * size); /*Beyaz resmi ekrana 16x16 olarak yazıyoruz*/
window.draw(sprite1); // Ekrana çizelim
}
for (int i = 0; i < num; i++)
{
sprite2.setPosition(s[i].x * size, s[i].y * size); /*Kırmızı resmi ekrana 16x16 olarak yazıyoruz */
window.draw(sprite2);
}
sprite2.setPosition(f.x * size, f.y * size); /*Kırmızı resmi ekrana 16x16 olarak yazıyoruz, yem niyetine */
window.draw(sprite2);
window.display();
}
return 0;
}