JAVA整蛊朋友小游戏之屎王争霸赛


主要运用java初期的面板,面容,面向对象,键盘监听,以及多线程实现动画等的方法。(本人初期小白有语法赘余之处还请谅解,欢迎提出批评指正)

如有问题也可私信问我


需要创建的类:
1.面板:JFrame
2.面容:Panel
3.人物:actor
4.粑粑:bianbian
5.血条:Health
6.游戏结束:gameOver

1.面板类 

package secondGame;

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

/*
需要创建的类:
1.面板:JFrame
2.面容:Panel
3.人物:actor
4.粑粑:bianbian
5.血条:Health
6.游戏结束:gameOver
 */
public class Game extends JFrame {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        gamePanel panel = new gamePanel();
        frame.setTitle("屎王争霸赛");
        frame.setSize(1360,670);
        frame.setLocationRelativeTo(null);
        frame.add(panel);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.requestFocus();//创建焦点,使监听起作用
        Thread thread = new Thread(panel);//实例化线程
        thread.start();//开始线程
    }
}

2.面容类

package secondGame;

import com.softeem.game.util.ImageUtil;

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


public class gamePanel extends JPanel implements Runnable{
    BufferedImage bg = ImageUtil.getPath("/images/背景.png");
    Boolean flag = true;

    public gamePanel () {
            //创建键盘适配器
            KeyAdapter adapter = new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    super.keyPressed(e);
                    //定义code,使其获取键盘按下的键
                    int code = e.getKeyCode();
                    if (code == KeyEvent.VK_UP) {
                        if (actor.y >= 280) {
                            actor.y -= 40;
                        }
                    }
                    if (code == KeyEvent.VK_DOWN) {
                        if (actor.y <= 480) {
                            actor.y += 40;
                        }
                    }
                    if (code == KeyEvent.VK_LEFT) {
                        actor.dir = 0;
                        if (actor.x >= 40) {
                            actor.x -= 40;
                        }
                    }
                    if (code == KeyEvent.VK_RIGHT) {
                        actor.dir = 1;
                        if (actor.x <= 1220) {
                            actor.x += 40;
                        }
                    }
                    if (code == KeyEvent.VK_SPACE) {
                        if (actor.x <= 1180 && actor.dir == 1) {
                            actor.x += 80;
                        }
                    }
                    if (code == KeyEvent.VK_SPACE) {
                        if (actor.x >= 80 && actor.dir == 0) {
                            actor.x -= 80;
                        }
                    }
                    repaint();
                }
            };
            addKeyListener(adapter);//将适配器加入到监听器中

    }
    //实例化相关对象
    Actor actor = new Actor();
    Health health = new Health();
    Baba baba = new Baba();
    Baba1 baba1 = new Baba1();
    Baba2 baba2 = new Baba2();
    Baba3 baba3 = new Baba3();
    Baba4 baba4 = new Baba4();
    gameOver gameOver = new gameOver();
    //paint方法,在面容中将图片画出
    public void paint(Graphics g){
        g.drawImage(bg,0,0,null);
        g.drawImage(actor.img,actor.x,actor.y,actor.w,actor.h,null);
        g.drawImage(health.img, health.x,health.y,health.w,health.h,null);
        g.drawImage(baba.img, baba.x, baba.y, baba.w, baba.h, null);
        g.drawImage(baba1.img, baba1.x, baba1.y, baba1.w, baba1.h, null);
        g.drawImage(baba2.img, baba2.x, baba2.y, baba2.w, baba2.h, null);
        g.drawImage(baba3.img, baba3.x, baba3.y, baba3.w, baba3.h, null);
        g.drawImage(baba4.img, baba4.x, baba4.y, baba4.w, baba4.h, null);
        //总分数
        int Sum;
        //每个粑粑的分数不一样
        Sum = baba.sum + baba1.sum + baba2.sum + baba3.sum + baba4.sum;
        //当结束时根据不同分数绘画出不同等级
        if (health.x < -1360){
            if(Sum < 40){
                g.drawImage(gameOver.img1,gameOver.x1,gameOver.y1,gameOver.w1,gameOver.h1,null);
            }
            if(Sum >= 40 && Sum < 90){
                g.drawImage(gameOver.img2,gameOver.x2,gameOver.y2,gameOver.w2,gameOver.h2,null);
            }
            if(Sum >= 90 && Sum < 150){
                g.drawImage(gameOver.img3,gameOver.x3,gameOver.y3,gameOver.w3,gameOver.h3,null);
            }
            if(Sum >= 150){
                g.drawImage(gameOver.img4,gameOver.x4,gameOver.y4,gameOver.w4,gameOver.h4,null);
            }
        }

    }
    //线程,重写run方法
    public void run(){

        while (flag){
            //控制粑粑下落,不同粑粑下落速度不一样
            baba.y += 3;
            baba1.y += 4;
            baba2.y += 5;
            baba3.y += 6;
            baba4.y += 7;
            //控制血条减少
            health.x -= 2;
            //当粑粑超出界限时重新生成粑粑
            if(baba.y>=560){baba.appear();baba.y = -10;}
            if(baba1.y>=560){baba1.appear();baba1.y = -10;}
            if(baba2.y>=560){baba2.appear();baba2.y = -10;}
            if(baba3.y>=560){baba3.appear();baba3.y = -10;}
            if(baba4.y>=560){baba4.appear();baba4.y = -10;}
            /*
            粑粑图片的高20,长20;
            人物图片的高80,长80;
            1、 判断人物是否吃到粑粑
            2、吃到不同的粑粑会加不同的生命值
             */
            if (baba.y>actor.y-20 && baba.y<actor.y+80){
                if (baba.x>=actor.x-17 && baba.x<=actor.x+77){
                    baba.remove();//移除粑粑
                    health.x += 10;//增加生命
                }
            }
            if (baba1.y>actor.y-20 && baba1.y<actor.y+80){
                if (baba1.x>=actor.x-17 && baba1.x<=actor.x+77){
                    baba1.remove();
                    health.x += 11;
                }
            }
            if (baba2.y>actor.y-20 && baba2.y<actor.y+80){
                if (baba2.x>=actor.x-17 && baba2.x<=actor.x+77){
                    baba2.remove();
                    health.x += 13;
                }
            }
            if (baba3.y>actor.y-20 && baba3.y<actor.y+80){
                if (baba3.x>=actor.x-17 && baba3.x<=actor.x+77){
                    baba3.remove();
                    health.x += 15;
                }
            }
            if (baba4.y>actor.y-20 && baba4.y<actor.y+80){
                if (baba4.x>=actor.x-17 && baba4.x<=actor.x+77){
                    baba4.remove();
                    health.x += 20;
                }
            }
            //当生命值没时,循环结束,即游戏结束
            if (health.x < -1360){
                flag = false;
                }

                try {
                    Thread.sleep(20);//循环每隔20ms循环一次
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            repaint();//重新调用画笔方法,重新画
        }

    }
}

3.人物类

package secondGame;

import com.softeem.game.util.ImageUtil;

import java.awt.image.BufferedImage;

public class Actor {
    BufferedImage img;
    int x,y,w,h;
    int dir;//控制方向:0为左   1为右
    public Actor(){
        img = ImageUtil.getPath("/images/pig.png");
        x = 600;
        y = 520;
        w = img.getWidth();
        h = img.getHeight();
    }
}

4.粑粑类(我定义了四个粑粑类,这里只展示其中一个,因为方法都一样)

package secondGame;

import com.softeem.game.util.ImageUtil;

import java.awt.image.BufferedImage;
import java.util.Random;

public class Baba {
    BufferedImage img;
    Random random = new Random();
    int x,y,w,h;
    int score = 0;
    int sum = 0;

    public Baba(){
        img = ImageUtil.getPath("/images/粑粑.png");
        x = 90;
        y = -10;
        w = img.getWidth();
        h = img.getHeight();
    }
    //appear方法实现粑粑随机生成
    public void appear(){
        x = random.nextInt(1270);
    }
    public void remove(){
        y = -10;
        score++;
        sum += score;
        x = random.nextInt(1270);
    }
}

5.血条类

package secondGame;

import com.softeem.game.util.ImageUtil;

import java.awt.image.BufferedImage;

public class Health {
    BufferedImage img;
    int x,y,w,h;
    public Health(){
        img = ImageUtil.getPath("/images/血量.png");
        x = 0;
        y = 0;
        w = img.getWidth();
        h = img.getHeight();
    }
}

6.游戏结束类

package secondGame;

import com.softeem.game.util.ImageUtil;

import java.awt.image.BufferedImage;

public class gameOver {
    BufferedImage img1,img2,img3,img4;
    int x1,y1,w1,h1;
    int x2,y2,w2,h2;
    int x3,y3,w3,h3;
    int x4,y4,w4,h4;
    public gameOver(){
        img1 = ImageUtil.getPath("/images/gameover1.png");
        img2 = ImageUtil.getPath("/images/gameover2.png");
        img3 = ImageUtil.getPath("/images/gameover3.png");
        img4 = ImageUtil.getPath("/images/gameover4.png");
        x1 = 400;
        y1 = 130;
        x2 = 400;
        y2 = 130;
        x3 = 400;
        y3 = 130;
        x4 = 400;
        y4 = 130;
        w1 = img1.getWidth();
        h1 = img1.getHeight();
        w2 = img2.getWidth();
        h2 = img2.getHeight();
        w3 = img3.getWidth();
        h3 = img3.getHeight();
        w4 = img4.getWidth();
        h4 = img4.getHeight();

    }
}

所用图片以及jar

jar:softeem_game.jar

图片自己画的,有点丑,意会意会即可

背景:

JAVA整蛊朋友小游戏之屎王争霸赛

人物(可以用朋友的照片,嘻嘻)

JAVA整蛊朋友小游戏之屎王争霸赛

粑粑

JAVA整蛊朋友小游戏之屎王争霸赛

结束画面(我做了四个)

JAVA整蛊朋友小游戏之屎王争霸赛JAVA整蛊朋友小游戏之屎王争霸赛JAVA整蛊朋友小游戏之屎王争霸赛JAVA整蛊朋友小游戏之屎王争霸赛

游戏界面:

JAVA整蛊朋友小游戏之屎王争霸赛

上一篇:Java swing双人对战游戏


下一篇:c#-在单独的过程中部署Akka.net参与者