创建一个4方向的蛇应用程序 [英] Creating a 4 direction snake application

查看:84
本文介绍了创建一个4方向的蛇应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我创建了一个简单的蛇游戏。我的项目包括一个面板和2个按钮(向左,向右)。点击右键程序后,检查蛇的头是否到达饲料。如果是的话会得到鱼。每次点击都会让蛇步入1个图片框。



这里没有问题。但我想开发我的项目。即,我想将我的项目转换为4方向蛇游戏。例如,表格将是10 * 10的图片框,并且将有4个按钮;左,右,上,下。



我花了太多时间,但我无法解决。是否有任何解决方案或建议?



Hello everyone,

I created a simple snake game. My project includes a panel and 2 buttons (go left, go right). After clicking right button program checks whether snake''s head reached to the feed or not. If yes gets the fish. Every click steps the snakes just 1 picturebox.

There is no problem until here. But I want to develop my project. namely, I want to convert my project into 4 direction snake game. For instance, the table will be 10*10 picture box are and there will be 4 buttons; left, right, up and down.

I have spend too much time but I couldn''t solve. Is there any solution or suggestion?

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 snakeEW
{
    public partial class Form1 : Form
    {
        private PictureBox[] pc;
        private int f, r;
        int sp, x = 0, y = 0;
        Random random = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            f=0;r=0;
            pc = new PictureBox[20];

            sp = random.Next() % 18 + 1;// Position of Bird selected
            sp = 5;
            int with = 40, height = 40;

            for (int i = 0; i < 20; i++)
            {
                pc[i] = new PictureBox();
                pc[i].Image = imageList1.Images[0];

                pc[i].Size = new Size(with, height);
                pc[i].Location = new Point(x, y);
                if (i == 0) pc[0].Visible = true;
                else pc[i].Visible = false;
                if (i == sp)
                {
                    pc[i].Image = imageList1.Images[1];
                    pc[i].Visible = true;
                }
                x += 40;
                this.panel1.Controls.Add(pc[i]);
            }

        }
        private void btnE_Click(object sender, EventArgs e)//Go East
        {
            if (f == 19)
            { MessageBox.Show("No more RIGHT"); return; }
            else
            {
                pc[++f].Visible = true;
                if (f != sp)
                {
                    pc[r++].Visible = false;

                }
                else
                {
                    pc[f].Image = imageList1.Images[0];
                    getFish();

                }
            }
        }

        private void getFish()
        {
            do
                sp = random.Next() % 19 + 1;
            while (sp >=r && sp<= f);
            pc[sp].Image = imageList1.Images[1];
            pc[sp].Visible = true;
       }

        private void btnW_Click(object sender, EventArgs e)//Go West
        {
            if (r == 0) { MessageBox.Show("No more LEFT"); return; }
            else
            {
                pc[--r].Visible = true;
                if (r != sp)
                {
                    pc[f--].Visible = false;

                }
                else
                {
                    pc[r].Image = imageList1.Images[0];
                    getFish();

                }
            }
        }

    }
  }





我应该使用节点。



感谢大家。祝你有个美好的一天。



I should use node.

Thanks to everyone. Have nice day.

推荐答案

问题与现有代码相同。对于向上箭头,您递减垂直位置,向下箭头递增它。您还可能会发现使用比 f r 更有用的变量名更容易。
The issue is just the same as your existing code. For the up arrow you decrement the vertical position and for the down arrow you increment it. You also might find it easier to use more useful variable names than f and r.


这篇关于创建一个4方向的蛇应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆