主题:我用C#写的贪食蛇,没有复杂算法与结构,很基础的知识。
2263801
[专家分:0] 发布于 2010-04-30 13:08:00
这个是花了我2个通宵写的,
代码有点多吧 写了900多行,完全是自己想的,除了图形重绘参考别人的例子。。
里面所有的东西,我完全没有看过别的代码自己写的。
是写的不好,不过这真的是很基础很基础。。。
就那个所谓的,面向的对象的吧。。。
最后更新于:2010-04-30 13:20:00
回复列表 (共30个回复)
沙发
2263801 [专家分:0] 发布于 2010-04-30 13:11:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
#region 全局量
SolidBrush snakecolor = new SolidBrush(Color.FromArgb(119, 221, 102));
SolidBrush headcolor = new SolidBrush(Color.Black);
SolidBrush foodBrush = new SolidBrush(Color.Red);
SolidBrush clearcolor = new SolidBrush(Color.Gray);
Timer timer = new Timer();
Snake snake = new Snake();
//蛇的动态数组
ArrayList snakelist = new ArrayList();
ArrayList foodlist = new ArrayList();
//食物动态坐标X
ArrayList foodlocation_X = new ArrayList();
//食物动态坐标Y
ArrayList foodlocation_Y = new ArrayList();
int MoveSize = 30;
int again = 0;
int foodx = 0;
int foody = 0;
//在蛇未吃到食物而重新按下另外键的复位
bool isagain =false;
//是否可重绘食物
bool paintfood = false;
//是否停止
bool stop = false;
//是否死亡
bool isdie = false;
//是否继续游戏
bool IsStart = false;
#endregion
#region Form1
public Form1()
{
InitializeComponent();
}
#endregion
板凳
2263801 [专家分:0] 发布于 2010-04-30 13:12:00
#region Form1_KeyDown
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Enter && timer.Enabled == false && IsStart == false)
{
NewGame();
}
else if (e.KeyCode == Keys.Space)
{
IsPause();
}
if (isagain == false)
{
isagain = true;
if (isdie == false)
{
if (again == 3 && e.KeyCode != Keys.Left ||
again == 1 && e.KeyCode != Keys.Right ||
again == 4 && e.KeyCode != Keys.Up ||
again == 2 && e.KeyCode != Keys.Down
|| timer.Enabled == false)
{
if (e.KeyCode == Keys.Right)
{
if (timer.Enabled == false)
{
again = 3;
MoveRight();
}
else if (timer.Enabled == true && again != 3)
{
again = 3;
MoveRight();
}
}
3 楼
2263801 [专家分:0] 发布于 2010-04-30 13:12:00
else if (e.KeyCode == Keys.Down)
{
if (timer.Enabled == false)
{
again = 4;
MoveDown();
}
else if (timer.Enabled == true && again != 4)
{
again = 4;
MoveDown();
}
}
else if (e.KeyCode == Keys.Left)
{
if (timer.Enabled == false)
{
again = 1;
MoveLeft();
}
else if (timer.Enabled == true && again != 1)
{
again = 1;
MoveLeft();
}
}
else if (e.KeyCode == Keys.Up)
{
if (timer.Enabled == false)
{
again = 2;
MoveUp();
}
else if (timer.Enabled == true && again != 2)
{
again = 2;
MoveUp();
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
#endregion
4 楼
2263801 [专家分:0] 发布于 2010-04-30 13:12:00
#region 每个方向键方法
public void MoveUp()
{
timer.Tick -= new EventHandler(timer_Tick_Right);
timer.Tick -= new EventHandler(timer_Tick_Down);
timer.Tick -= new EventHandler(timer_Tick_Left);
timer.Tick += new EventHandler(timer_Tick_Up);
timer.Start();
}
public void MoveLeft()
{
timer.Tick -= new EventHandler(timer_Tick_Up);
timer.Tick -= new EventHandler(timer_Tick_Right);
timer.Tick -= new EventHandler(timer_Tick_Down);
timer.Tick += new EventHandler(timer_Tick_Left);
timer.Start();
}
public void MoveRight()
{
timer.Tick -= new EventHandler(timer_Tick_Up);
timer.Tick -= new EventHandler(timer_Tick_Down);
timer.Tick -= new EventHandler(timer_Tick_Left);
timer.Tick += new EventHandler(timer_Tick_Right);
timer.Start();
}
public void MoveDown()
{
timer.Tick -= new EventHandler(timer_Tick_Up);
timer.Tick -= new EventHandler(timer_Tick_Right);
timer.Tick -= new EventHandler(timer_Tick_Left);
timer.Tick += new EventHandler(timer_Tick_Down);
timer.Start();
}
#endregion
5 楼
2263801 [专家分:0] 发布于 2010-04-30 13:13:00
#region 每个时间事件
private void timer_Tick_Up(object sender, EventArgs e)
{
MoveSnake(2);
}
private void timer_Tick_Left(object sender, EventArgs e)
{
MoveSnake(1);
}
private void timer_Tick_Down(object sender, EventArgs e)
{
MoveSnake(4);
}
private void timer_Tick_Right(object sender, EventArgs e)
{
MoveSnake(3);
}
#endregion
6 楼
2263801 [专家分:0] 发布于 2010-04-30 13:13:00
#region 蛇实体
public class Snake
{
private int x = 0;
private int y = 0;
private int width = 28;
private int height = 28;
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
public int Y
{
get
{
return y;
}
set
{
y = value;
}
}
public int Height
{
get
{
return height;
}
set
{
height = value;
}
}
public int Width
{
get
{
return width;
}
set
{
width = value;
}
}
public Snake CreatePart(int x, int y, int width, int height)
{
Snake s = new Snake();
s.X = x;
s.Y = y;
s.Height = height;
s.Width = width;
return s;
}
}
#endregion
7 楼
2263801 [专家分:0] 发布于 2010-04-30 13:13:00
#region 移动蛇
public void MoveSnake(int x)
{
int temp_x = 0;
int temp_y = 0;
bool isate = false;
ArrayList templist = new ArrayList();
Snake addsnake = new Snake();
switch (x)
{
case 1:
for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--) //移动递增
{
snake = (Snake)snakelist[j]; //填充由最后一个开始
Graphics graphicsdraw = this.CreateGraphics();
if (Int32.Parse(i.ToString()) == 0)
{
graphicsdraw.FillRectangle(headcolor, snake.X - MoveSize, snake.Y, snake.Width, snake.Height);
if (Security(snake.X - MoveSize, snake.Y))
{
break;
}
temp_x = snake.X;
temp_y = snake.Y;
templist.Add(snake.CreatePart(snake.X - MoveSize, snake.Y, snake.Width, snake.Height));
if (snake.X - MoveSize == foodx && snake.Y == foody) //是否吃到食物
{
Setlevel();
isate = true;
Snake fitstsnake = ((Snake)snakelist[0]);
addsnake.Width = fitstsnake.Width;
addsnake.Height = fitstsnake.Height;
addsnake.X = fitstsnake.X;
addsnake.Y = fitstsnake.Y;
}
}
else
{
graphicsdraw.FillRectangle(snakecolor, temp_x, temp_y, snake.Width, snake.Height);
templist.Add(snake.CreatePart(temp_x, temp_y, snake.Width, snake.Height));
temp_x = snake.X;
temp_y = snake.Y;
}
Graphics graphicsclear = this.CreateGraphics();
Snake snake2 = (Snake)snakelist[j];
graphicsclear.FillRectangle(clearcolor, snake2.X, snake2.Y, snake2.Width, snake2.Height);
}
8 楼
2263801 [专家分:0] 发布于 2010-04-30 13:13:00
if (stop == false)
{
if (isate)
{
AddScore();
for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--) //重新定位坐标-
{
Snake tsnake = (Snake)templist[i];
snake = (Snake)snakelist[j];
snake.X = tsnake.X;
snake.Y = tsnake.Y;
}
snakelist.Insert(0, addsnake);
templist.Insert(0, addsnake);
Newfood();
}
else
{
for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--) //重新定位坐标-
{
Snake tsnake = (Snake)templist[i];
snake = (Snake)snakelist[j];
snake.X = tsnake.X;
snake.Y = tsnake.Y;
}
}
}
ISstop();
break;
9 楼
2263801 [专家分:0] 发布于 2010-04-30 13:14:00
case 2:
for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--) //移动递增
{
snake = (Snake)snakelist[j]; //填充由最后一个开始
Graphics graphicsdraw = this.CreateGraphics();
if (Int32.Parse(i.ToString()) == 0 )
{
if (Security(snake.X,snake.Y- MoveSize))
{
break;
}
graphicsdraw.FillRectangle(headcolor, snake.X, snake.Y - MoveSize, snake.Width, snake.Height);
temp_x = snake.X;
temp_y = snake.Y;
templist.Add(snake.CreatePart(snake.X, snake.Y - MoveSize, snake.Width, snake.Height));
if (snake.X == foodx && snake.Y-MoveSize == foody) //是否吃到食物
{
Setlevel();
isate = true;
Snake fitstsnake = ((Snake)snakelist[0]);
addsnake.Width = fitstsnake.Width;
addsnake.Height = fitstsnake.Height;
addsnake.X = fitstsnake.X;
addsnake.Y = fitstsnake.Y;
}
}
else
{
graphicsdraw.FillRectangle(snakecolor, temp_x, temp_y, snake.Width, snake.Height);
templist.Add(snake.CreatePart(temp_x, temp_y, snake.Width, snake.Height));
temp_x = snake.X;
temp_y = snake.Y;
}
Graphics graphicsclear = this.CreateGraphics();
Snake snake2 = (Snake)snakelist[j];
graphicsclear.FillRectangle(clearcolor, snake2.X, snake2.Y, snake2.Width, snake2.Height);
}
10 楼
2263801 [专家分:0] 发布于 2010-04-30 13:14:00
if (stop==false) // 如果没撞到墙
{
if (isate) // 如果吃到食物
{
AddScore();
for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--) //重新定位坐标-
{
Snake tsnake = (Snake)templist[i];
snake = (Snake)snakelist[j];
snake.X = tsnake.X;
snake.Y = tsnake.Y;
}
snakelist.Insert(0, addsnake);
templist.Insert(0, addsnake);
Newfood();
}
else
{
for (int i = 0, j = snakelist.Count - 1; i < snakelist.Count; i++, j--) //重新定位坐标-
{
Snake tsnake = (Snake)templist[i];
snake = (Snake)snakelist[j];
snake.X = tsnake.X;
snake.Y = tsnake.Y;
}
}
}
ISstop();
break;
我来回复