大家好,又见面了,我是你们的朋友全栈君。
这是本人第一篇博客,感谢收看,之后对游戏做出的修改,将以方法代码块放在最后
Form方法
100毫秒刷新
private void timer1_Tick(object sender, EventArgs e)
{
DrawSnake();
DrawFood();
}
秒刷新(蛇的移动速度由此决定)
private void timer2_Tick(object sender, EventArgs e)
{
Snake.SnakeMove(Snake.wasd);
EatFood();
TobeOrNotTobe();
label2.Text = score.ToString();
}
画蛇
public void DrawSnake()
{
RectangleF[] rectangles = new RectangleF[Snake.realsnake.Count];
int num;
for (num = 0; num < Snake.realsnake.Count; num++)
{
rectangles[num] = new RectangleF(Snake.realsnake[num].Location_x,
Snake.realsnake[num].Location_y, Snake.width, Snake.height);
}
this.Refresh();
Graphics g = this.CreateGraphics();
SolidBrush myBrush = new SolidBrush(Color.Black);
g.FillRectangles(myBrush, rectangles);
}
创建食物
public void newfood()
{
Food food = new Food();
foodx = food.Location_x;
foody = food.Location_y;
}
画食物
public void DrawFood()
{
Graphics f = this.CreateGraphics();
SolidBrush mybrush = new SolidBrush(Color.Red);
f.FillRectangle(mybrush, foodx, foody, 10, 10);
}
吃掉食物
public void EatFood()
{
if (Snake.realsnake[Snake.realsnake.Count - 1].Location_x == foodx
&& Snake.realsnake[Snake.realsnake.Count - 1].Location_y == foody)
{
newfood();
Snake.EatLong();
score += 1;
}
}
生存还是毁灭
public void TobeOrNotTobe()
{
int x = Snake.realsnake[Snake.realsnake.Count - 1].Location_x;
int y = Snake.realsnake[Snake.realsnake.Count - 1].Location_y;
if (!(x <= 500 && x >= 0 && y <= 500 && y >= 0))
{
GameOver();
timer2.Enabled = false;
}
}
游戏结束
public void GameOver()
{
label3.Visible = true;
label4.Visible = true;
label5.Text = score.ToString();
label5.Visible = true;
button1.Text = "重新开始";
button1.Visible = true;
}
button点击事件链
private void button1_Click(object sender, EventArgs e)
{
Snake.Snakestart();
newfood();
score = 0;
timer1.Enabled = true;
timer2.Enabled = true;
button1.Visible = false;
label3.Visible = false;
label4.Visible = false;
label5.Visible = false;
}
其他静态变量
static int score;
static int foodx, foody;
游戏主体类
点击开始游戏,开始游玩。
蛇
using System.Collections.Generic;
namespace 贪吃蛇form
{
public class Snake
{
public int Location_x;//0为x,1为y
public int Location_y;
public static int width = 10, height = 10;
public static List<Snake> realsnake = new List<Snake>();
//当前方向
public static int wasd;
public Snake()
{
Location_x = 250;
Location_y = 250;
}
public static void Snakestart()
{
wasd = 4;
realsnake.Clear();
Snake s0 = new Snake();
Snake s1 = new Snake();
Snake s2 = new Snake();
Snake s3 = new Snake();
realsnake.Add(s0);
s1.Location_x = s0.Location_x - 10;
realsnake.Add(s1);
s2.Location_x = s1.Location_x - 10;
realsnake.Add(s2);
s3.Location_x = s2.Location_x - 10;
realsnake.Add(s3);
//添加四个长度,横向添加list 初始化小蛇蛇完成
}
public static void SnakeMove(int x)//8426
{
int i = realsnake.Count - 1;
if (x == 8)
{
//对list蛇身进行处理
Snakelist();
realsnake[i].Location_y -= 10;
}
else if (x == 4)
{
Snakelist();
realsnake[i].Location_x -= 10;
}
else if (x == 2)
{
Snakelist();
realsnake[i].Location_y += 10;
}
else if (x == 6)
{
Snakelist();
realsnake[i].Location_x += 10;
}
}
public static void Snakelist()
{
int j = 0;
int x, y;
int i = realsnake.Count - 1;
for (j = 0; j < i; j++)
{
x = realsnake[j + 1].Location_x;
y = realsnake[j + 1].Location_y;
realsnake[j].Location_y = y;
realsnake[j].Location_x = x;
}
}
public static void EatLong()
{
Snake S_eat = new Snake();
int xx = realsnake[realsnake.Count - 1].Location_x;
int yy = realsnake[realsnake.Count - 1].Location_y;
if (wasd == 8)
{
S_eat.Location_y = yy - 10;
S_eat.Location_x = xx;
realsnake.Add(S_eat);
}
else if (wasd == 4)
{
S_eat.Location_x = xx - 10;
S_eat.Location_y = yy;
realsnake.Add(S_eat);
}
else if (wasd == 2)
{
S_eat.Location_y = yy + 10;
S_eat.Location_x = xx;
realsnake.Add(S_eat);
}
else if (wasd == 6)
{
S_eat.Location_y = yy;
S_eat.Location_x = xx + 10;
realsnake.Add(S_eat);
}
}
}
}
食物
using System;
namespace 贪吃蛇form
{
public class Food
{
public int Location_x;
public int Location_y;
public Food()
{
Random x = new Random();
Random y = new Random();
Location_x = x.Next(0, 49) * 10;
Location_y = y.Next(0, 49) * 10;
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/143842.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...