Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MAINBORD
- using System;
- using System.Windows.Forms;
- using System.Drawing;
- namespace Assignment2
- {
- public class Bord : Form
- {
- public Bord() : base()
- {
- Text = "Snake";
- Width = 800;
- Height = 600;
- BackColor = Color.White;
- }
- }
- }
- //CIRCLE
- using System;
- namespace Assignment2
- {
- public class Circle
- {
- public int X { get; set; }
- public int Y { get; set; }
- public Circle()
- {
- Y = 0;
- X = 0;
- }
- }
- }
- //MAINCLASS
- using System;
- using System.Windows.Forms;
- namespace Assignment2
- {
- static class MainClass
- {
- public static void Main(string[] args)
- {
- Engine2 engine = new Engine2();
- engine.Run();
- }
- }
- }
- //ENGINE
- using System;
- using System.Drawing;
- using System.Collections.Generic;
- using System.Windows.Forms;
- namespace Assignment2
- {
- public class Engine2
- {
- public static Bord MainBord { get; set; }
- private Timer Timer;
- public player player1;
- //private ISet<player> Players = new HashSet<player>()
- //{
- // new player(20,20)
- //};
- public Engine2()
- {
- new Settings();
- MainBord = new Bord();
- player1 = new player();
- Timer = new Timer();
- }
- public void Run()
- {
- MainBord.Paint += updateScreen;
- Timer.Interval = 1000 / 25;
- Timer.Tick += Updater;
- Timer.Start();
- Circle head = new Circle { X = 5, Y = 10 };
- player1.Snake.Add(head);
- Application.Run(MainBord);
- //StartGame();
- }
- private void Updater(object sender, EventArgs e)
- {
- Directions directions = choseDirection();
- if (Settings.GameOver == true)
- {
- if(directions == Directions.esc)
- {
- StartGame();
- }
- }
- else
- {
- if (directions == Directions.up && Settings.direction != Directions.down)
- {
- Settings.direction = Directions.up;
- }
- else if (directions == Directions.down && Settings.direction != Directions.up)
- {
- Settings.direction = Directions.down;
- }
- else if (directions == Directions.left && Settings.direction != Directions.right)
- {
- Settings.direction = Directions.left;
- }
- else if(directions == Directions.right && Settings.direction != Directions.left)
- {
- Settings.direction = Directions.right;
- }
- player1.MovePlayer();
- }
- MainBord.Refresh();
- }
- public void updateScreen(object obj, PaintEventArgs args)
- {
- player1.updateScreen(args.Graphics);
- }
- private void StartGame()
- {
- player1.Snake.Clear();
- Circle head = new Circle() { X = 5, Y = 10 };
- player1.Snake.Add(head);
- Application.Run(MainBord);
- }
- public Directions choseDirection()
- {
- var key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.UpArrow: //Direction.Up
- return Directions.up;
- case ConsoleKey.DownArrow://Direction.Down:
- return Directions.down;
- case ConsoleKey.LeftArrow:
- return Directions.left;
- case ConsoleKey.RightArrow:
- return Directions.right;
- default:
- return Directions.esc;
- }
- }
- }
- }
- //PLAYER
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- namespace Assignment2
- {
- public class player
- {
- //public enum Direction { up, down, left, right };
- public List<Circle> Snake;
- public Brush SnakeColor;
- public int Width;
- public int Height;
- //public Direction direction;
- public player()
- {
- Width = Settings.Width;
- Height = Settings.Height;
- //direction = Settings.direction;
- SnakeColor = Brushes.Green;
- Snake = new List<Circle>();
- }
- public void MovePlayer( )
- {
- var key = Console.ReadKey(true);
- //loop for snake head and parts
- for (int i = Snake.Count -1 ; i >= 0; i--)
- {
- // if snake head is active
- if (i == 0)
- {
- //move body along with head
- switch (Settings.direction/*Settings.direction*/)
- {
- case Directions.up :
- Snake[i].Y--;
- break;
- case Directions.down:
- Snake[i].Y++;
- break;
- case Directions.left:
- Snake[i].X--;
- break;
- case Directions.right:
- Snake[i].X++;
- break;
- }
- /* int maxXpos = Engine2.MainBord.Width;
- int maxYpos = Engine2.MainBord.Height;
- // if snake outruns the gameboard
- if (Snake[i].X < 0 || Snake[i].Y < 0 || Snake[i].X > maxXpos || Snake[i].Y > maxYpos)
- {
- die();
- }
- // if snake runs into itself
- for (int j = 1; j < Snake.Count; j++)
- {
- if (Snake[i].X == Snake[j].X && Snake[i].Y == Snake[j].Y)
- {
- die();
- }
- }
- // later will check collision with food
- if (Snake[i].X == 10000 && Snake[i].Y == 10000)
- {
- die();
- //eat();
- }*/
- }
- //if no collision continue
- else
- {
- Snake[i].X = Snake[i - 1].X;
- Snake[i].Y = Snake[i - 1].Y;
- }
- }
- }
- public void updateScreen(Graphics e)
- {
- if (Settings.GameOver == false)
- {
- for (int i = 0; i < Snake.Count; i++)
- {
- if (i == 0)
- {
- SnakeColor = Brushes.Black;
- }
- else
- {
- SnakeColor = Brushes.Blue;
- }
- e.FillEllipse(SnakeColor, Snake[i].X,Snake[i].Y,Width,Height);
- }
- }
- }
- public void die()
- {
- Settings.GameOver = true;
- }
- }
- }
- //SETTINGS
- using System;
- namespace Assignment2
- {
- public enum Directions { up, down, left, right, esc };
- public class Settings
- {
- public static bool GameOver { get; set; }
- public static Directions direction { get; set; }
- public static int Width { get; set; }
- public static int Height { get; set; }
- public static int Score { get; set; }
- public static int Speed { get; set; }
- public Settings()
- {
- GameOver = false;
- direction = Directions.down;
- Width = 20;
- Height = 20;
- Score = 0;
- Speed = 20;
- }
- }
- }
- //INPUT
- using System;
- using System.Collections;
- using System.Windows.Forms;
- using System.Collections.Generic;
- namespace Assignment2
- {
- public class Input
- {
- public static Hashtable kTable = new Hashtable();
- public static bool KeyPressed(Keys key)
- {
- if (kTable[key] == null)
- {
- return false;
- }
- return (bool)kTable[key];
- }
- public static void State(Keys key, bool state)
- {
- kTable[key] = state;
- }
- public static Directions ChoseDirection()
- {
- var key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.UpArrow: //Direction.Up
- return Directions.up;
- case ConsoleKey.DownArrow://Direction.Down:
- return Directions.down;
- case ConsoleKey.LeftArrow:
- return Directions.left;
- case ConsoleKey.RightArrow:
- return Directions.right;
- default:
- return Directions.esc;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement