⤥ 奔跑吧!小恐龙 ⤦ 送一份Java小游戏源码
大家好,我是锋哥,最近比较苦逼;
逛互联网,找了个java小游戏-奔跑吧!小恐龙,作者:JarvisHsu 放松下,顺便分享给大家,直接给源码,导入idea直接运行。
⤥ 奔跑吧!小恐龙 ⤦ 游戏源码领取方式:
扫描下方公众号【Java资料站】回复:小恐龙,
可获取下载链接
👇👇👇
👆长按上方二维码 2 秒 回复「小恐龙」即可获取
锋哥秀一下
技术比较渣,玩一会就挂了!
Java Swing实现,以及线程Thread的应用,大家有兴趣的可以研究下源码;
package cn.hellojarvis.view;
import cn.hellojarvis.service.ScoreRecorder;
import cn.hellojarvis.service.Sound;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* Created by IntelliJ IDEA.
*
* @author : JarvisHsu
* @create 2020/08/20 16:45
*/
public class MainFrame extends JFrame {
public MainFrame(){
restart();
setBounds(340,150,1024,720);
setTitle("奔跑吧!小恐龙!");
Sound.background();
ScoreRecorder.init();
addListener();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public void restart(){
Container c = getContentPane();
c.removeAll();
GamePanel gamePanel = new GamePanel();
c.add(gamePanel);
addKeyListener(gamePanel);
c.validate();
}
private void addListener(){
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
ScoreRecorder.saveScore();
}
});
}
}
package cn.hellojarvis.service;
import cn.hellojarvis.view.GamePanel;
import cn.hellojarvis.view.MainFrame;
import cn.hellojarvis.view.ScoreDialog;
import java.awt.*;
/**
* Created by IntelliJ IDEA.
*
* @author : JarvisHsu
* @create 2020/08/20 16:44
*/
public class FreshThread extends Thread {
public static final int FRESH = 20;
public GamePanel gamePanel;
public FreshThread(GamePanel gamePanel) {
this.gamePanel = gamePanel;
}
@Override
public void run() {
while (!gamePanel.isFinish()) {
gamePanel.repaint();
try {
Thread.sleep(FRESH);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Container c = gamePanel.getParent();
while (!(c instanceof MainFrame)) {
c = c.getParent();
}
MainFrame frame = (MainFrame) c;
new ScoreDialog(frame);
frame.restart();
}
}
package cn.hellojarvis.modle;
import cn.hellojarvis.service.FreshThread;
import cn.hellojarvis.service.Sound;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* Created by IntelliJ IDEA.
*
* @author : JarvisHsu
* @create 2020/08/20 16:44
*/
public class Dinosaur {
//主图片
private BufferedImage image;
//跑步图片
private BufferedImage image1, image2, image3;
//坐标
public int x, y;
//跳跃的增变量
private int jumpValue = 0;
//跳跃状态
private boolean jumpState = false;
//踏步计时器
private int stepTimer = 0;
//跳起的最大高度
private final int JUMP_HEIGHT = 120;
//落地最低坐标
private final int LOWEST_Y = 415;
//刷新时间
private final int FRESH = FreshThread.FRESH;
public Dinosaur() {
this.x = 50;
this.y = LOWEST_Y;
try {
this.image1 = ImageIO.read(new File("image/dinosaur1.png"));
this.image2 = ImageIO.read(new File("image/dinosaur2.png"));
this.image3 = ImageIO.read(new File("image/dinosaur3.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
//踏步
private void step() {
//每过250ms,更换一张图片。
int temp = (this.stepTimer / 150) % 2;
if (jumpState){
image = image3;
}else {
if (temp == 1) {
image = image1;
} else {
image = image2;
}
}
//计时器递增
this.stepTimer += FRESH;
}
//跳跃
public void jump() {
if (!this.jumpState) {
Sound.jump();
}
this.jumpState = true;
}
//移动
public void move() {
this.step();
if (this.jumpState) {
if (y >= this.LOWEST_Y) {
this.jumpValue = -5;
}
if (y <= this.LOWEST_Y - this.JUMP_HEIGHT) {
jumpValue = 5;
}
y += jumpValue;
if (y >= this.LOWEST_Y) {
jumpState = false;
}
}
}
public Rectangle getFootBounds() {
return new Rectangle(x + 25, y + 100, 45, 44);
}
public Rectangle getHeadBounds() {
return new Rectangle(x + 55, y + 50, 50, 25);
}
@Override
public String toString() {
return "Dinosaur{" +
"image=" + image +
", image1=" + image1 +
", image2=" + image2 +
", image3=" + image3 +
", x=" + x +
", y=" + y +
", jumpValue=" + jumpValue +
", jumpState=" + jumpState +
", stepTimer=" + stepTimer +
", JUMP_HEIGHT=" + JUMP_HEIGHT +
", LOWEST_Y=" + LOWEST_Y +
", FRESH=" + FRESH +
'}';
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public BufferedImage getImage1() {
return image1;
}
public void setImage1(BufferedImage image1) {
this.image1 = image1;
}
public BufferedImage getImage2() {
return image2;
}
public void setImage2(BufferedImage image2) {
this.image2 = image2;
}
public BufferedImage getImage3() {
return image3;
}
public void setImage3(BufferedImage image3) {
this.image3 = image3;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getJumpValue() {
return jumpValue;
}
public void setJumpValue(int jumpValue) {
this.jumpValue = jumpValue;
}
public boolean isJumpState() {
return jumpState;
}
public void setJumpState(boolean jumpState) {
this.jumpState = jumpState;
}
public int getStepTimer() {
return stepTimer;
}
public void setStepTimer(int stepTimer) {
this.stepTimer = stepTimer;
}
public int getJUMP_HEIGHT() {
return JUMP_HEIGHT;
}
public int getLOWEST_Y() {
return LOWEST_Y;
}
public int getFRESH() {
return FRESH;
}
}
package cn.hellojarvis.modle;
import cn.hellojarvis.view.BackgroundImage;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
/**
* Created by IntelliJ IDEA.
*
* @author : JarvisHsu
* @create 2020/08/20 16:44
*/
public class Obstacle {
private int x,y;
private BufferedImage image;
private BufferedImage stone;
private BufferedImage cacti;
private int speed;
public Obstacle(){
try {
this.stone = ImageIO.read(new File("image/rock.png"));
this.cacti = ImageIO.read(new File("image/cacti.png"));
} catch (IOException e) {
e.printStackTrace();
}
Random random = new Random();
if (random.nextInt(2)==0){
this.image = this.cacti;
}else{
this.image = this.stone;
}
this.x = 1024;
this.y = 560 - image.getHeight();
this.speed = BackgroundImage.SPEED;
}
public void move(){
x-=this.speed;
}
public boolean isLive(){
return x > -image.getWidth();
}
public Rectangle getBounds(){
if (image==cacti){
return new Rectangle(x,y,24,image.getHeight()-1);
}
return new Rectangle(x+5,y,32,28);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public BufferedImage getStone() {
return stone;
}
public void setStone(BufferedImage stone) {
this.stone = stone;
}
public BufferedImage getCacti() {
return cacti;
}
public void setCacti(BufferedImage cacti) {
this.cacti = cacti;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
@Override
public String toString() {
return "Obstacle{" +
"x=" + x +
", y=" + y +
", image=" + image +
", stone=" + stone +
", cacti=" + cacti +
", speed=" + speed +
'}';
}
}
⤥ 奔跑吧!小恐龙 ⤦ 游戏源码领取方式:
扫描下方公众号【Java资料站】回复:小恐龙,
可获取下载链接
👇👇👇
👆长按上方二维码 2 秒 回复「小恐龙」即可获取
评论