Bienvenido a Tecnohackers

Tecnohackers » Programacion » Area de Programacion » Programacion a Bajo Nivel. APIs, Hooking, ASM, C/C++, etc.
 » 

Shmup 1.0: Shooter Console Game [C#]



Autor Tema: Shmup 1.0: Shooter Console Game [C#]  (Leído 1581 veces)

Desconectado zolo

  • Consigliere
  • Master
  • *****
  • Mensajes: 22377
  • Un Mes, Un Año o Toda Una Vida, Da Igual, Estare
Shmup 1.0: Shooter Console Game [C#]
« en: Julio 28, 2016, 07:02:29 am »
Código: You are not allowed to view links. Register or Login
########################################################################################
########################################################################################
#+-
#+- Title: Shmup
#+- Author: TortegaFR
#+- Category: .Net Game
#+- Description:  Its very simple video console game about Shooter.
#+- Date Release: 2016-07-24
#+- 
########################################################################################
########################################################################################
 

Codigo Fuente

program.cs

Código: You are not allowed to view links. Register or Login
//#define DEBUG

using System;

namespace game
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Game game = new Game();
            ConsoleKeyInfo keyInfo;
            while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
            {
                switch (keyInfo.Key)
                {
                    case ConsoleKey.W: // tirer
                        game.player.tirer(game.player.posX, game.player.posY + 2);
                        break;

                    case ConsoleKey.UpArrow:
                        if (game.player.posX != 1)
                        {
                            if (game.map.getElement(game.player.posX - 1, game.player.posY) != ' ')
                            {
                                if (game.map.getElement(game.player.posX - 1, game.player.posY) == 'B')
                                {
                                    game.bonusEvent();
                                }
                                else
                                {
                                    game.player.meurt();
                                }
                            }
                            else
                            {
                                game.player.deplacer(game.player.posX - 1, game.player.posY);
                            }
                        }
                        break;

                    case ConsoleKey.DownArrow:
                        if (game.player.posX != 23)
                        {
                            if (game.map.getElement(game.player.posX + 1, game.player.posY) != ' ')
                            {
                                if (game.map.getElement(game.player.posX + 1, game.player.posY) == 'B')
                                {
                                    game.bonusEvent();
                                }
                                else
                                {
                                    game.player.meurt();
                                }
                            }
                            else
                            {
                                game.player.deplacer(game.player.posX + 1, game.player.posY);
                            }
                        }
                        break;

                    case ConsoleKey.RightArrow:

                        if (game.player.posY != 79)
                        {
                            if (game.map.getElement(game.player.posX, game.player.posY + 1) != ' ')
                            {
                                if (game.map.getElement(game.player.posX, game.player.posY + 1) == 'B')
                                {
                                    game.bonusEvent();
                                }
                                else
                                {
                                    game.player.meurt();
                                }
                            }
                            else
                            {
                                game.player.deplacer(game.player.posX, game.player.posY + 1);
                            }
                        }
                        break;

                    case ConsoleKey.LeftArrow:
                        if (game.player.posY != 1)
                        {
                            if (game.map.getElement(game.player.posX, game.player.posY - 1) != ' ')
                            {
                                if (game.map.getElement(game.player.posX, game.player.posY - 1) == 'B')
                                {
                                    game.bonusEvent();
                                }
                                else
                                {
                                    game.player.meurt();
                                }
                            }
                            else
                            {
                                game.player.deplacer(game.player.posX, game.player.posY - 1);
                            }
                        }
                        break;
                }
            }
        }
    }
}
 

Game.cs

Código: You are not allowed to view links. Register or Login
using System;
using System.Collections.Generic;
using System.Threading;

namespace game
{
    internal class Game
    {
        public Map map;
        public Joueur player;
        private Dictionary<char, ConsoleColor> mapColor;

        public Game()
        {
            this.map = new Map();
            this.player = new Joueur(this.map);

            this.mapColor = new Dictionary<char, ConsoleColor>();
            mapColor['>'] = ConsoleColor.Cyan;
            mapColor['■'] = ConsoleColor.Red;
            mapColor['B'] = ConsoleColor.Yellow;
            mapColor['#'] = ConsoleColor.Gray;
            mapColor['_'] = ConsoleColor.DarkBlue;
            mapColor['\\'] = ConsoleColor.DarkBlue;
            mapColor['/'] = ConsoleColor.DarkBlue;
            mapColor['-'] = ConsoleColor.DarkBlue;
            mapColor['+'] = ConsoleColor.DarkBlue;
            mapColor['|'] = ConsoleColor.DarkBlue;

            Thread threadRefresh = new Thread(() =>
            {
                while (player.vivant)
                {
                    Thread.Sleep(5);
                    RefreshMap();
                }
            });

            Thread threadLaser = new Thread(() =>
            {
                while (player.vivant)
                {
                    Thread.Sleep(10);
                    for (int i = 0; i != 24; i++)
                    {
                        for (int j = 0; j != 79; j++)
                        {
                            if (map.getElement(i, j) == '■') //laser
                            {
                                map.ajoutLaser(i, j + 1);
                                map.tejElement(i, j);
                                if (j < 78)
                                {
                                    if (checkLaserCollision(i, j + 2))
                                    {
                                        map.tejElement(i, j + 1);
                                        map.tejElement(i, j + 2);
                                    }
                                    j++;
                                }
                                else
                                {
                                    map.tejElement(i, j + 1);
                                }
                            }
                        }
                    }
                }
            });

            Thread threadBlocs = new Thread(() =>
            {
                while (player.vivant)
                {
                    Thread.Sleep(300);
                    for (int i = 0; i != 24; i++)
                    {
                        for (int j = 0; j != 79; j++)
                        {
                            if (map.getElement(i, j) == '#' || isStrongBloc(i, j))
                            {
                                if (checkBlocCollision(i, j - 1) == true)
                                {
                                    player.meurt();
                                }
                                else
                                {
                                    if (map.getElement(i, j) == '#')
                                    {
                                        map.ajoutBlocGris(i, j - 1);
                                    }
                                    else
                                    {
                                        map.ajoutBlocVert(i, j - 1, map.getElement(i, j));
                                    }

                                    map.tejElement(i, j);

                                    if (j < 2)
                                    {
                                        map.tejElement(i, j - 1);
                                    }
                                }
                            }
                            else if (map.getElement(i, j) == 'B')
                            {
                                if (checkBlocCollision(i, j - 1) == true)
                                {
                                    bonusEvent();
                                    map.tejElement(i, j - 1);
                                }
                                else
                                {
                                    map.ajoutBlocBonus(i, j - 1);
                                    map.tejElement(i, j);
                                }

                                if (j < 2)
                                {
                                    map.tejElement(i, j - 1);
                                }
                            }
                        }
                    }
                }
            });

            threadRefresh.Start();
            threadBlocs.Start();
            threadLaser.Start();
        }

        public bool checkBlocCollision(int blocX, int blocY)
        {
            if (map.getElement(blocX, blocY) == '>' && map.getElement(blocX, blocY) != 'B')
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool checkLaserCollision(int laserX, int laserY)
        {
            if (map.getElement(laserX, laserY) == '#')
            {
                return true;
            }
            else if (isStrongBloc(laserX, laserY))
            {
                map.ajoutBlocVert(laserX, laserY - 1, map.getElement(laserX, laserY));
                return false;
            }
            else
            {
                return false;
            }
        }

        public void bonusEvent()
        {
            for (int i = 0; i != 24; i++)
            {
                for (int j = 1; j != 80; j++)
                {
                    if (isStrongBloc(i, j))
                    {
                        map.ajoutBlocGris(i, j);
                    }
                }
            }
        }

        public bool isStrongBloc(int x, int y)
        {
            char element = map.getElement(x, y);
            if (element == '_' || element == '\\' || element == '/' || element == '-' || element == '+' || element == '|')
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void RefreshMap()
        {
            int cursorPos = 0;
            char[,] plateau = new char[25, 80];

            for (int i = 0; i != 24; i++)
            {
                for (int j = 1; j != 80; j++)
                {
                    plateau = map.getMap();
                    if (mapColor.ContainsKey(plateau[i, j]))
                    {
                        Console.ForegroundColor = mapColor[plateau[i, j]];
                    }
                    Console.Write(plateau[i, j]);
                }
                cursorPos++;
                Console.SetCursorPosition(0, cursorPos);
            }
        }
    }
}
 

You are not allowed to view links. Register or Login


You are not allowed to view links. Register or Login

Fuente: Zephomet / el-hacker.com


You are not allowed to view links. Register or Login

Tags:
Tags:

 


SMF 2.0.19 | SMF © 2016, Simple Machines
Paginas Afiliadas
Twitter - FaceBook - Daraxblog
Designed by Smf Personal