frmSnake.cs
上传用户:cljx11
上传日期:2022-08-09
资源大小:49k
文件大小:5k
- 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 Snake_v2
- {
- public partial class frmSnake : Form
- {
- public frmSnake()
- {
- InitializeComponent();
- }
- private Block b = new Block();
- private SnakeMove mov = new SnakeMove();
- private int _event = 1;
- private bool _door = true;
- private bool _over = false;
- private void GameReset()
- {
- Graphics gp = picBG.CreateGraphics();
- SnakeBody.BodyClear();
- SnakeMap.MapClear();
- gp.Clear(Color.LightSteelBlue);
- SnakeBody.NewSnake(3);
- Apple.NewApple();
- _event = 1;
- GamePaint(gp);
- }
- private void GameOver(Graphics gp)
- {
- SnakeBody.BodyClear();
- SnakeMap.MapClear();
- gp.Clear(Color.LightSteelBlue);
- gp.DrawString("Game Over~~!", new Font("Verdana", 20), new SolidBrush(Color.Orange), 70, 55);
- timer.Enabled = false;
- _over = true;
- gp.Dispose();
- }
- private void GamePaint(Graphics gp)
- {
- gp.Clear(Color.LightSteelBlue);
- for (int i = 0; i < SnakeBody.BodyX.Count; i++)
- b.SnakeBody((int)SnakeBody.BodyX[i], (int)SnakeBody.BodyY[i], gp);
- b.Apple(Apple.GetAppleX(), Apple.GetAppleY(), gp);
- }
- private void picBG_Paint(object sender, PaintEventArgs e)
- {
- Graphics gp = e.Graphics;
- gp.Clear(Color.LightSteelBlue);
- if (_over == false) GamePaint(gp);
- else
- {
- gp.Clear(Color.LightSteelBlue);
- gp.DrawString("Game Over~~!", new Font("Verdana", 20), new SolidBrush(Color.Orange), 70, 55);
- }
- }
- private void btnStar_Click(object sender, EventArgs e)
- {
- GameReset();
- timer.Enabled = true;
- _over = false;
- }
- private void btnClose_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- #region TimerTick事件
- private void timer_Tick(object sender, EventArgs e)
- {
- Graphics gp = picBG.CreateGraphics();
- if (_event == 1)
- {
- if (mov.Mov(1, 0,gp) == false)
- {
- GameOver(gp);
- return;
- }
- }
- else if (_event == -1)
- {
- if (mov.Mov(-1, 0,gp) == false)
- {
- GameOver(gp);
- return;
- }
- }
- else if (_event == 2)
- {
- if (mov.Mov(0, -1,gp) == false)
- {
- GameOver(gp);
- return;
- }
- }
- else if (_event == -2)
- {
- if (mov.Mov(0, 1,gp) == false)
- {
- GameOver(gp);
- return;
- }
- }
- _door = true;
- }
- #endregion
- #region 键盘响应
- protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
- {
- if (keyData == Keys.Right)
- {
- if (_event != -1 && _door)
- {
- _event = 1;
- _door = false;
- }
- return true;
- }
- else if (keyData == Keys.Left)
- {
- if (_event != 1 && _door)
- {
- _event = -1;
- _door = false;
- }
- return true;
- }
- else if (keyData == Keys.Up)
- {
- if (_event != -2 && _door)
- {
- _event = 2;
- _door = false;
- }
- return true;
- }
- else if (keyData == Keys.Down)
- {
- if (_event != 2 && _door)
- {
- _event = -2;
- _door = false;
- }
- return true;
- }
- return base.ProcessCmdKey(ref msg, keyData);
- }
- #endregion
- }
- }