• 贪吃蛇C#和JAVA实现


      1 using System;
      2 using System.Windows.Forms;
      3 using System.Drawing; 
      4 class Window : Form
      5 {
      6     Point[] a = new Point[1000];
      7     int size=0;
      8     Random r = new Random();
      9     Point food=new Point();
     10     int dir;
     11     const int Up = 0;
     12     const int Right = 1;
     13     const int Left = 3;
     14     const int Down = 2;
     15     int [,]d={{0,-1},{1,0},{0,1},{-1,0}};
     16     int xsize = 50, ysize = 30;
     17     int per = 20;
     18     public Window()
     19     {
     20         Text = "贪吃蛇--made by weidiao.neu";
     21         a[0].X = 10;
     22         a[0].Y = 10;
     23         dir = 1;
     24         size = 1;
     25         food.X = 40;
     26         food.Y = 25;
     27         ClientSize = new Size(xsize * per, ysize * per);
     28         Paint += draw;
     29         KeyDown += keyDown;
     30         Timer t = new Timer();
     31         t.Interval = 400;
     32         t.Tick += delegate
     33         { 
     34             int x = a[0].X + d[dir,0];
     35             int y = a[0].Y + d[dir,1];
     36             if (x < 0 || y < 0 || x >= xsize || y >= ysize||dead(x,y))
     37             {
     38                 t.Stop();
     39                 MessageBox.Show("你死了");
     40                 Application.Exit();
     41             } 
     42             if (x == food.X && y == food.Y)
     43             {
     44                 size++;
     45                 while (true)
     46                 {
     47                     int fx = r.Next(0, xsize - 1);
     48                     int fy = r.Next(0, ysize - 1);
     49                     int i;
     50                     for ( i = 0; i < size - 1; i++)
     51                     {
     52                         if (fx == a[i].X && fy == a[i].Y) break;
     53                     }
     54                     if (i == size - 1)
     55                     {
     56                         food.X = fx;
     57                         food.Y = fy;
     58                         break;
     59                     }
     60                 }
     61             }
     62             for (int i = size - 1; i > 0; i--)
     63             {
     64                 a[i] = a[i - 1];
     65             }
     66             a[0].X = x;
     67             a[0].Y = y;
     68             Invalidate();
     69         };
     70         t.Start();
     71     }
     72     bool dead(int x, int y)
     73     {
     74         for (int i = 0; i < size; i++)
     75         {
     76             if (a[i].X == x && a[i].Y == y) return true;
     77         }
     78         return false;
     79     }
     80     void keyDown(object o, KeyEventArgs e)
     81     {
     82         switch (e.KeyCode)
     83         {
     84             case Keys.Up: if (dir == Down) return; else dir = Up; break;
     85             case Keys.Down: if (dir == Up) return; else dir = Down; break;
     86             case Keys.Left: if (dir == Right) return; else dir = Left; break;
     87             case Keys.Right: if (dir == Left) return; else dir = Right; break;
     88         }
     89     }
     90     void draw(object o, PaintEventArgs e)
     91     { 
     92         var g = e.Graphics;
     93         g.Clear(Color.Silver);
     94         for (int i = 0; i < size; i++)
     95         {
     96             g.FillRectangle(new SolidBrush(Color.SeaGreen), a[i].X*per,per* a[i].Y, per,per);
     97         }
     98         g.FillEllipse(new SolidBrush(Color.Red), food.X*per, food.Y*per, per,per);
     99     }
    100 }
    101 class Program
    102 {
    103     static void Main()
    104     {
    105         Application.Run(new Window());
    106     }
    107 }

    demo to yanshuang.

    java版

    package aaa;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    class Haha extends JFrame {
        public static void main(String[] args) {
            new Haha();
        }
        Point[] a = new Point[1000];
        int size = 0;
        Random r = new Random();
        Point food = new Point();
        int dir;
        final int Up = 0;
        final int Right = 1;
        final int Left = 3;
        final int Down = 2;
        final int[][] d = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
        final int xsize = 50, ysize = 30;
        final int per = 20;
        BufferedImage bufferedImage = new BufferedImage(per * xsize, per * ysize,
                BufferedImage.TYPE_INT_ARGB);
        Timer t;
    
        public Haha() {
            setTitle("贪吃蛇--made by weidiao.neu");
            a[0] = new Point(10, 10);
            dir = 1;
            size = 1;
            food.x = 40;
            food.y = 25;
            setSize(xsize * per, ysize * per);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            t = new Timer();
            t.schedule(new TimerTask() {
    
                @Override
                public void run() {
                    int x = a[0].x + d[dir][0];
                    int y = a[0].y + d[dir][1];
                    if (x < 0 || y < 0 || x >= xsize || y >= ysize || dead(x, y)) {
                        t.cancel();
                        JOptionPane.showConfirmDialog(Haha.this, "你死了");
                    }
                    if (x == food.x && y == food.y) {
                        size++;
                        while (true) {
                            int fx = r.nextInt(xsize);
                            int fy = r.nextInt(ysize);
                            int i;
                            for (i = 0; i < size - 1; i++) {
                                if (fx == a[i].x && fy == a[i].y)
                                    break;
                            }
                            if (i == size - 1) {
                                food.x = fx;
                                food.y = fy;
                                break;
                            }
                        }
                    }
                    for (int i = size - 1; i > 0; i--) {
                        a[i] = new Point(a[i - 1]);
                    }
                    a[0].x = x;
                    a[0].y = y;
                    repaint();
                }
            }, 0, 400);
            addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    keyDown(e.getKeyCode());
                }
            });
        }
    
        boolean dead(int x, int y) {
            for (int i = 0; i < size; i++) {
                if (a[i].x == x && a[i].y == y)
                    return true;
            }
            return false;
        }
    
        void keyDown(int keyCode) {
            switch (keyCode) {
                case KeyEvent.VK_UP :
                    if (dir == Down)
                        return;
                    else
                        dir = Up;
                    break;
                case KeyEvent.VK_DOWN :
                    if (dir == Up)
                        return;
                    else
                        dir = Down;
                    break;
                case KeyEvent.VK_LEFT :
                    if (dir == Right)
                        return;
                    else
                        dir = Left;
                    break;
                case KeyEvent.VK_RIGHT :
                    if (dir == Left)
                        return;
                    else
                        dir = Right;
                    break;
            }
        }
    
        @Override
        public void paint(Graphics g) {
            Graphics2D gg = bufferedImage.createGraphics();
            gg.setColor(Color.gray);
            gg.fillRect(0, 0, getWidth(), getHeight());
            gg.setColor(Color.green);
            for (int i = 0; i < size; i++) {
                gg.fillRect(a[i].x * per, per * a[i].y, per, per);
            }
            gg.setColor(Color.red);
            gg.fillOval(food.x * per, food.y * per, per, per);
            g.drawImage(bufferedImage, 0, 0, null);
        }
    }
  • 相关阅读:
    Odoo Documentation : Fields
    Odoo models.py BaseModel
    Odoo Documentation : Environment
    Odoo Documentation : Recordsets
    Odoo中使用的部分表名及用途
    Odoo启动过程
    6779. Can you answer these queries VII
    1874 素数和最大
    3150 Pibonacci数
    2817 Tangent的愤怒
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/4936091.html
Copyright © 2020-2023  润新知