24
2020
12

贪吃蛇---Java

StartGames.java文件

package com.xiao.snake;

import javax.swing.*;


public class StartGames {
    public static void main(String[] args) {
        //绘制一个静态窗口 JFrame
        JFrame frame = new JFrame();
        frame.setBounds(10,10,900,720);//设置界面的大小
        frame.setResizable(false);//窗口大小则不可以改变
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭事件,游戏可以关闭了
        //2.面板JPanel 可以加入到JFrame
        frame.add(new GamePanel());
        frame.setVisible(true);//让窗口能够展现出来
    }
}

Date.java文件

package com.xiao.snake;

import javax.swing.*;
import java.net.URL;

    //存放外部数据
public class Date {
    //头部的图片 URL:定位图片的地址   ImageIcon:图片
   public  static URL headerURL=Date.class.getResource("/com/xiao/statics/header.png");
   public  static ImageIcon header=new ImageIcon(headerURL);
   //蛇的身体和各个方向的头部
   public static URL bodyURL=Date.class.getResource("/com/xiao/statics/body.png");
   public static URL downURL=Date.class.getResource("/com/xiao/statics/down.png");
   public static URL leftURL=Date.class.getResource("/com/xiao/statics/left.png");
   public static URL rightURL=Date.class.getResource("/com/xiao/statics/right.png");
   public static URL upURL=Date.class.getResource("/com/xiao/statics/up.png");
   public static ImageIcon body=new ImageIcon(bodyURL);
   public static ImageIcon down=new ImageIcon(downURL);
   public static ImageIcon left=new ImageIcon(leftURL);
   public static ImageIcon right=new ImageIcon(rightURL);
   public static ImageIcon up=new ImageIcon(upURL);
   //蛇的食物
   public static URL foodURL=Date.class.getResource("/com/xiao/statics/food.png");
   public static ImageIcon food=new ImageIcon(foodURL);

    }

GamePanel.java文件

com.xiao.snakejavax.swing.*java.awt.*java.awt.event.ActionEventjava.awt.event.ActionListenerjava.awt.event.KeyEventjava.awt.event.KeyListenerjava.util.RandomGamePanel JPanel KeyListenerActionListener {
    [] =[][] =[]String =Timer =Timer()Random =Random()=() {
        init().setFocusable().addKeyListener().start()}

    (){
       =[]=[]=[]=[]=[]=[]===+*.nextInt()=+*.nextInt()= }


    (Graphics g) {
        .paintComponent(g).setBackground(Color.)g.fillRect()(.equals()){
        Date..paintIcon(g[][])}(.equals()){
        Date..paintIcon(g[][])}(.equals()){
        Date..paintIcon(g[][])}(.equals()){
        Date..paintIcon(g[][])}


        (i = i < i++) {
            Date..paintIcon(g[i][i])}
        (==){
            g.setColor(Color.)g.setFont(Font(Font.))g.drawString()}
        Date..paintIcon(g)g.setColor(Color.)g.setFont(Font(Font.))g.drawString(+ )g.drawString(+ )(){
            g.setColor(Color.)g.setFont(Font(Font.))g.drawString()}
    }
(KeyEvent e) {
    keyCode = e.getKeyCode()(keyCode==KeyEvent.){
            (){
                = init()}{
                = !}
            repaint()}
        (keyCode==KeyEvent.){
            =}(keyCode==KeyEvent.){
            =}(keyCode==KeyEvent.){
            =}(keyCode==KeyEvent.){
            =}
    }

    (ActionEvent e) {
        (){
            (i = -i >i--) {[i]=[i-][i]=[i-]}
            (.equals()){
                []=[]+([]>){
                    []=}
            }(.equals()){
                []=[]-([]<) []=}(.equals()){
                []-=([]<) []=}(.equals()){
                []+=([]>) []=}

([]==&&[]==){
    ++= + =+*.nextInt()=+*.nextInt()}
(i = i < i++) {
                ([i]==[] && [i]==[] ){
                    = }
            }
            repaint()}

    }

    (KeyEvent e) {
   }

    (KeyEvent e) {
        }


}

跟着狂神一起敲的


gzh

微信扫码关注

更新实时通知

« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。