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 推箱子_窗体版_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void mapshow_agrin()//从新开始时运行,根据地图信息重新编译图片位置;
{
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 14; j++)
{
if (map_3[i, j] == 1)
{
qiang[qt].Top = i * 20;
qiang[qt].Left = j * 20;
qiang[qt].Image = Image.FromFile(@"C:\Users\Administrator\Desktop/photo/qt.jpg");
qiang[qt].BackgroundImageLayout = ImageLayout.Center;
qt++;
}
if (map_3[i, j] == 2)
{
xiangzi[xz].Top = i * 20;
xiangzi[xz].Left = j * 20;
xiangzi[xz].Image = Image.FromFile(@"C:\Users\Administrator\Desktop/photo/xz.jpg");
xiangzi[xz].BackgroundImageLayout = ImageLayout.Center;
xz++;
}
if (map_3[i, j] == 4)
{
mubiao[mb].Top = i * 20;
mubiao[mb].Left = j * 20;
mubiao[mb].Image = Image.FromFile(@"C:\Users\Administrator\Desktop/photo/mb1.jpg");
mubiao[mb].BackgroundImageLayout = ImageLayout.Center;
mb++;
}
if (map_3[i, j] == 3)
{
y = i;
x = j;
}
}
}
xiaoren1.Height = xiaoren1.Width = 20;
xiaoren1.Top = y * 20;
xiaoren1.Left = x * 20;
//xiaoren.borderstyle = borderstyle.fixedsingle;
xiaoren1.Show();
}
void mapshow()//程序启动时要运行的地图编译程序,按照地图信息生成图片框,实行下一关时也会运行;
{
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 14; j++)
{
if (map_1[i, j] == 1)
{
PictureBox h = new PictureBox();//生成一个新的图片盒子,盒子总体上限可调;
h.Width = h.Height = 20;
h.Top = i*20;
h.Left = j*20;
h.Image = Image.FromFile(@"C:\Users\Administrator\Desktop/photo/qt.jpg");//图片的绝对地址;
h.BackgroundImageLayout = ImageLayout.Center;
//h.BackColor = Color.Peru;
h.BorderStyle = BorderStyle.FixedSingle;
qiang[qt] = h;
this.Controls.Add(h);//将生成的图片添加到控件中;
qt++;
}
if (map_1[i, j] == 2)
{
PictureBox h = new PictureBox();
h.Width = h.Height = 20;
h.Top = i * 20;
h.Left = j * 20;
h.Image = Image.FromFile(@"C:\Users\Administrator\Desktop/photo/xz.jpg");
h.BackgroundImageLayout = ImageLayout.Center;
//h.BackColor = Color.PaleGreen;
//h.BorderStyle=BorderStyle.FixedSingle;
xiangzi[xz] = h;
this.Controls.Add(h);
xz++;
}
if (map_1[i, j] == 4)
{
PictureBox h = new PictureBox();
h.Width = h.Height = 20;
h.Top = i * 20;
h.Left = j * 20;
h.Image = Image.FromFile(@"C:\Users\Administrator\Desktop/photo/mb1.jpg");
h.BackgroundImageLayout = ImageLayout.Center;
//h.BackColor = Color.Yellow;
//h.BorderStyle = BorderStyle.FixedSingle;
h.AutoSize = false;
mubiao[mb] = h;
this.Controls.Add(h);
mb++;
}
if (map_1[i, j] == 3)
{
y = i ;
x = j ;
}
}
}
xiaoren1.Height = xiaoren1.Width = 20;
xiaoren1.Top = y * 20;
xiaoren1.Left = x * 20;
//xiaoren.borderstyle = borderstyle.fixedsingle;
xiaoren1.Show();
}
private void move(object sender, KeyEventArgs e)//整个游戏需要用到的键盘按键控制;
{
if (gamestart==true &&(e.KeyCode == Keys.Up || e.KeyCode == Keys.W))
{
m = Convert.ToInt32(move_m.up);
moveup();
}
if (gamestart == true && (e.KeyCode == Keys.Down || e.KeyCode == Keys.S))
{
m = Convert.ToInt32(move_m.down);
movedown();
}
if (gamestart == true && (e.KeyCode == Keys.Left || e.KeyCode == Keys.A))
{
m = Convert.ToInt32(move_m.left);
moveleft();
}
if (gamestart == true && (e.KeyCode == Keys.Right || e.KeyCode == Keys.D))
{
m = Convert.ToInt32(move_m.right);
moveright();
}
if (gamestart == true && e.KeyCode == Keys.R)
{
congwan_Click(sender,e);
}
}
public void moveup()//向上运动时所需的控制程序;
{
if (map_1[y - 1, x] == 0 || map_1[y - 1, x] == 4)//小人的下一个目标点为空地时;
{
if (map_2[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map_1[y, x] = 4;
map_1[y - 1, x] = 3;
xiaoren1.Top -= 20;
y--;
}
else//如果小人现在的坐标不是目标点的坐标
{
map_1[y - 1, x] = 3;
xiaoren1.Top -= 20;
map_1[y, x] = 0;
y--;
}
}
else if (map_1[y - 1, x] == 1)//小人的下一个坐标为墙,跳过此次循环
{ }
else if (map_1[y - 1, x] == 2 || map_1[y - 1, x] == 5)//小人的下一个坐标为未到达目标的箱子或到达目标的箱子
{
if (map_1[y - 2, x] == 1 || map_1[y - 2, x] == 2 || map_1[y - 2, x] == 5)//箱子的下一个目标为墙或空箱子或到达目标的箱子
{ }
else if (map_1[y - 2, x] == 0)//箱子的下一个目标为空地
{
if (map_2[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map_1[y - 2, x] = 2;
movexz();
map_1[y, x] = 4;
//movemb();
map_1[y - 1, x] = 3;
xiaoren1.Top -= 20;
//movembxz();
y--;
}
else//如果小人现在的坐标不是目标点的坐标
{
map_1[y - 2, x] = 2;
movexz();
//movembxz();
map_1[y - 1, x] = 3;
xiaoren1.Top -= 20;
map_1[y, x] = 0;
y--;
用winform(c#窗体应用程序)实现推箱子小游戏
最新推荐文章于 2024-06-15 09:57:46 发布