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 贪吃蛇
{
public partial class Form1 : Form
{
const int gz格子宽度 = 40;
const int gz格子行数 = 15;
const int gz格子列数 = 20;
public enum gz格子类型
{
空,蛇头,蛇身,蛇尾,果子
}
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
绘制格线(gz格子宽度, gz格子行数, gz格子列数, g);
ht绘图(g, i蛇头位置, j蛇头位置, gz格子类型.蛇头);
//gz绘图(g, 3, j蛇身位置, gz格子类型.蛇身);
//gz绘图(g, 3, j蛇尾位置, gz格子类型.蛇尾);
ht绘图(g, 7, 7, gz格子类型.果子);
} private void ht绘图(Graphics g, int i, int j,gz格子类型 gz类型)
{
Brush brush;
switch (gz类型)
{
case gz格子类型.果子:
brush = new SolidBrush(Color.Red);
g.FillRectangle(brush, gz格子宽度 * j, gz格子宽度 * i, 40, 40);
break;
case gz格子类型.蛇头:
brush = new SolidBrush(Color.CadetBlue);
g.FillEllipse(brush, gz格子宽度 * j, gz格子宽度 * i, 40, 40);
break;
case gz格子类型.蛇身:
brush = new SolidBrush(Color.CadetBlue);
g.FillRectangle(brush, gz格子宽度 * j, gz格子宽度 * i, 40, 40);
break;
case gz格子类型.蛇尾:
brush = new SolidBrush(Color.CadetBlue);
g.FillRectangle(brush, gz格子宽度 * j, gz格子宽度 * i, 40, 40);
break;
}
} private static void 绘制格线(int gz格子宽度, int gz格子行数, int gz格子列数, Graphics g)
{
Pen pen粗笔 = new Pen(Color.FromArgb(255, 0, 0, 0), 2);
for (int i = 0; i < gz格子行数 + 1; ++i)
{
g.DrawLine(pen粗笔, gz格子宽度, gz格子宽度 * i + gz格子宽度, gz格子宽度 * gz格子列数 + gz格子宽度, gz格子宽度 * i + gz格子宽度);
}
for (int j = 0; j < gz格子列数 + 1; ++j)
{
g.DrawLine(pen粗笔, gz格子宽度 * j + gz格子宽度, gz格子宽度, gz格子宽度 * j + gz格子宽度, gz格子宽度 * gz格子行数 + gz格子宽度);
} pen粗笔.Dispose();
}
public enum FX方向
{
不动,上,下,左,右
}
int i蛇头位置=3, j蛇头位置=4,i蛇身位置,j蛇身位置=3,i蛇尾位置,j蛇尾位置=2;
private void timer1_Tick(object sender, EventArgs e)
{
j蛇头位置++;
if (j蛇头位置 == gz格子列数+1)
{
j蛇头位置 = 1;
}
Refresh();
}
FX方向 fx方向;
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
switch ((Keys)e.KeyChar)
{
case Keys.Up:
fx方向 = FX方向.上;
break;
case Keys.Down:
fx方向 = FX方向.下;
break;
case Keys.Left:
fx方向 = FX方向.左;
break;
case Keys.Right:
fx方向 = FX方向.右;
break;
}
}
}
}求在timer1_Tick里实现接收键盘事件的语句
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace 贪吃蛇
{
public partial class Form1 : Form
{
const int gz格子宽度 = 40;
const int gz格子行数 = 15;
const int gz格子列数 = 20;
public enum gz格子类型
{
空,蛇头,蛇身,蛇尾,果子
}
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
绘制格线(gz格子宽度, gz格子行数, gz格子列数, g);
ht绘图(g, i蛇头位置, j蛇头位置, gz格子类型.蛇头);
//gz绘图(g, 3, j蛇身位置, gz格子类型.蛇身);
//gz绘图(g, 3, j蛇尾位置, gz格子类型.蛇尾);
ht绘图(g, 7, 7, gz格子类型.果子);
} private void ht绘图(Graphics g, int i, int j,gz格子类型 gz类型)
{
Brush brush;
switch (gz类型)
{
case gz格子类型.果子:
brush = new SolidBrush(Color.Red);
g.FillRectangle(brush, gz格子宽度 * j, gz格子宽度 * i, 40, 40);
break;
case gz格子类型.蛇头:
brush = new SolidBrush(Color.CadetBlue);
g.FillEllipse(brush, gz格子宽度 * j, gz格子宽度 * i, 40, 40);
break;
case gz格子类型.蛇身:
brush = new SolidBrush(Color.CadetBlue);
g.FillRectangle(brush, gz格子宽度 * j, gz格子宽度 * i, 40, 40);
break;
case gz格子类型.蛇尾:
brush = new SolidBrush(Color.CadetBlue);
g.FillRectangle(brush, gz格子宽度 * j, gz格子宽度 * i, 40, 40);
break;
}
} private static void 绘制格线(int gz格子宽度, int gz格子行数, int gz格子列数, Graphics g)
{
Pen pen粗笔 = new Pen(Color.FromArgb(255, 0, 0, 0), 2);
for (int i = 0; i < gz格子行数 + 1; ++i)
{
g.DrawLine(pen粗笔, gz格子宽度, gz格子宽度 * i + gz格子宽度, gz格子宽度 * gz格子列数 + gz格子宽度, gz格子宽度 * i + gz格子宽度);
}
for (int j = 0; j < gz格子列数 + 1; ++j)
{
g.DrawLine(pen粗笔, gz格子宽度 * j + gz格子宽度, gz格子宽度, gz格子宽度 * j + gz格子宽度, gz格子宽度 * gz格子行数 + gz格子宽度);
} pen粗笔.Dispose();
}
public enum FX方向
{
不动,上,下,左,右
}
int i蛇头位置=3, j蛇头位置=4,i蛇身位置,j蛇身位置=3,i蛇尾位置,j蛇尾位置=2;
private void timer1_Tick(object sender, EventArgs e)
{
j蛇头位置++;
if (j蛇头位置 == gz格子列数+1)
{
j蛇头位置 = 1;
}
Refresh();
}
FX方向 fx方向;
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
switch ((Keys)e.KeyChar)
{
case Keys.Up:
fx方向 = FX方向.上;
break;
case Keys.Down:
fx方向 = FX方向.下;
break;
case Keys.Left:
fx方向 = FX方向.左;
break;
case Keys.Right:
fx方向 = FX方向.右;
break;
}
}
}
}求在timer1_Tick里实现接收键盘事件的语句
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货