【实例简介】
本推箱子游戏使用数组实现,用不同的数字标识不同的物体,从而简化了人物移动的判断过程
【实例截图】
【核心代码】
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;
using System.Media;
namespace 推箱子2._0
{
public partial class Form1 : Form
{
public const int len=10;//数组长度
int [,] num = new int[len,len];//初始化数组,数组元素全为0
public const int height = 50, width = 50;//墙的长宽
int row,col;//标记人物位置
int direction = 2;//方向1,2,3,4代表上下左右
//SoundPlayer player = new SoundPlayer();
int leval = 1;//关卡等级标记
int map = 1;//地图
public Form1()
{
InitializeComponent();
initNum();
this.KeyPreview = true;
this.BackgroundImage = new Bitmap(@"map1\floor1.png");//初始化窗口背景
}
/* 初始化界面 */
private void initGameView()
{
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);//创建一个位图,作为PictureBox的Image
label2.Text = "第 " leval " 关";
Bitmap bitmp;//用于承载物体的图片
Graphics g = Graphics.FromImage(bmp);
if (map == 2)//地图2
{
for (int i = 0; i < len; i )
{
for (int j = 0; j < len; j )
{
bitmp = new Bitmap(@"map2\block.gif");
if (num[i, j] == 0)
bitmp = new Bitmap(@"map2\block.gif");
if (num[i, j] == 1)
bitmp = new Bitmap(@"map2\wall.png");
if (num[i, j] == 2)
bitmp = new Bitmap(@"map2\box.png");
if (num[i, j] == 3)
bitmp = new Bitmap(@"map2\ball.png");
if (num[i, j] == 4)
{
switch (direction)
{
case 1:
bitmp = new Bitmap(@"map2\person_up.png"); break;
case 2:
// bitmp = new Bitmap(@"Images\pic04down.png"); break;
bitmp = new Bitmap(@"map2\person_down.png"); break;
case 3:
bitmp = new Bitmap(@"map2\person_left.png"); break;
case 4:
bitmp = new Bitmap(@"map2\person_right.png"); break;
}
row = i;
col = j;
}
if (num[i, j] == 5)
bitmp = new Bitmap(@"map2\person5.jpg");
if (num[i, j] == 6)
bitmp = new Bitmap(@"map2\box.png");
g.DrawImage(bitmp, i * width, j * height, width, height);
}
}
pictureBox1.Image = bmp;
}
if (map == 1)//地图1
{
for (int i = 0; i < len; i )
{