• 大小: 88KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2024-02-05
  • 语言: C#
  • 标签: C#  贪吃蛇  小游戏  

资源简介

用C#实现的简单的贪吃蛇小游戏,附所有源代码和项目文件,编译器VS2008。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 贪吃蛇1._0
{
    public partial class Form1 : Form
    {
        int direction=1;//方向,1234代表上下左右
        int length=3;
        int score = 0;
        int level = 1;
        Point lastfood=new Point(00);
        List now_snake=new List();
        
        public Form1()
        {
            InitializeComponent();
            now_snake.Add(new Point(140120));
            now_snake.Add(new Point(140130));
            now_snake.Add(new Point(140140));
           
        }

        private void add_food()//去除旧的食物并增加新的食物
        {
            Graphics g = panelscreen.CreateGraphics();
            Pen pb = new Pen(Brushes.Black);
            Pen pw = new Pen(panelscreen.BackColor);
            if (now_snake.Count > 3)
            {
                g.FillRectangle(pb.Brush lastfood.X lastfood.Y 10 10);
                g.DrawRectangle(pb lastfood.X lastfood.Y 10 10);
            }
            Random ran = new Random();
            do
            {
                lastfood.X = ran.Next(0 14) * 10;
                lastfood.Y = ran.Next(0 14) * 10;
            }
            while(now_snake.Contains(lastfood));
            g.FillEllipse(Brushes.Green lastfood.X lastfood.Y 10 10);
            g.DrawEllipse(pw lastfood.X lastfood.Y 10 10);
        }

        private void eat_food()
        {
            Point p=now_snake[0];
            if (direction == 1 && p.X == lastfood.X && p.Y == (10 + lastfood.Y) ||
                direction == 2 && p.X == lastfood.X && p.Y == (lastfood.Y - 10) ||
                direction == 3 && p.Y == lastfood.Y && p.X == (10 + lastfood.X) ||
                direction == 4 && p.Y == lastfood.Y && p.X == (lastfood.X - 10)
                )
            {
                
                now_snake.Add(new Point(lastfood.Xlastfood.Y));
                add_food();
                length++;
                score += level;
                labellength.Text = “当前长度:“ + length.ToString();
                labellevel.Text = “当前速度:“ + level.ToString();
                labelscore.Text = “当前分数:“ + score.ToString();
            }
        }

        private void snake_ran()
        {
            if (go_out())
            {
                buttonend_Click(buttonend new EventArgs());
                return;
            }
            eat_food();
            Graphics g = panelscreen.CreateGraphics();
            Pen pb = new Pen(Brushes.Black);
            Pen pw = new Pen(panelscreen.BackColor);
            g.FillRectangle(pw.Brush now_snake[now_snake.Count - 1].X now_snake[now_snake.Count - 1].Y 10 10);
            now_snake.Remove(now_snake[now_snake.Count-1]);
            Point p = now_snake[0];
            swi

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      16384  2010-12-18 11:34  贪吃蛇1.0\贪吃蛇1.0\bin\Debug\贪吃蛇1.0.exe

     文件      32256  2010-12-18 11:34  贪吃蛇1.0\贪吃蛇1.0\bin\Debug\贪吃蛇1.0.pdb

     文件      14328  2010-12-18 20:53  贪吃蛇1.0\贪吃蛇1.0\bin\Debug\贪吃蛇1.0.vshost.exe

     文件        490  2009-06-11 05:14  贪吃蛇1.0\贪吃蛇1.0\bin\Debug\贪吃蛇1.0.vshost.exe.manifest

     文件      17408  2010-12-20 12:01  贪吃蛇1.0\贪吃蛇1.0\bin\Release\贪吃蛇1.0.exe

     文件      30208  2010-12-20 12:01  贪吃蛇1.0\贪吃蛇1.0\bin\Release\贪吃蛇1.0.pdb

     文件      14328  2011-01-20 14:44  贪吃蛇1.0\贪吃蛇1.0\bin\Release\贪吃蛇1.0.vshost.exe

     文件        490  2009-06-11 05:14  贪吃蛇1.0\贪吃蛇1.0\bin\Release\贪吃蛇1.0.vshost.exe.manifest

     文件       8306  2010-12-20 12:01  贪吃蛇1.0\贪吃蛇1.0\Form1.cs

     文件      10716  2010-12-20 11:46  贪吃蛇1.0\贪吃蛇1.0\Form1.Designer.cs

     文件       7924  2010-12-19 23:27  贪吃蛇1.0\贪吃蛇1.0\Form1.resx

     文件        825  2010-12-18 20:53  贪吃蛇1.0\贪吃蛇1.0\obj\Debug\贪吃蛇1.0.csproj.FileListAbsolute.txt

     文件        847  2010-12-18 11:31  贪吃蛇1.0\贪吃蛇1.0\obj\Debug\贪吃蛇1.0.csproj.GenerateResource.Cache

     文件      16384  2010-12-18 11:34  贪吃蛇1.0\贪吃蛇1.0\obj\Debug\贪吃蛇1.0.exe

     文件      32256  2010-12-18 11:34  贪吃蛇1.0\贪吃蛇1.0\obj\Debug\贪吃蛇1.0.pdb

     文件        180  2010-12-18 11:31  贪吃蛇1.0\贪吃蛇1.0\obj\Debug\贪吃蛇1._0.Form1.resources

     文件        180  2010-12-16 20:58  贪吃蛇1.0\贪吃蛇1.0\obj\Debug\贪吃蛇1._0.Properties.Resources.resources

     文件       4608  2010-12-18 23:00  贪吃蛇1.0\贪吃蛇1.0\obj\Release\TempPE\Properties.Resources.Designer.cs.dll

     文件        841  2011-01-20 14:44  贪吃蛇1.0\贪吃蛇1.0\obj\Release\贪吃蛇1.0.csproj.FileListAbsolute.txt

     文件        847  2010-12-19 23:27  贪吃蛇1.0\贪吃蛇1.0\obj\Release\贪吃蛇1.0.csproj.GenerateResource.Cache

     文件      17408  2010-12-20 12:01  贪吃蛇1.0\贪吃蛇1.0\obj\Release\贪吃蛇1.0.exe

     文件      30208  2010-12-20 12:01  贪吃蛇1.0\贪吃蛇1.0\obj\Release\贪吃蛇1.0.pdb

     文件       1644  2010-12-19 23:27  贪吃蛇1.0\贪吃蛇1.0\obj\Release\贪吃蛇1._0.Form1.resources

     文件        180  2010-12-18 23:00  贪吃蛇1.0\贪吃蛇1.0\obj\Release\贪吃蛇1._0.Properties.Resources.resources

     文件        494  2010-12-16 20:53  贪吃蛇1.0\贪吃蛇1.0\Program.cs

     文件       1356  2010-12-16 20:53  贪吃蛇1.0\贪吃蛇1.0\Properties\AssemblyInfo.cs

     文件       2850  2010-12-18 23:00  贪吃蛇1.0\贪吃蛇1.0\Properties\Resources.Designer.cs

     文件       5961  2010-12-18 23:00  贪吃蛇1.0\贪吃蛇1.0\Properties\Resources.resx

     文件       1098  2010-12-16 20:53  贪吃蛇1.0\贪吃蛇1.0\Properties\Settings.Designer.cs

     文件        249  2010-12-16 20:53  贪吃蛇1.0\贪吃蛇1.0\Properties\Settings.settings

............此处省略21个文件信息

评论

共有 条评论