標題:
2011 0917 貪食蛇(可吃蘋果版)
[打印本頁]
作者:
buy
時間:
2011-9-17 11:33
標題:
2011 0917 貪食蛇(可吃蘋果版)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace SnakeBase
{
// 定義方向
enum Direction
{
Up, Down, Left, Right
}
//蛇節點
class NodeSnake
{
public int x, y; //目前節點座標
public int pre_x, pre_y; // 前一秒的座標
public void SetCurrentXYToPreXY()
{
this.pre_x = this.x;
this.pre_y = this.y;
}
}
//蘋果
class NodeApple
{
public int x, y; //目前節點座標
public NodeApple()
{
x = 30;
y = 30;
}
public void SetXAndY(int x, int y)
{
this.x = x;
this.y = y;
}
}
//貪食蛇
class Snake
{
private int width = 50;
private int height = 40;
private int pixelPerBody = 10;
private NodeApple myApple;
public Direction myDirection = Direction.Down; //目前移動方向
List<NodeSnake> myBody = new List<NodeSnake>(); // 目前蛇身
public Snake()
{
myBody.Add(new NodeSnake {
x = 0,
y =0
});
myBody.Add(new NodeSnake
{
x = 1,
y = 0
});
myBody.Add(new NodeSnake
{
x = 2,
y = 0
});
myApple = new NodeApple();
}
public Snake(int lengthOfBody)
{
for (int i = 0; i < lengthOfBody; i++)
{
myBody.Add(new NodeSnake
{
x = i,
y = 0
});
}
myApple = new NodeApple();
}
//自動往前一格
public void Move()
{
var IsEatApple = false;
var d = this.myDirection;
int index = 0; //紀錄目前判斷到哪個蛇身
foreach (var bdy in this.myBody)
{
if (index == 0) // 頭
{
bdy.SetCurrentXYToPreXY(); //先儲存目前節點的目前位置
if (d == Direction.Down)
{
bdy.y += 1; //往下走
}
else if (d == Direction.Up)
{
bdy.y -= 1; //往上走
}
else if (d == Direction.Left)
{
bdy.x -= 1; //往左走
}
else
{
bdy.x += 1; //往右走
}
// 有無吃到蘋果
if (bdy.x == this.myApple.x && bdy.y == myApple.y)
{
IsEatApple = true;
}
}
else //身體
{
bdy.SetCurrentXYToPreXY(); //先儲存目前節點的目前位置
bdy.x = myBody[index - 1].pre_x;
bdy.y = myBody[index - 1].pre_y;
}
index++;
}
if (IsEatApple) //判斷如果有吃到蘋果
{
var last = this.myBody.Last();
this.myBody.Add(new NodeSnake()
{
x = last.pre_x,
y = last.pre_y
});
NewApplePosition();
}
}
/// <summary>
/// 重新放蘋果
/// </summary>
private void NewApplePosition()
{
Random a = new Random();
bool IsCollection = false;
int x, y;
do
{
x = a.Next(1, this.width );
y = a.Next(1, this.height );
foreach (var bdy in this.myBody)
{
if (bdy.x == x && bdy.y == y)
{
IsCollection = true;
}
}
} while (IsCollection == true);
this.myApple.SetXAndY(x,y);
}
/// <summary>
/// 每次移動一格的方法
/// </summary>
/// <param name="d">往哪個方向移動</param>
public void Move(Direction d)
{
int index = 0; //紀錄目前判斷到哪個蛇身
foreach (var bdy in this.myBody)
{
if (index == 0) // 頭
{
bdy.SetCurrentXYToPreXY(); //先儲存目前節點的目前位置
if (d == Direction.Down)
{
bdy.y += 1; //往下走
}
else if (d == Direction.Up)
{
bdy.y -= 1; //往上走
}
else if (d == Direction.Left)
{
bdy.x -= 1; //往左走
}
else
{
bdy.x += 1; //往右走
}
}
else //身體
{
bdy.SetCurrentXYToPreXY(); //先儲存目前節點的目前位置
bdy.x = myBody[index - 1].pre_x;
bdy.y = myBody[index - 1].pre_y;
}
index++;
}
}
/// <summary>
/// 把整條蛇畫出來,並且回傳200*200圖片
/// </summary>
/// <returns></returns>
public Bitmap GetBitmapFromSnake()
{
Bitmap bmp = new Bitmap(this.width * this.pixelPerBody, this.height * this.pixelPerBody);
Graphics GDI = Graphics.FromImage(bmp);
Pen pen = new Pen(Color.White, 1);
pen.Color = Color.Red; //畫蘋果
GDI.DrawRectangle(pen, (float)this.myApple.x * this.pixelPerBody, (float)this.myApple.y * this.pixelPerBody,
(float)this.pixelPerBody, (float)this.pixelPerBody);
pen.Color = Color.White; //畫蘋果
foreach (var node in this.myBody)
{
GDI.DrawRectangle(pen, (float)node.x * this.pixelPerBody, (float)node.y * this.pixelPerBody, (float)this.pixelPerBody, (float)this.pixelPerBody);
}
return bmp;
}
}
}
複製代碼
作者:
buy
時間:
2011-9-17 11:36
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 SnakeBase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Snake snake1 = new Snake();
private void Form1_Load(object sender, EventArgs e)
{
foreach (var control in this.Controls)
{
if( control.ToString().Contains("Button") )
{
var tmp = (System.Windows.Forms.Button)control;
tmp.PreviewKeyDown += ArrowButton_PreviewKeyDown;
}
}
pictureBox1.Image = snake1.GetBitmapFromSnake();
}
private void btnRight_Click(object sender, EventArgs e)
{
SnakeMoveRight();
}
private void SnakeMoveRight()
{
snake1.myDirection = Direction.Right;
}
private void btnDown_Click(object sender, EventArgs e)
{
snake1.myDirection = Direction.Down;
}
private void btnLeft_Click(object sender, EventArgs e)
{
snake1.myDirection = Direction.Left;
}
private void btnUp_Click(object sender, EventArgs e)
{
snake1.myDirection = Direction.Up;
}
private void btnStart_Click(object sender, EventArgs e)
{
snake1 = new Snake(3);
pictureBox1.Image = snake1.GetBitmapFromSnake();
timer_Move.Enabled = true;
timer_Refresh.Enabled = true;
}
private void timer_Move_Tick(object sender, EventArgs e)
{
snake1.Move();
}
private void timer_Refresh_Tick(object sender, EventArgs e)
{
pictureBox1.Image = snake1.GetBitmapFromSnake();
}
private void btn_SppedUp_Click(object sender, EventArgs e)
{
if (timer_Move.Interval >= 200)
{
timer_Move.Interval = timer_Move.Interval - 100;
}
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
SnakeMoveRight();
}
}
private void btnStart_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
SnakeMoveRight();
}
}
private void ArrowButton_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
SnakeMoveRight();
}
}
}
}
複製代碼
作者:
p17johnny
時間:
2011-9-20 21:24
自嘲: 哈哈 我以後用不了電腦了, 我爸把資源回收桶用不見 結果說是我? 弄壞掉之後就算了~
笑, 以後不用上課了(回家寫不了)
作者:
buy
時間:
2011-9-24 10:27
[attach]515[/attach]
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2