博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
随手写 --- 贪吃蛇
阅读量:5345 次
发布时间:2019-06-15

本文共 6285 字,大约阅读时间需要 20 分钟。

public class BodyCell : PictureBox    {        public Direction direct        { get; set; }        public BodyCell()            : base()        {            this.Width = 10;            this.Height = 10;            this.BackColor = Color.Black;            direct = Direction.Right;        }    }using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace Snake{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        public int bodyCount = 5;        public Direction direct = Direction.Right;        //public BodyCell[] cells;        public List
cells; public PictureBox food; private void Form1_Load(object sender, EventArgs e) { cells = new List
(); for (int i = 0; i < bodyCount; i++) { BodyCell cell = new BodyCell(); cell.Left = i * 10; cell.Top = panel1.Height / 2; cells.Add(cell); panel1.Controls.Add(cell); } food = new PictureBox(); food.BackColor = Color.Black; food.Height = 10; food.Width = 10; Random rY = new Random(); food.Top = rY.Next(0, 30) * 10; Random rX = new Random(); food.Left = rX.Next(0, 30) * 10; panel1.Controls.Add(food); } private void Timer_Process_Tick(object sender, EventArgs e) { SnakeProcess(); if (GameOver()) { Timer_Process.Enabled = false; MessageBox.Show("Game Over"); } } private void SnakeProcess() { #region 移动 for (int i = bodyCount - 1; i >= 0; i--) { if (cells[i].direct == Direction.Right) { cells[i].Left += 10; } else if (cells[i].direct == Direction.Left) { cells[i].Left -= 10; } else if (cells[i].direct == Direction.Up) { cells[i].Top -= 10; } else if (cells[i].direct == Direction.Down) { cells[i].Top += 10; } } for (int i = 0; i < bodyCount - 1; i++) { cells[i].direct = cells[i + 1].direct; } #endregion #region 吃食物 EatFood(); #endregion } private bool GameOver() { bool isOver = false; //撞墙 if (cells[bodyCount - 1].Top == 0 && cells[bodyCount - 1].direct == Direction.Up) { isOver = true; } if (cells[bodyCount - 1].Top == 300 && cells[bodyCount - 1].direct == Direction.Down) { isOver = true; } if (cells[bodyCount - 1].Left == 0 && cells[bodyCount - 1].direct == Direction.Left) { isOver = true; } if (cells[bodyCount - 1].Left == 300 && cells[bodyCount - 1].direct == Direction.Right) { isOver = true; } //撞自己 for (int i = bodyCount - 2; i >= 0; i--) { if (cells[i].Top == cells[bodyCount - 1].Top && cells[i].Left == cells[bodyCount - 1].Left) { isOver = true; break; } } return isOver; } private void EatFood() { BodyCell head = cells[bodyCount - 1]; if (cells[bodyCount - 1].Top == food.Top && cells[bodyCount - 1].Left == food.Left) { BodyCell cell = new BodyCell(); panel1.Controls.Add(cell); cell.direct = head.direct; if (head.direct == Direction.Up) { cell.Top = head.Top - 10; cell.Left = head.Left; } else if (head.direct == Direction.Down) { cell.Top = head.Top + 10; cell.Left = head.Left; } else if (head.direct == Direction.Left) { cell.Top = head.Top; cell.Left = head.Left - 10; } else if (head.direct == Direction.Right) { cell.Top = head.Top; cell.Left = head.Left + 10; } cells.Add(cell); bodyCount++; panel1.Controls.Remove(food); Random rY = new Random(); food.Top = rY.Next(0, 30) * 10; Random rX = new Random(); food.Left = rX.Next(0, 30) * 10; panel1.Controls.Add(food); } } private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up && cells[bodyCount - 1].direct != Direction.Down) { //direct = Direction.Up; cells[bodyCount - 1].direct = Direction.Up; } else if (e.KeyCode == Keys.Left && cells[bodyCount - 1].direct != Direction.Right) { //direct = Direction.Left; cells[bodyCount - 1].direct = Direction.Left; } else if (e.KeyCode == Keys.Down && cells[bodyCount - 1].direct != Direction.Up) { //direct = Direction.Down; cells[bodyCount - 1].direct = Direction.Down; } else if (e.KeyCode == Keys.Right && cells[bodyCount - 1].direct != Direction.Left) { //direct = Direction.Right; cells[bodyCount - 1].direct = Direction.Right; } } } public enum Direction { Up, Down, Left, Right }}

  

posted on
2014-10-22 16:46 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/september-bk/p/4043661.html

你可能感兴趣的文章
UILabel
查看>>
【热门技术】三种SEO方式
查看>>
[Hades_技术]哈迪斯初级技术应用
查看>>
SQLiteOpenHelper
查看>>
Luogu P1141 01迷宫【搜索/dfs】By cellur925
查看>>
js onclick事件传参
查看>>
WiCloud 商业Wi-Fi管理平台
查看>>
团队项目--未完待续
查看>>
双重标准,我该怎么解决
查看>>
python中的网页标签等字符处理
查看>>
Mybatis输入类型和结果类型
查看>>
Linux常用命令(五)
查看>>
Linux常用命令(四)
查看>>
Linux常用命令(六)
查看>>
Linux常用命令(六)
查看>>
Linux常用命令(八)
查看>>
Linux常用命令(七)
查看>>
Linux常用命令(九)
查看>>
Linux常用命令(十一)
查看>>
Linux常用命令(十)
查看>>