c# – Final Testing Of Snake Game

Hello!

I’ve been posting here as of recently because i need help with my final update of my code!

I know that everything is kinda of a mess but i’ll try to explain as best i can.

I have three different classes: Starter, GameWorld, TestClass (And an interface).
The GameWorld has two methods, one for the boarder of the game and one to clear the console.

The Starter Class has a method called “GameSense” which bascially has everything about the food and the behavior of my snake.

Now i’ve been trying to make a tests using a constructor in a Class within my Starter Class named “Pixel”. Pixel has the X/Y values and background color i need. PS: Pixel is at the bottom of the Starter Class.

All i want is to be able to test different things such as “Will (2,4)+(1,3) = (3,7)?”.

Any kinda of help or anything you would like me to fix and ill be right on it!
I am trying to learn as much as possible about testing right now!
Thanks in advance!

StarterClass:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Snake
{
    /// <summary>
    /// Start class, everything goes from here.
    /// Made by Karwan!
    /// </summary>
    public class Starter : InterF
    {
        private static Direction movement;

        public enum Direction { Up, Down, Left, Right }
        public static object Key { get; private set; }

        /// <summary>
        /// Main method.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string() args)
        {
            //Making the text green and also given a player 
            //options to chose from. 
            Console.ForegroundColor = ConsoleColor.Green;
            bool loopC = true;
            while (loopC)
            {
                string mystring = null; Console.WriteLine("Welcome to Snake.nPress S+Enter to start or press Q+Enter to exit.");
                mystring = Console.ReadLine();
                switch (mystring)
                {
                    case "Q":
                        Environment.Exit(0);
                        break;
                    case "S":
                        Console.WriteLine("Starting game!");
                        System.Threading.Thread.Sleep(4000);
                        loopC = false;
                        break;
                    default:
                        Console.WriteLine("Invalid Entry.. Try again.");
                        break;
                }

            }

            //Call for GameSense if
            //they want to play.
            GameSense();

        }



        /// <summary>
        /// Game object!
        /// </summary>
        public static void GameSense()

        {
            //Game console height/width.
            Console.WindowHeight = 30;
            Console.WindowWidth = 70;
            int screenwidth = Console.WindowWidth;
            int screenheight = Console.WindowHeight;
            Random randomnummer = new Random();
            //Lenght of tail == current score.
            int score = 2;
            int gameover = 0;
            //Gather positions from pixel class.
            pixel positions = new pixel();
            positions.xpos = screenwidth / 2;
            positions.ypos = screenheight / 2;
            positions.Black = ConsoleColor.Red;
         
            //Position manegment.
            List<int> xpos = new List<int>();
            List<int> ypos = new List<int>();
            int berryx = randomnummer.Next(0, screenwidth);
            int berryy = randomnummer.Next(0, screenheight);
            //Time manegment.
            DateTime time1 = DateTime.Now;
            DateTime time2 = DateTime.Now;
       


            //Draw world from GameWorld.cs.
            GameWorld.DrawBorder(screenwidth, screenheight);

            while (true)
            {
                GameWorld.ClearConsole(screenwidth, screenheight);
                if (positions.xpos == screenwidth - 1 || positions.xpos == 0 || positions.ypos == screenheight - 1 || positions.ypos == 0)
                {
                    gameover = 1;
                }

                Console.ForegroundColor = ConsoleColor.Green;
                if (berryx == positions.xpos && berryy == positions.ypos)
                {
                    score++;
                    berryx = randomnummer.Next(1, screenwidth - 2);
                    berryy = randomnummer.Next(1, screenheight - 2);
                }
                for (int i = 0; i < xpos.Count(); i++)
                {
                    Console.SetCursorPosition(xpos(i), ypos(i));
                    Console.Write("*");
                    if (xpos(i) == positions.xpos && ypos(i) == positions.ypos)
                    {
                        gameover = 1;
                    }
                }
                if (gameover == 1)
                {
                    break;
                }
                Console.SetCursorPosition(positions.xpos, positions.ypos);
                Console.ForegroundColor = positions.Black;
                Console.Write("*");
                //Food color & position.
                Console.SetCursorPosition(berryx, berryy);
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("*");
                Console.CursorVisible = false;
                time1 = DateTime.Now;
     
                while (true)
                {
                    time2 = DateTime.Now;
                    if (time2.Subtract(time1).TotalMilliseconds > 500) { break; }
                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo info = Console.ReadKey(true);
                        if (info.Key.Equals(ConsoleKey.UpArrow) && movement != Direction.Down)
                        {
                            movement = Direction.Up;
                        }
                        else if (info.Key.Equals(ConsoleKey.DownArrow) && movement != Direction.Up)
                        {
                            movement = Direction.Down;
                        }
                        else if (info.Key.Equals(ConsoleKey.LeftArrow) && movement != Direction.Right)
                        {
                            movement = Direction.Left;
                        }
                        else if (info.Key.Equals(ConsoleKey.RightArrow) && movement != Direction.Left)
                        {
                            movement = Direction.Right;
                        }
                    }
                }
                //Giving the connections value
                //to change x/y to make the movment happen.
                xpos.Add(positions.xpos);
                ypos.Add(positions.ypos);
                switch (movement)
                {
                    case Direction.Up:
                        positions.ypos--;
                        break;
                    case Direction.Down:
                        positions.ypos++;
                        break;
                    case Direction.Left:
                        positions.xpos--;
                        break;
                    case Direction.Right:
                        positions.xpos++;
                        break;
                }
                if (xpos.Count() > score)
                {
                    xpos.RemoveAt(0);
                    ypos.RemoveAt(0);
                }
            }
            Console.SetCursorPosition(20, screenheight / 2);
            Console.WriteLine("Game over, Score: " + score);
            Console.SetCursorPosition(20, screenheight / 2 );
            System.Threading.Thread.Sleep(1000);
            restart();

        }

        /// <summary>
        /// Restarter.
        /// </summary>
        public static void restart()
        {
            Console.WindowHeight = 30;
            Console.WindowWidth = 70;
            int screenwidth = Console.WindowWidth;
            int screenheight = Console.WindowHeight;
 
            string Over = null; Console.WriteLine("n             Would you like to start over? Y/N");
            Console.SetCursorPosition(20, screenheight/2 +2 );
            bool O = true;

            while (O)
            {
                Over = Console.ReadLine();
                switch (Over)
                {
                    case "Y":
      
                        Console.WriteLine("n              Restarting!");
                        System.Threading.Thread.Sleep(2000);
                        
            
                        GameSense(); 
                        break;
                    case "N":
                        Console.WriteLine("n                Thank you for playing!");
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("Invalid Entry.. Try again.");
                        break;
                }
            }

        }
        /// <summary>
        /// Set/get pixel position.
        /// </summary>
        public class pixel
        {

            public int xpos { get; set; }
            public int ypos { get; set; }
            public ConsoleColor Black { get; set; }
        }
    }
}

GameWorld Class:

using System;
using System.Linq;
using static Snake.Starter;

public class GameWorld 
{

    /// <summary>
    /// Clear console for snake.
    /// </summary>
    /// <param name="screenwidth"></param>
    /// <param name="screenheight"></param>
    public static void ClearConsole(int screenwidth, int screenheight)
    {
        var blackLine = string.Join("", new byte(screenwidth - 2).Select(b => " ").ToArray());
        Console.ForegroundColor = ConsoleColor.Black;
        for (int i = 1; i < screenheight - 1; i++)
        {
            Console.SetCursorPosition(1, i);
            Console.Write(blackLine);
        }
    }

    /// <summary>
    /// Draw boared.
    /// </summary>
    /// <param name="screenwidth"></param>
    /// <param name="screenheight"></param>
    public static void DrawBorder(int screenwidth, int screenheight)
    {
        var horizontalBar = string.Join("", new byte(screenwidth).Select(b => "■").ToArray());

        Console.SetCursorPosition(0, 0);
        Console.Write(horizontalBar);
        Console.SetCursorPosition(0, screenheight - 1);
        Console.Write(horizontalBar);

        for (int i = 0; i < screenheight; i++)
        {
            Console.SetCursorPosition(0, i);
            Console.Write("■");
            Console.SetCursorPosition(screenwidth - 1, i);
            Console.Write("■");
        }
    }

}

Interface:

using System;

public class InterF
{

    /// <summary>
    /// Interface for Starter.
    /// </summary>
    interface INterF
    {

        void GameSense();
        void DrawBoarder();
        void ClearConsole(); 

    }
}

And the test class, the class i need to update.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using Snake;
using System;
using System.Collections.Generic;
using System.Text;
using static Snake.Starter;

namespace Snake.Tests
{
    (TestClass())
    public class StarterTests
    {
        (TestMethod())

        public void Test()
        {
            // pixel a = new pixel(1, 2);
            // pixel b = new pixel(3, 4);
            /// pixel c = a + b;

        //Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(c.xpos, 4);
        //Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(c.ypos, 6);
    }
    }
}

Is there a book on the algorithms and code needed to build most popular board and card game?

Is there a book on the algorithms and code needed to build most popular board and card game? I am trying to find some useful resources to build a bunch of mini games. I am trying to build every game in Clubhouse Games: 51 Worldwide Classics in less than a year, but to do that I need to have the algorithms handed out to me given I am working full time. Is there any good reference book that would help me do this?

python – Im doing a game

I really don’t know how to handle this nothing shows up in my pygame window not even the screen.fill line doesn’t do anything anyone know

import pygame
def game():
pygame.init()

black = (0, 0, 0)
green = (0,255,0)
running = True

screen = pygame.display.set_mode((800,600))

screen.fill(green)

button_rectangle = pygame.draw.rect(screen,black,(200,150,100,50))


while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

game()

c# – Object orianted programing in a game

Right now i have a snake game that is working but i need a way to make my code look better and work better.

A method i named “GameSense” is very long and i need a way to make it more object oriented. I want to use Enums.

This is what my teacher sent back to me:

The game is very nice and nicely working tail! The test you have also follows the right idea but uses an UpdatePosition method that you have not implemented. I’m also thinking about your class pixel – you do not use it for anything in the rest of the game. You should use it instead of e.g. berryx, berryy. What I am trying to access is more use of object orientation and division into methods, and not a single long method like GameSense right now. Also want to see you use enum / bool instead of strings for e.g. the variables movement and buttonpressed. However, I’m glad you got an interface on a corner!

As you can see… i feel pretty suck right now.
What do i need to do here inorder to make the program be more object orianted?
Any tips along the way would be super helpful!
Thanks!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Snake
{
    /// <summary>
    /// Start class, everything goes from here.
    /// Made by Karwan!
    /// </summary>
   public class Starter : InterF
    { 
        public static object Key { get; private set; }

        /// <summary>
        /// Main method.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string() args)
        {
            //Making the text green and also given a player 
            //options to chose from. 
            Console.ForegroundColor = ConsoleColor.Green;
            bool loopC = true;
            while (loopC)
            {
                string mystring = null; Console.WriteLine("Welcome to Snake.nPress S+Enter to start or press Q+Enter to exit.");
                mystring = Console.ReadLine();
                switch (mystring)
                {
                    case "Q":
                        Environment.Exit(0);
                        break;
                    case "S":
                        Console.WriteLine("Starting game!");
                        System.Threading.Thread.Sleep(4000);
                        loopC = false;
                        break;
                    default:
                        Console.WriteLine("Invalid Entry.. Try again.");
                        break;
                }

            }

            //Call for GameSense if
            //they want to play.
            GameSense(); 

        }


      
        /// <summary>
        /// Game object!
        /// </summary>
        public static void GameSense()

        {
            //Game console height/width.
            Console.WindowHeight = 30;
            Console.WindowWidth = 70;
            int screenwidth = Console.WindowWidth;
            int screenheight = Console.WindowHeight;
            Random randomnummer = new Random();
            //Lenght of tail == current score.
            int score = 2;
            int gameover = 0;
            //Gather positions from pixel class.
            pixel positions = new pixel();
            positions.xpos = screenwidth / 2;
            positions.ypos = screenheight / 2;
            positions.Black = ConsoleColor.Red;
            string movement = "RIGHT";
            //Position manegment.
            List<int> xpos = new List<int>();
            List<int> ypos = new List<int>();
            int berryx = randomnummer.Next(0, screenwidth);
            int berryy = randomnummer.Next(0, screenheight);
            //Time manegment.
            DateTime time1 = DateTime.Now;
            DateTime time2 = DateTime.Now;
            string buttonpressed = "no";

            
            //Draw world from GameWorld.cs.
            GameWorld.DrawBorder(screenwidth, screenheight);

            while (true)
            {
                GameWorld.ClearConsole(screenwidth, screenheight);
                if (positions.xpos == screenwidth - 1 || positions.xpos == 0 || positions.ypos == screenheight - 1 || positions.ypos == 0)
                {
                    gameover = 1;
                }

                Console.ForegroundColor = ConsoleColor.Green;
                if (berryx == positions.xpos && berryy == positions.ypos)
                {
                    score++;
                    berryx = randomnummer.Next(1, screenwidth - 2);
                    berryy = randomnummer.Next(1, screenheight - 2);
                }
                for (int i = 0; i < xpos.Count(); i++)
                {
                    Console.SetCursorPosition(xpos(i), ypos(i));
                    Console.Write("*");
                    if (xpos(i) == positions.xpos && ypos(i) == positions.ypos)
                    {
                        gameover = 1;
                    }
                }
                if (gameover == 1)
                {
                    break;
                }
                Console.SetCursorPosition(positions.xpos, positions.ypos);
                Console.ForegroundColor = positions.Black;
                Console.Write("*");
                //Food color & position.
                Console.SetCursorPosition(berryx, berryy);
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("*");
                Console.CursorVisible = false;
                time1 = DateTime.Now;
                buttonpressed = "no";
                while (true)
                {
                    time2 = DateTime.Now;
                    if (time2.Subtract(time1).TotalMilliseconds > 500) { break; }
                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo info = Console.ReadKey(true);
                        //Connecting the buttons to the x/y movments. 
                        if (info.Key.Equals(ConsoleKey.UpArrow) && movement != "DOWN" && buttonpressed == "no")
                        {
                            movement = "UP";
                            buttonpressed = "yes";
                        }
                        if (info.Key.Equals(ConsoleKey.DownArrow) && movement != "UP" && buttonpressed == "no")
                        {
                            movement = "DOWN";
                            buttonpressed = "yes";
                        }
                        if (info.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT" && buttonpressed == "no")
                        {
                            movement = "LEFT";
                            buttonpressed = "yes";
                        }
                        if (info.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT" && buttonpressed == "no")
                        {
                            movement = "RIGHT";
                            buttonpressed = "yes";
                        }
                    }
                }
                //Giving the connections value
                //to change x/y to make the movment happen.
                xpos.Add(positions.xpos);
                ypos.Add(positions.ypos);
                switch (movement)
                {
                    case "UP":
                        positions.ypos--;
                        break;
                    case "DOWN":
                        positions.ypos++;
                        break;
                    case "LEFT":
                        positions.xpos--;
                        break;
                    case "RIGHT":
                        positions.xpos++;
                        break;
                }
                if (xpos.Count() > score)
                {
                    xpos.RemoveAt(0);
                    ypos.RemoveAt(0);
                }
            }
            Console.SetCursorPosition(screenwidth / 5, screenheight / 2);
            Console.WriteLine("Game over, Score: " + score);
            Console.SetCursorPosition(screenwidth / 5, screenheight / 2 + 1);
            System.Threading.Thread.Sleep(1000);
            restart();
            
        }

        /// <summary>
        /// Restarter.
        /// </summary>
        public static void restart()
        {
        
            string Over = null; Console.WriteLine("nWould you like to start over? Y/N");
            bool O = true;

            while (O)
            {
                Over = Console.ReadLine();
                switch (Over)
                {
                    case "Y":
                        Console.WriteLine("nRestarting!");
                        System.Threading.Thread.Sleep(2000);
                        break;
                    case "N":
                        Console.WriteLine("nThank you for playing!");
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("Invalid Entry.. Try again.");
                        break;
                }
            }

        }
        /// <summary>
        /// Set/get pixel position.
        /// </summary>
       public class pixel
        {
            public int xpos { get; set; }
            public int ypos { get; set; }
            public ConsoleColor Black { get; set; }
        }
    }
}

My test class

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using Snake;
using System;
using System.Collections.Generic;
using System.Text;
using static Snake.Starter;

namespace Snake.Tests
{
    (TestClass())
    public class StarterTests
    {
        /// <summary>
        /// Testing the movment.
        /// </summary>
        (TestMethod())
        public void test()
        {
           
           var pos = new pixel();
            pos.xpos = 10;
            pos.ypos = 10;

            // this is the operation:
            UpdatePosition(pos, "UP");

            // check the results
            NUnit.Framework.Assert.That(pos.xpos, Is.EqualTo(10));
            NUnit.Framework.Assert.That(pos.ypos, Is.EqualTo(9));
        }

        /// <summary>
        /// Updating pixel class.
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="v"></param>
        private void UpdatePosition(pixel pos, string v)
        {
            throw new NotImplementedException();
        }
    }
}

This is just the gameworld:

using System;
using System.Linq;

public class GameWorld 
{


    /// <summary>
    /// Clear console for snake.
    /// </summary>
    /// <param name="screenwidth"></param>
    /// <param name="screenheight"></param>
    public static void ClearConsole(int screenwidth, int screenheight)
    {
        var blackLine = string.Join("", new byte(screenwidth - 2).Select(b => " ").ToArray());
        Console.ForegroundColor = ConsoleColor.Black;
        for (int i = 1; i < screenheight - 1; i++)
        {
            Console.SetCursorPosition(1, i);
            Console.Write(blackLine);
        }
    }

    /// <summary>
    /// Draw boared.
    /// </summary>
    /// <param name="screenwidth"></param>
    /// <param name="screenheight"></param>
    public static void DrawBorder(int screenwidth, int screenheight)
    {
        var horizontalBar = string.Join("", new byte(screenwidth).Select(b => "■").ToArray());

        Console.SetCursorPosition(0, 0);
        Console.Write(horizontalBar);
        Console.SetCursorPosition(0, screenheight - 1);
        Console.Write(horizontalBar);

        for (int i = 0; i < screenheight; i++)
        {
            Console.SetCursorPosition(0, i);
            Console.Write("■");
            Console.SetCursorPosition(screenwidth - 1, i);
            Console.Write("■");
        }
    }

}

dnd 5e – Good house rules for 1-on-1 game

I’m DMing a game for my son so this is his solo game, and first D&D game. For any dads or anyone else with 1 on 1 gaming experience, what are some good house rules to add? I was thinking of bonuses like doubling his proficiency, giving him an extra HD per level, extra actions, etc to make his character more survivable. Especially at low levels, as a single unlucky critical hit can end his game. But I don’t want to break the game.

Something I’d rather not do is just throw 1 enemy at him at a time. We had 2 encounters so far. The 1st encounter with 2 goblins was successful but he almost died. The 2nd encounter with 3 goblins had him drop to 0 HP, but I wrote it off by saying they captured him instead of killing him outright. For his next session he’ll have to find a way to escape.

So rather than just giving him 1 thing to fight every time, what are some other ways I can make his character more survivable while giving him exciting fights?

If it matters, he’s a 1st level fighter.

Copyright on game art – Game Development Stack Exchange

So I was checking some assets on asset store (2d games pixel-art mostly) and got a question

These two as example:

https://assetstore.unity.com/packages/2d/environments/2d-isometric-tile-starter-pack-27944

https://assetstore.unity.com/packages/2d/environments/2d-isometric-village-pack-86059

they look like many textures you see in any other 2d game, and doesn’t look very unique – I seen similar textures in many games, and I doubt devs/companies bought exactly those texture packs, probably artists just created their own arts that look similar, or maybe sometimes, for simpler textures like sand or water, exactly like existing textures in these or some other packs.

So the question is – how copyright works on art? Can I read about it somewhere, or maybe someone can just post a simple version of explanation? Is there minimal size on what can be copyrighted? Can I copyright a pixel? Or 16×16 pixels with, say 16 colors, not that much combinations, can I copyright all possible “arts”? If my game will accidentally have some character or texture that already exist in the world – how it should be treated – at what stage or percent it’s ok and at what it’s not?

So far I decided to google “Can I copyright a pixel” and looking at links (like this one https://steamcommunity.com/app/342280/discussions/0/371919771747864271/), but who know, maybe I can get something more useful here.

unity – recommendation for a programable FPS game for training forces

I wish to develop a training simulator for army ground forces.

Basic concept is that they can save lots of training time and budget by going through a simulator phase before the physical war-training, explicitly, learning:

  • How to cooperate with one another and improve team work
  • Learn the basics of their weapons capabilities
  • Learn the territory of interest
  • Develop and practice different tactics

All this at a low cost relative to only physical training.

Where should I start? what platforms are available that can be used for effective war-training and can be easily adapted to new maps/fighting territories and warfare equipment?

Thanks!

game – Conectar dois pontos com uma linha reta ou com curvas suaves no GODOT

Imagino que seja algo simples, mas não consigo encontrar em nenhum lugar. Como posso permitir o usuario conectar dois pontos com uma linha no GODOT? Assim como aquela task de conectar fios do Among Us.
inserir a descrição da imagem aqui

entity component system – Modeling RTS game logic using ECS

So I’ve been trying to make a simple RTS engine for some time and I always hit a wall because of increasing, inflexible and complicated hierarchy trees. I’ve ultimately found out that an ECS is a good pattern in order to avoid such pitfalls.
For many scenarios I can envision how all things would be coupled, but there are some edge cases I can not manage to figure out.

Let’s say we have a simple ECS, which lets us add entities, add/remove components from entities and a system which queries for a set of components and updates all entities which have the respective components attached to themselves.

  • Scenario 1 – [Single-Player] Should the player be an entity?
    Having the player as an entity would mean having a system just for one entity that holds a PlayerComponent…

  • Scenario 2 – Tech tree and updates to tech trees?
    1. Tech tree cannot be an entity since you want buildings to have their own tech tree, so it must be a component then.
    2. Tech tree is a component, but then you want the player to be able to research a tech from a tree.
      So the UI must represent this action as an event which you send to a supposed TechSystem.
      In the end this means you iterate through all the entities which have TechTreeComponent, to get the one you need, instead of just updating directly through a reference or an ID.
      This could be solved by having a SelectionSystem which holds a state of selected items, but systems cannot have state…

I probably need a way to send a targeted event towards just one entity or inter-system communication, although the second one can get messy quite fast if I choose to add multithreading or priority to systems. If anyone can provide some pointers and how to optimize the architecture for such cases I would appreciate it.

Is it possible to clone a game, and make modifiactions to it?

I basically want to know whether or not it is possible to copy a mobile game and make it into a new game by modifying it . I want to be able to access , moderation tools and basically alter, things like charcters, events, in ap purchases etc. Alter it to make it different enough from the original and make it available to other users. The game I want to copy is no longer in the play stores and I want to change that.