C# CONSOLE YILAN OYUNU

2,200 views
Skip to first unread message

cansuuk...@gmail.com

unread,
Mar 4, 2016, 1:35:27 PM3/4/16
to C SHARP UYGULAMALAR FORUM
Merhaba ben lise 3e gidiyorum. Öğretmenim benden Visual Studio'da c# Console ile bir yılan oyunu yapmamı istedi fakat bu konuda fazla bir bilgim yok, acaba bana yardımcı olabilir misiniz? Eğer yardım ederseniz çok sevinirim :)))(() ÖNCELİĞİM EKRANDA ORTAYA BİR ŞEKİL KOYMAK :((((((

c sharp uygulamalar

unread,
Mar 4, 2016, 3:55:10 PM3/4/16
to C SHARP UYGULAMALAR FORUM
Aşağıdaki linkte bir örnek var. Anlamadığım kısım olursa buradan sorabilirsin.

http://pastebin.com/q8GMmRrf

cansuuk...@gmail.com

unread,
Mar 6, 2016, 2:51:57 AM3/6/16
to C SHARP UYGULAMALAR FORUM
verdiğiniz internet sitesi koruma altındaymış, erişim engelli diyor :(

c sharp uygulamalar

unread,
Mar 6, 2016, 3:41:56 AM3/6/16
to C SHARP UYGULAMALAR FORUM



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace ConsoleSnake
{
    class Vector2
    {
        public int X;
        public int Y;
        public Vector2(int x, int y)
        {
            X = x;
            Y = y;
        }
    }

    class Program
    {
        static List<Vector2> snakePos = new List<Vector2>();
        static int direction = 1;
        static Random r = new Random();
        static Vector2 foodPos;
        static double lastT = 0;
        static int sN = 0;
        static Vector2 screenBounds = new Vector2(80, 24);
        static ConsoleColor[] rainbowColor = new ConsoleColor[] {ConsoleColor.Red, ConsoleColor.Yellow, ConsoleColor.Green, ConsoleColor.Blue};
        static bool canControl = true;

        static void reset()
        {
            snakePos.Clear();
            foodPos = new Vector2(r.Next(1, screenBounds.X), r.Next(1, screenBounds.Y));
            for (int i = 0; i < 6; i++)
                snakePos.Add(new Vector2(i + 1, 6));
            draw(true);
        }

        static void getInput()
        {
            while (!canControl)
            {
            }
                ConsoleKey ck = Console.ReadKey().Key;
                if (ck == ConsoleKey.UpArrow && direction != 1)
                    direction = 0;
                else
                    if (ck == ConsoleKey.DownArrow && direction != 0)
                        direction = 1;
                    else
                        if (ck == ConsoleKey.LeftArrow && direction != 3)
                            direction = 2;
                        else
                            if (ck == ConsoleKey.RightArrow && direction != 2)
                                direction = 3;
                            else
                                if (ck == ConsoleKey.Spacebar)
                                    draw(true);
                canControl = false;
            getInput();
        }

        static void Main(string[] args)
        {
            reset();

            new Thread(getInput).Start();
            draw(true);

            while (true)
            {
                for (int i = snakePos.Count - 1; i > 0; i--)
                    snakePos[i] = snakePos[i - 1];

                if (direction == 0)
                    snakePos[0] = new Vector2(snakePos[0].X, snakePos[0].Y - 1);
                if (direction == 1)
                    snakePos[0] = new Vector2(snakePos[0].X, snakePos[0].Y + 1);
                if (direction == 2)
                    snakePos[0] = new Vector2(snakePos[0].X - 1, snakePos[0].Y);
                if (direction == 3)
                    snakePos[0] = new Vector2(snakePos[0].X + 1, snakePos[0].Y);

                foreach (Vector2 sp in snakePos)
                    if (sp.X == foodPos.X && sp.Y == foodPos.Y)
                    {
                        foodPos = new Vector2(r.Next(1, screenBounds.X), r.Next(1, screenBounds.Y));
                        snakePos.Add(snakePos[snakePos.Count - 1]);
                        break;
                    }

                if (snakePos[0].Y < 0)
                    snakePos[0].Y = screenBounds.Y - 1;

                if (snakePos[0].X < 0)
                    snakePos[0].X = screenBounds.X - 1;

                if (snakePos[0].Y > screenBounds.Y - 1)
                    snakePos[0].Y = 0;

                if (snakePos[0].X > screenBounds.X - 1)
                    snakePos[0].X = 0;

                for (int i = snakePos.Count - 2; i > 0; i--)
                    if (snakePos[i].X == snakePos[0].X && snakePos[i].Y == snakePos[0].Y)
                    {
                        reset();
                        break;
                    }
                canControl = true;
                draw(true);
                Thread.Sleep(50);
            }
        }

        static void draw(bool clear)
        {
            if (!clear)
            {
                foreach (Vector2 pos in snakePos)
                {
                    Console.SetCursorPosition(pos.X, pos.Y);
                    Console.Write('O');
                }
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.SetCursorPosition(foodPos.X, foodPos.Y);
                Console.Write('*');
                Console.ForegroundColor = ConsoleColor.Gray;
                
                Console.SetCursorPosition(snakePos[snakePos.Count - 1].X, snakePos[snakePos.Count - 1].Y);
                Console.Write(' ');
            }
            else
            {
                Console.Clear();
                char[,] drawChars = new char[screenBounds.X, screenBounds.Y];

                drawChars[0, 0] = 'a';
                for (int x = 0; x < drawChars.GetLength(0); x++)
                    for (int y = 0; y < drawChars.GetLength(1); y++)
                        drawChars[x, y] = ' ';

                for (int i = snakePos.Count - 2; i > 0; i--)
                    drawChars[snakePos[i].X, snakePos[i].Y] = 'O';
                drawChars[foodPos.X, foodPos.Y] = '*';

                for (int y = 0; y < drawChars.GetLength(1); y++)
                    for (int x = 0; x < drawChars.GetLength(0); x++)
                    {
                        if (drawChars[x, y] == '*')
                            Console.ForegroundColor = ConsoleColor.Yellow;

                        for (int i = 0; i < snakePos.Count - 2; i++)
                            if (x == snakePos[i].X && y == snakePos[i].Y && false)
                            {
                                int ii = i;
                                while (ii > rainbowColor.Length - 1)
                                    ii -= rainbowColor.Length;
                                Console.ForegroundColor = rainbowColor[ii];
                            }

                        Console.Write(drawChars[x, y]);
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
            }
        }
    }
}

cansuuk...@gmail.com

unread,
Mar 6, 2016, 5:41:18 PM3/6/16
to C SHARP UYGULAMALAR FORUM
çok teşekkür ederim resmen hayatımı kurtardınız :))

irem önem

unread,
Dec 23, 2022, 9:18:04 AM12/23/22
to C SHARP UYGULAMALAR FORUM
Bu kodun doğru çalıştığından emin miyiz?

6 Mart 2016 Pazar tarihinde saat 10:41:56 UTC+2 itibarıyla c sharp uygulamalar şunları yazdı:
Reply all
Reply to author
Forward
0 new messages