GUI考古小项目代码记录:贪吃猪

本文详细介绍了一款基于Java Swing的贪吃猪游戏开发过程,包括游戏面板设计、图像资源加载、键盘监听处理及计分板实现等核心功能。通过具体代码示例,展示了如何创建游戏面板、初始化游戏状态、绘制游戏元素以及实现蛇的移动和食物生成逻辑。
摘要由CSDN通过智能技术生成

三个class:Data(图片数据)/GamePanel(游戏面板)/StartGame(开始)
1.Data

package com.zpf.greedysnakedemo;

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

public class Data {
    public static URL pigheadurl=Data.class.getResource("statics/pighead.jpg");
    public static ImageIcon pighead=new ImageIcon(pigheadurl);

    public static URL headerurl=Data.class.getResource("statics/header.jpg");
    public static ImageIcon header=new ImageIcon(headerurl);

    public static URL foodurl=Data.class.getResource("statics/food.jpg");
    public static ImageIcon food=new ImageIcon(foodurl);
}

2.GamePanel

package com.zpf.greedysnakedemo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GamePanel extends JPanel {
    int length;
    int[] snakeX=new int[800];
    int[] snakeY=new int[800];
    boolean isStart;
    boolean gameOver;
    int[] foodXY=new int[2];
    int direct;//0 右边 1 左边 2 上边 3 下边
    Timer timer=new Timer(100,new MyActionListener());
    public GamePanel(){
        init();
    }

    public void init(){
        length=3;
        snakeX[0]=100;snakeY[0]=100;
        snakeX[1]=75;snakeY[1]=100;
        snakeX[2]=50;snakeY[2]=100;
        direct=0;
        isStart=false;
        gameOver=false;
        this.setFocusable(true);
        this.addKeyListener(new MyKeyListener());
        setFoodXY();
        timer.start();
    }
    public void reset(){
        length=3;
        snakeX[0]=100;snakeY[0]=100;
        snakeX[1]=75;snakeY[1]=100;
        snakeX[2]=50;snakeY[2]=100;
        direct=0;
        isStart=false;
        gameOver=false;
       // this.setFocusable(true);
       // this.addKeyListener(new MyKeyListener());
        setFoodXY();
        //timer.start();
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.WHITE);
        Data.header.paintIcon(this,g,25,11);
        g.setFont(new Font("微软雅黑",Font.BOLD,20));
        g.drawString("分数:"+(length*5-15),700,20);
        g.drawString("长度:"+length,700,50);
        g.setColor(Color.WHITE);
        g.fillRect(25,75,850,600);
        Data.food.paintIcon(this,g,foodXY[0],foodXY[1]);
        g.setColor(Color.BLACK);
        for(int i=0;i<length;i++) {
            Data.pighead.paintIcon(this, g, snakeX[i], snakeY[i]);
        }
        if(isStart==false&&gameOver==false){
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏",200,200);
        }
        if(gameOver==true){
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("游戏结束了,按空格键重新开始",200,200);
        }
    }
    protected void setFoodXY(){
        foodXY[0]=(int)(Math.random()*825+25);
        foodXY[1]=(int)(Math.random()*500+100);
        foodXY[0]=foodXY[0]-foodXY[0]%25;
        foodXY[1]=foodXY[1]-foodXY[1]%25;
    }
    protected void gameOver(){
        gameOver=true;
        isStart=false;
    }
    private  class MyKeyListener extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode=e.getKeyCode();
            if(keyCode==KeyEvent.VK_SPACE){
                isStart=!isStart;
                repaint();
            }
            if(keyCode==KeyEvent.VK_SPACE&&gameOver==true){
                reset();
                gameOver=false;
                isStart=!isStart;
                repaint();
            }
            else {
                switch (keyCode) {
                    case KeyEvent.VK_RIGHT:
                        direct=0;
                        break;
                    case KeyEvent.VK_LEFT:
                        direct=1;
                        break;
                    case KeyEvent.VK_UP:
                        direct=2;
                        break;
                    case KeyEvent.VK_DOWN:
                        direct=3;
                        break;
                }
            }
        }
    }
    private class MyActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            if(isStart==true){
                for (int i = length - 1; i > 0; i--) {
                    snakeX[i] = snakeX[i - 1];
                    snakeY[i] = snakeY[i - 1];
                }
                //右移
                if(direct==0) {
                    snakeX[0] = snakeX[0] + 25;
                }
                //左移
                else if(direct==1){
                    snakeX[0] = snakeX[0] -25;
                }
                //上移
                else if(direct==2){
                    snakeY[0] = snakeY[0] -25;
                }
                //下移
                else if(direct==3){
                    snakeY[0] = snakeY[0] +25;
                }
                if(snakeX[0]>=850)
                    snakeX[0]=25;
                if(snakeX[0]<25)
                    snakeX[0]=850;
                if(snakeY[0]>=600)
                    snakeY[0]=75;
                if(snakeY[0]<75)
                    snakeY[0]=600;
                for(int i=0;i<length;i++){
                if (snakeX[i]==foodXY[0]&&snakeY[i]==foodXY[1]) {
                    length++;
                    setFoodXY();
                    snakeX[length-1]=snakeX[length-2];
                    snakeY[length-1]=snakeY[length-2];
                }

                }
                for(int i=1;i<length;i++){
                    if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                        gameOver();
                        break;
                    }
                }

                repaint();
            }

        }
    }
}

3.StartGame

package com.zpf.greedysnakedemo;

import javax.swing.*;

public class StartGame {
    public static void main(String[] args) {
        JFrame jFrame=new JFrame("贪吃猪");
        jFrame.add(new GamePanel());
        jFrame.setVisible(true);
        jFrame.setResizable(false);
        jFrame.setBounds(10,10,900,720);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值