编程-程序媛小白求助:C#贪吃蛇精灵游戏

问题描述

程序媛小白求助:C#贪吃蛇精灵游戏

我编了一个贪吃蛇的游戏,代码如下。编译无错误,但运行后蛇不会动啊~我真的真的不知道问题出在哪儿。多谢各位大神指教~
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;

namespace GreedySnake3
{

public partial class Form2 : Form
{
    Snake snack1 = new Snake(4);
    private System.Drawing.Color foodColor = System.Drawing.Color.Red;
    Bean food = new Bean();
    bool play = false;
    bool foodDraw = false;
    private void Form2_Load(object sender, EventArgs e)
    {
           pictureBox1.Paint += pictureBox1_Paint;
           timer1.Tick+=timer1_Tick;
    }

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        play = true;
        this.timer1.Enabled = true;
        this.timer1.Interval = 500;

    }
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        DrawGameFrame(e.Graphics);   

        snack1.DrawSnack(e.Graphics);   

        if (foodDraw == false)
        {
            food.GetFoodPoint();
            foodDraw = true;
        }
        food.DrawFood(e.Graphics);   

        if (play)
        {
            snack1.SnackMoce(e.Graphics);

        }
        if (this.timer1.Enabled == true)
        {
            this.button1.Focus();
        }

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (play)
        {
            snack1.Addsnack();
        }
        if (snack1.HeadPoint == food.Origin)
        {

            foodDraw = false;
            snack1.Addsnack();

        }
        if (snack1.Diedif())
        {
            timer1.Enabled = false;
            play = false;
            MessageBox.Show("Game Over!");

        }
    }

    public void DrawGameFrame(Graphics g)
    {
        for (int i = 0; i < this.pictureBox1.Width; i += 10)
            for (int j = 0; j < this.pictureBox1.Height; j += 10)
            {
                g.FillEllipse(Brushes.LightGreen, i, j, 10, 10);
            }
    }

    private void button1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.A  && snack1.Direction != 1)
            snack1.Direction = 2;
        else if (e.KeyCode == Keys.D && snack1.Direction != 2)
            snack1.Direction = 1;
        else if (e.KeyCode == Keys.W && snack1.Direction != 4)
            snack1.Direction = 3;
        else if (e.KeyCode == Keys.S && snack1.Direction != 3)
            snack1.Direction = 4;

    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }

}
public class Bean
{
    Point origin=new Point(100,100);
    public Point Origin
    {
        get { return origin; }
        set { origin = value; }
    }
    public void GetFoodPoint()
        {
            Random random = new Random();
            int x = random.Next(1, 40)*10;
            int y = random.Next(1, 30)*10;
            origin = new Point(x, y);
        }
        public void DrawFood(Graphics g)
        {
            g.FillEllipse(Brushes.Red, origin.X, origin.Y, 10, 10);
        }
    }

public class Snake
{
    public Point startPoint = new Point(0, 0);
    Point addPoint;
    public ArrayList snackPoint = new ArrayList();
    Point headPoint;
    public Point HeadPoint
    {
        get { return headPoint; }
        set { headPoint = value; }
    }
    int direction = 1;
    public int Direction
    {
        get { return direction; }
        set { direction = value; }
    }

    public Snake(int lenth)
    {
        for (int i = 0; i < lenth; i++)
        {
            snackPoint.Add(startPoint);
            if (i == lenth - 1)
            {
                HeadPoint = startPoint;
                return;
            }
            startPoint = new Point(startPoint.X + 10, startPoint.Y);
        }
    }
    public void DrawSnack(Graphics g)
    {
        for (int i = 0; i < snackPoint.Count; i++)
        {
            g.FillEllipse(Brushes.Blue, ((Point)snackPoint[i]).X, ((Point)snackPoint[i]).Y, 10, 10);
        }
    }

    public void Addsnack()
    {
        if (direction == 1)
            addPoint = new Point(headPoint.X + 10, headPoint.Y);
        else if (direction == 2)
            addPoint = new Point(headPoint.X - 10, headPoint.Y);
        else if (direction == 3)
            addPoint = new Point(headPoint.X, headPoint.Y - 10);
        else if (direction == 4)
            addPoint = new Point(headPoint.X, headPoint.Y + 10);
        snackPoint.Add(addPoint);
        headPoint = addPoint;
    }

    public void RemoveSnackNode()
    {
        snackPoint.RemoveAt(0);
    }

    public void SnackMoce(Graphics g)
    {
        DrawSnack(g);
        RemoveSnackNode();
    }

    public bool Diedif()
    {
        for (int i = 0; i < snackPoint.Count - 1; i++)
        {
            if ((Point)snackPoint[i] == headPoint)
                return true;
        }

        if (headPoint.X < 0 || headPoint.X > 400 || headPoint.Y < 0 || headPoint.Y > 300)
            return true;
        return false;
    }
}

}

解决方案

pictureBox1 的paint 事件不会时时触发 在timer1_tick中添加 pictureBox1.Refresh(); 让它定时触发paint 重新绘制自己
private void timer1_Tick(object sender, EventArgs e)
{
if (play)
{
pictureBox1.Refresh();
snack1.Addsnack();

        }
        if (snack1.HeadPoint == food.Origin)
        {
            pictureBox1.Refresh();
            foodDraw = false;
            snack1.Addsnack();

        }
        if (snack1.Diedif())
        {
            pictureBox1.Refresh();
            timer1.Enabled = false;
            play = false;
            MessageBox.Show("Game Over!");

        }
    }

解决方案二:

你的代码是直接复制粘贴的么?选中你的PictureBox1,在属性窗格里点闪电按钮,看Paint事件关联给pictureBox1_Paint了没有。

解决方案三:

你的timer好像没有start

解决方案四:

没有错误么

时间: 2024-09-11 21:46:56

编程-程序媛小白求助:C#贪吃蛇精灵游戏的相关文章

设计-小白求助 C++编程 急 在线等

问题描述 小白求助 C++编程 急 在线等 1.定义一个职工类CWorker,数据成员包括姓名.职工号.工龄.工资:成员函数有构造函数,设置数据成员值的函数,读取数据成员值的函数.在主函数中定义对象数组,再编写一个CWorker类的友元函数,计算平均工资,查找最高工资. 2.设计一个职工类CWorker,成员包括姓名.职工号.工龄.工资及相关的成员函数.由它派生出教师类CTeacher,包括职称,所属部门等属性和相关的成员函数.编写一个主函数,对设计的类进行测试.

c语言-简单的C语言程序--小白求助

问题描述 简单的C语言程序--小白求助 #include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> int main() { int N,n,i; char t[1000],s[1000]; scanf("%d",&N); while(N--) { gets(t); scanf("%d",&n); for(i=0;i&

小白求助-c/c++如何实现启动一个指定的应用程序,并且能够检测该程序的版本号~~~~~~~~~!!!!

问题描述 c/c++如何实现启动一个指定的应用程序,并且能够检测该程序的版本号~~~~~~~~~!!!! 小白求助:我想写一个程序来启动谷歌浏览器,并且检测谷歌浏览器的版本.我该如何 实现呢?求大神给出思路,最好能够告诉我用到什么 API ....... 解决方案 system("chrome路径")就可以打开chrome了,版本号真不知道..不过我看 chrome路径下有一个目录,目录名就是版本号,但要注意目录可能有多个,所以取最新的 解决方案二: 可以获得这个应用程序的属性,属性里

小白求助,这个c++程序为什么第一次循环for跳过getline?

问题描述 小白求助,这个c++程序为什么第一次循环for跳过getline? string sen: int n; cin>>n; for(int i=1;i<=(n-1);++i) { getline(cin,sen); cout<<sen; counting(sen);//counting是一个函数 cout<<"a:"<<numa<<endl; cout<<"e:"<<n

编译器-小白求助,求质数程序死循环

问题描述 小白求助,求质数程序死循环 for i in range(1,10000): for n in range(1, (i - 1)): if ( (i % n) != 0): print i 解决方案 import math def isPrime(n): if n <= 1: return False for i in range(2,int(math.sqrt(n))+1): if n%i == 0: return False return True def hasPrime(n):

c语言-新人小白求助C语言 各位大大快来啊

问题描述 新人小白求助C语言 各位大大快来啊 #include int main(void) { int a; int b; int c; int n; printf("请输入三个整数值 分别代表 日 月 年:"); scanf("%d",&a); scanf("%d",&b); scanf("%d",&c); n=a%10( n==1?printf("%dst ",a):( n%1

学术-入门网络小白求助热心大神

问题描述 入门网络小白求助热心大神 我是一名网络工程专业的学生,今年大二,喜欢网络安全开发方向的东西.希望各路大神提供一下好的建议,已经自学完计算机网络教材. 解决方案 网络安全需要你对网络体系结构.协议.密码学.操作系统.软件开发.逆向工程等等有非常透彻的理解和研究.网络安全算是计算机科学中国外差距比较大的领域,如果你立志从事这些研究,而且你才大二,有条件最好出国深造. 解决方案二: 网络方面可能会需要一些类似CCIE的证书神马的 解决方案三: APUE和Unix网络编程可有看?

udp-C++套接字编程遇到问题,求助大神

问题描述 C++套接字编程遇到问题,求助大神 小弟大学党,操作系统老师布置作业,写一个可以实现收发报文的小程序,要求使用socket套接字.自己去网上查找资料,尝试写了一个,开始单独收或者单独发的时候都没问题,但是想把收发的功能整合到一个程序里面的时候遇到问题. 我用的是UDP点对点通信机制,测试的时候,用了寝室同学的电脑,两台电脑在相同平台下(vc++ 6.0)编译运行,在编译之前把接收绑定的地址和发送地址对调(我这里的发送地址对应他那边的接收地址,地址都是在本地连接上查看的).但是我发现,我

poi excel 排序-小白求助:poi排序excel的问题

问题描述 小白求助:poi排序excel的问题 怎么让生成出来的excel,按规则排序 我的程序是读取指定excel,然后一行一行的读,一行一行的生成excel. 但生成的excel需要排序,我只能再写一个方法,读我生成的excel,再生成一个排序好的excel 解决方案 题主要按照什么排序呢?你先把数据从excel中拿出来,后根据需要进行排序就好了嘛. 解决方案二: 在你的生成Excel的方法前面加上你所需要的排序方法