Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Snake_Game
{
public partial class Form1 : Form
{
Graphics paper;
Snake snake = new Snake();
Random randFood = new Random();
int counter = 0;
food[] food;
bool Left = false;
bool Right = true;
bool Up = false;
bool Down = false;
int[,] level = {
{1,1,1,1,1},
{1,0,0,0,0},
{1,0,0,0,0},
{1,0,0,0,0},
{1,1,1,1,1},
};
public Form1()
{
InitializeComponent();
for (int y = 0; y < 5; y++)
{
for (int x = 0; x < 5; x++)
{
food = new food[counter];
counter++;
}
}
counter = 0;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
paper = e.Graphics;
snake.drawSnake(paper);
for (int y = 0; y < 5; y++)
{
for (int x = 0; x < 5; x++)
{
if (level[y, x] == 0)
{
food[counter].drawFood(paper, x, y);
counter++;
}
}
}
counter=0;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Down && Up == false)
{
Down = true;
Up = false;
Left = false;
Right = false;
}
if (e.KeyData == Keys.Up && Down == false)
{
Down = false;
Up = true;
Left = false;
Right = false;
}
if (e.KeyData == Keys.Left && Right == false)
{
Down = false;
Up = false;
Left = true;
Right = false;
}
if (e.KeyData == Keys.Right && Left == false)
{
Down = false;
Up = false;
Left = false;
Right = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (Down)
{
snake.moveDown();
}
if (Up)
{
snake.moveUp();
}
if (Right)
{
snake.moveRight();
}
if (Left)
{
snake.moveLeft();
}
this.Invalidate();
}
}
}
Food
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Snake_Game
{
public class food
{
private int x, y, width, height;
private SolidBrush brush;
private Rectangle foodRec;
public food()
{
brush = new SolidBrush(Color.Black);
width = 10;
height = 10;
foodRec = new Rectangle(x, y, width, height);
}
public void drawFood(Graphics paper, int x, int y)
{
foodRec.X = x*10;
foodRec.Y = y*10;
paper.FillRectangle(brush, foodRec);
}
}
}
Snake
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Snake_Game
{
public class Snake
{
public Rectangle snakeRec;
private SolidBrush brush;
private int x, y, width, height;
public Snake()
{
snakeRec = new Rectangle();
brush = new SolidBrush(Color.Blue);
x = 30;
y = 30;
width = 10;
height = 10;
for (int i = 0; i < 1; i++)
{
snakeRec = new Rectangle(x, y, width, height);
x -= 10;
}
}//constructor
public void drawSnake(Graphics paper)
{
paper.FillRectangle(brush, snakeRec);
}
public void drawSnake()
{
snakeRec = snakeRec;
}
public void moveDown()
{
drawSnake();
snakeRec.Y += 10;
}
public void moveUp()
{
drawSnake();
snakeRec.Y -= 10;
}
public void moveLeft()
{
drawSnake();
snakeRec.X -= 10;
}
public void moveRight()
{
drawSnake();
snakeRec.X += 10;
}
}
}

New Topic/Question
Reply




MultiQuote





|