网络小游戏

07-16 1573阅读

一、项目简介

功能描述:射击类小游戏,控制玩家击杀敌人获得分数,参考别人代码

参考git地址或博客地址:GitHub - Hpasserby/ZombieCrisis: JAVA 射击生存类小游戏

个人负责任务: 设计游戏界面和结束画面,设计玩家(生命值、武器等)和敌人角色(生命值、攻击方式等)

团队博客链接:java课设-CSDN博客

二、功能架构图

网络小游戏

三、个人任务简述

设计游戏界面和结束画面,设计玩家(生命值、武器等)和敌人角色(生命值、攻击方式等)

四、部分代码

package Game;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
public class GameClient extends Frame {
    public static final int WORLD_WIDTH = 960;
    public static final int WORLD_HEIGHT = 720;
    private Image offScreenImage;
    private World world;
    public GameClient(boolean Doubleplayer) {
        this.world = new World(960, 720, Doubleplayer);
        this.offScreenImage = null;
    }
    public void paint(Graphics g) {
        if (!this.world.End()) {
            this.world.drawWorld(g);
        } else {
            this.world.drawEnd(g);
        }
    }
    public void update(Graphics g) {
        if (this.offScreenImage == null) {
            this.offScreenImage = this.createImage(960, 720);
        }
        Graphics gOffScreen = this.offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.lightGray);
        gOffScreen.fillRect(0, 0, 960, 720);
        gOffScreen.setColor(c);
        this.paint(gOffScreen);
        g.drawImage(this.offScreenImage, 0, 0, (ImageObserver)null);
    }
    public void lauchFrame() {
        this.setLocation(400, 100);
        this.setSize(960, 720);
        this.setTitle("ZombieCrisis");
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        this.setResizable(false);
        this.setBackground(Color.lightGray);
        this.addKeyListener(new KeyMonitor((Hero)this.world.getObject(0)));
        if (this.world.getObject(1) instanceof Hero) {
            this.addKeyListener(new KeyMonitor((Hero)this.world.getObject(1)));
        }
        this.setVisible(true);
        (new Thread(new PaintThread())).start();
    }
    private class KeyMonitor extends KeyAdapter {
        Hero hero;
        public KeyMonitor(Hero hero) {
            this.hero = hero;
        }
        public void keyReleased(KeyEvent e) {
            this.hero.keyReleased(e);
        }
        public void keyPressed(KeyEvent e) {
            this.hero.KeyPressed(e);
        }
    }
    private class PaintThread implements Runnable {
        private PaintThread() {
        }
        public void run() {
            while(true) {
                GameClient.this.repaint();
                try {
                    Thread.sleep(30L);
                } catch (InterruptedException var2) {
                    var2.printStackTrace();
                }
            }
        }
    }
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package Game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.util.Random;
public class Blood extends GameObject {
    private int picX;
    private int picY;
    public Blood(int x, int y, World world) {
        super("Blood", 0, 0, 99999, x, y, false, world);
        Random rand = new Random();
        int picX = Math.abs(rand.nextInt()) % 2;
        int picY = Math.abs(rand.nextInt()) % 2;
        this.picX = picX * 475;
        this.picY = picY * 475;
    }
    public void draw(Graphics g) {
        g.drawImage((Image)imgMap.get(this.name), this.x - 30, this.y, this.x + 30, this.y + 45, this.picX, this.picY, this.picX + 475, this.picY + 475, (ImageObserver)null);
    }
    public void collisionResponse(GameObject object) {
    }
    public void onAttack(Weapon weapon) {
    }
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package Game;
import java.awt.Graphics;
import java.util.Iterator;
import java.util.List;
public class Enemy extends Role {
    private Pathfinder pathfinder;
    private Role target;
    private List path;
    private int refreshPath;
    private int collisionDelay;
    public Enemy(String name, int HP, int radius, int speed, int x, int y, World world) {
        super(name, HP, radius, speed, x, y, world);
        this.pathfinder = new Pathfinder(new WorldGrids(world), this);
        this.target = this.getTarget();
        this.refreshPath = 0;
        this.collisionDelay = 0;
    }
    public Role getTarget() {
        Iterator tmp = this.world.getObjectsIterator();
        Role target = null;
        double minDistance = 1.0E7;
        while(tmp.hasNext()) {
            GameObject obj = (GameObject)tmp.next();
            if (obj instanceof Hero && obj.getHP() > 0) {
                double distance = Math.pow((double)(this.x - obj.getX()), 2.0) + Math.pow((double)(this.y - obj.getY()), 2.0);
                target = minDistance  0) {
            return Direction.LD;
        } else if (deltaX == 0 && deltaY > 0) {
            return Direction.D;
        } else if (deltaX > 0 && deltaY > 0) {
            return Direction.RD;
        } else if (deltaX > 0 && deltaY == 0) {
            return Direction.R;
        } else {
            return deltaX > 0 && deltaY = 0.0 && angle = 337.5 && angle = 22.5 && angle = 67.5 && angle = 112.5 && angle = 157.5 && angle = 202.5 && angle = 247.5 && angle = 292.5 && angle  0) {
                    Grid nextGrid = (Grid)this.path.get(0);
                    this.oldDir = this.dir == Direction.STOP ? this.oldDir : this.dir;
                    this.dir = this.getNextDir(nextGrid);
                }
                if (this.getDir() == Direction.STOP) {
                    this.oldDir = this.judgeAccurateDir(this.target.getX(), this.target.getY()) == Direction.STOP ? this.oldDir : this.judgeAccurateDir(this.target.getX(), this.target.getY());
                }
            } else {
                this.dir = Direction.STOP;
            }
        }
    }
    public void draw(Graphics g) {
        this.locateDirection();
        super.draw(g);
    }
    public void collisionResponse(GameObject object) {
        this.dir = Direction.STOP;
        if (this.maintainCollisionDelay(3) 
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]