为了学习.net,学着写了这个小游戏。顺便实践一下OO了。请高手指教。
//
form1.cs
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Timers;

namespace
snake
{
public partial class GameBox : Form
{
private System.Timers.Timer timer;
private GameBoxStatus status;
private Snake snake;

public GameBox()
{
InitializeComponent();
status = GameBoxStatus.OFF;
}

public void Turn(Direction direction)
{
if (status == GameBoxStatus.ON)
{
snake.Turn(direction);
}
}

public void Power()
{
if (status == GameBoxStatus.ON)
{
PowerOff();
}
else
{
PowerOn();
}
}

private void PowerOn()
{
// 初始化timer
timer = new System.Timers.Timer();
timer.AutoReset = true;
timer.Enabled = false;
timer.Interval = 100;
// 初始化snake
snake = new Snake(drawingPanel, timer);


timer.Elapsed += new ElapsedEventHandler(snake.Move);

status = GameBoxStatus.ON;
}

private void PowerOff()
{
//snake.EraseAllBlocks();
// 通知绘图区清空
Graphics graphics = drawingPanel.CreateGraphics();
graphics.Clear(Color.White);

// 销毁当前的snake对象
snake.DestroySnake();
snake = null;

timer.Stop();
timer = null;

status = GameBoxStatus.OFF;
}


//
private void powerButton_Click(object sender, EventArgs e)
{
Power();
}

private void upButton_Click(object sender, EventArgs e)
{
Turn(Direction.UP);
}

private void leftButton_Click(object sender, EventArgs e)
{
Turn(Direction.LEFT);
}

private void downButton_Click(object sender, EventArgs e)
{
Turn(Direction.DOWN);
}

private void rightButton_Click(object sender, EventArgs e)
{
Turn(Direction.RIGHT);
}

private void pauseButton_Click(object sender, EventArgs e)
{
if (status == GameBoxStatus.ON)
{
snake.Pause();
}
}

private void continueButton_Click(object sender, EventArgs e)
{
if (status == GameBoxStatus.ON)
{
snake.Resume();
}
}
}

enum GameBoxStatus
{
ON, OFF
}

public enum Direction
{
UP, DOWN, LEFT, RIGHT
}
}
//
snake.cs
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Windows.Forms;
using
System.Drawing;

namespace
snake
{
class Snake
{
private Graphics graphics;
private System.Timers.Timer timer;

private SnakeStatus status;
private Direction direction;

//private SnakeBody snakeBody;

private bool isDirectionChangeAllowed;


与块有关*************************************************************************** ******************************************************************************************

/// <summary>
/// 初始化snake。设置好引用——与snake通信的对象的引用
/// </summary>
/// <param name="drawingPanel"></param>
/// <param name="timer"></param>
public Snake(Panel drawingPanel, System.Timers.Timer timer)
{
drawingPanel.Paint += new PaintEventHandler(drawingPanel_Paint);
this.graphics = drawingPanel.CreateGraphics();
AppendBlock(new Block(3, 1));
AppendBlock(new Block(2, 1));
AppendBlock(new Block(1, 1));// 这样初始化块,暗示了初时方向为向右

drawingPanel.Invalidate(); // 已经有块了,绘制他们

this.timer = timer;
}


/// <summary>
/// 响应paint消息。先绘制背景,再绘制所有块
/// </summary>
void drawingPanel_Paint(object sender, PaintEventArgs e)
{
DrawAllBlocks();
}



public void Die()
{
if (status == SnakeStatus.RUNNING)
{
timer.Stop();
graphics.Clear(Color.White);
graphics.DrawString("Game Over", new Font("Arial", 16), new SolidBrush(Color.Black), new Point(0, 0));
status = SnakeStatus.DEAD;
}
}

public void Move(object source, System.Timers.ElapsedEventArgs e)
{
if (status == SnakeStatus.RUNNING)
{
isDirectionChangeAllowed = true;

// move snake body according to direction

// new block coordinates
int NewHeadX=0;
int NewHeadY=0;
switch (direction)
{
case Direction.UP:
NewHeadX = headBlock.X;
NewHeadY = headBlock.Y - 1;
break;
case Direction.DOWN:
NewHeadX = headBlock.X;
NewHeadY = headBlock.Y + 1;
break;
case Direction.LEFT:
NewHeadX = headBlock.X - 1;
NewHeadY = headBlock.Y;
break;
case Direction.RIGHT:
NewHeadX = headBlock.X + 1;
NewHeadY = headBlock.Y;
break;
}

if(NewHeadX<1||NewHeadX>50||NewHeadY<1||NewHeadY>20)
{
Die();
return;
}

// 擦除和绘制
tailBlock.Erase(graphics);
tailBlock.SetPos(NewHeadX, NewHeadY);
Rotate();
headBlock.Draw(graphics);
}
}

public void Pause()
{
if (status == SnakeStatus.RUNNING)
{
this.status = SnakeStatus.PAUSED;
timer.Stop();
}
}

public void Resume()
{
if (status == SnakeStatus.PAUSED)
{
status = SnakeStatus.RUNNING;
timer.Start();
}
}

public void Turn(Direction newDirection)
{
if (status == SnakeStatus.RUNNING && isDirectionChangeAllowed)
{
if (newDirection == Direction.UP && this.direction == Direction.DOWN)
{
return;
}
if (newDirection == Direction.DOWN && this.direction == Direction.UP)
{
return;
}
if (newDirection == Direction.LEFT && this.direction == Direction.RIGHT)
{
return;
}
if (newDirection == Direction.RIGHT && this.direction == Direction.LEFT)
{
return;
}

this.direction = newDirection;
isDirectionChangeAllowed = false;
}
if (status == SnakeStatus.INITIALIZED)
{
this.timer.Start();
this.status = SnakeStatus.RUNNING;
//this.isDirectionChangeAllowed = true;
if (newDirection == Direction.LEFT)
{
this.direction = Direction.RIGHT;
}
else
{
this.direction = newDirection;
}

}
if (status == SnakeStatus.DEAD)
{
// do nothing
}
if (status == SnakeStatus.PAUSED)
{
// do nothing
}
}


}

enum SnakeStatus
{
INITIALIZED, RUNNING, DEAD,PAUSED
}

}
//
block.cs
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Drawing;

namespace
snake
{
class Block
{
public Block Next;
public Block Previous;

private int x;
private int y;

public Block(int x, int y)
{
this.x = x;
this.y = y;

Next = null;
Previous = null;
}

public int X
{
get
{
return x;
}
}

public int Y
{
get
{
return y;
}
}

public void SetPos(int x, int y)
{
this.x = x;
this.y = y;
}

public void Draw(Graphics graphics)
{
graphics.FillRectangle(Brushes.Black, (x - 1) * 8, (y - 1) * 8, 8, 8);
graphics.DrawRectangle(new Pen(Brushes.White), (x - 1) * 8, (y - 1) * 8, 8, 8);
}

public void Erase(Graphics grahics)
{
grahics.FillRectangle(Brushes.White, (x - 1) * 8, (y - 1) * 8, 8, 8);
}
}
}
程序界面




























































































































































































































































































































































































