用java制作小游戏:小恐龙跑酷

插入链接与图片

运行图片:( 第一次做游戏,画风比较丑哈!最左边的就是主角小恐龙了,中间的是障碍物仙人掌,右边的小狗也是障碍物,右上角的是分数条)

简单游戏java程序代码 java小游戏代码_简单游戏java程序代码

简单游戏java程序代码 java小游戏代码_Image_02

图片:

简单游戏java程序代码 java小游戏代码_游戏_03


以下是Java工程中的每一个类的代码:

**

MainFrame.java类:

**

import java.awt.Container;
import javax.swing.JFrame;

public class MainFrame extends JFrame {
GamePanel p;

public MainFrame() {
	setDefaultCloseOperation(EXIT_ON_CLOSE);//设置退出方式
	p = new GamePanel();
	Container c = getContentPane();
	c.add(p);
	
	addKeyListener(p);
}

public static void main(String[] args) {
	MainFrame frame = new MainFrame();
	frame.setBounds(500, 235, 1020, 780);//设置窗体的大小和位置
	frame.setVisible(true);
}

public void restart() {
	Container c=getContentPane();
	c.removeAll();
	
	GamePanel np=new GamePanel();
	c.add(np);
	addKeyListener(np);
	
	c.validate();
}

}

**

GamePanel.java类:

**

import java.awt.Color;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.Rectangle;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;import javax.imageio.ImageIO;
 import javax.swing.JPanel;public class GamePanel extends JPanel implements KeyListener {
 BufferedImage image;
 Graphics2D g2;
 Dinosaur golden;
boolean finish = false;

static final int FRESH = 15;

BackgroundImage background;

List<Obstacle> list = new ArrayList<Obstacle>();

int addObstacleTimer = 0;

int score=0;
int addScoreTimer=0;

public GamePanel() {
	image = new BufferedImage(1000, 735, BufferedImage.TYPE_INT_BGR);
	g2 = image.createGraphics();
	golden = new Dinosaur();
	background = new BackgroundImage();

	list.add(new Obstacle());
	FreshThread t = new FreshThread(this);
	t.start();
}

private void painImage() {
	golden.move();
	background.roll();

	g2.drawImage(background.image, 0, 0, this);
	
	//Rectangle rt=golden.getfeetBounds();
	//g2.setColor(Color.black);
	//g2.fillRect(rt.x, rt.y, rt.width, rt.height);
	
	g2.drawImage(golden.image, golden.x, golden.y, this);

	if (addObstacleTimer >= 2000) {
		Random r=new Random();
		int a=r.nextInt(100);//创建一个100以内的随机数
		if(a>60) {//有百分之六十的概率出现新障碍
			list.add(new Obstacle());
		}
		addObstacleTimer = 0;
	}

	for (int i = 0; i < list.size(); i++) {
		Obstacle o = list.get(i);
		o.move();
		//rt=o.getBounds();
		//g2.fillRect(rt.x, rt.y, rt.width, rt.height);
		g2.drawImage(o.image, o.x, o.y, this);
		
		if(o.getBounds().intersects(golden.getfeetBounds())) {
			gameOver();
		}
	}
	
	if(addScoreTimer>=500) {
		score+=10;
		addScoreTimer=0;
	}
	g2.setColor(Color.black);
	g2.setFont(new Font("黑体", Font.BOLD, 24));//设置分数字体为黑体加粗,24号大小
	g2.drawString(String.format("%05d", score), 800, 20);//给分数设置格式
	
	addObstacleTimer+=FRESH;
	addScoreTimer+=FRESH;

}

public void gameOver() {
	finish=true;
}

@Override
public void paint(Graphics g) {
	painImage();
	g.drawImage(image, 0, 0, this);
}

public boolean isFinish() {
	return finish;
}

@Override
public void keyPressed(KeyEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent arg0) {
	int code = arg0.getKeyCode();
	if (code == KeyEvent.VK_SPACE) {
		golden.jump();
	}

}

@Override
public void keyTyped(KeyEvent arg0) {
	// TODO Auto-generated method stub

}

}

**

## Dinosaur.java类:

**


import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Dinosaur {
	int x, y;
	BufferedImage image;
	BufferedImage image1, image2, image3;
	int stepTime = 0;
	int fresh = GamePanel.FRESH;

	boolean jumpState = false;//初始化恐龙的跳跃状态

	int jumpHeight = 370;//跳跃高度
	final int LOWEST_Y = 620;//恐龙站在地面的的y坐标
	
	int jumpValue=0;

	public Dinosaur() {
		try {
			image1 = ImageIO.read(new File("src/小恐龙1.jpg"));//读取图片
			image2 = ImageIO.read(new File("src/小恐龙2.jpg"));
			image1 = ImageIO.read(new File("src/小恐龙1.jpg"));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		x = 5;
		y = LOWEST_Y;
	}

	public void move() {
		step();
		if(jumpState) {
			if(y>=LOWEST_Y) {
				jumpValue=-10;
			}
			if(y<=LOWEST_Y-jumpHeight) {
				jumpValue=10;
			}
			y+=jumpValue;
			if(y>=LOWEST_Y) {
				jumpState=false;
			}
		}
	

	}

	void step() {
		int tmp = stepTime / 150 % 3;//恐龙摆臂快慢
		switch (tmp) {
		case 0:
			image = image1;
			break;
		case 1:
			image = image2;
			break;
		default:
			image = image1;
			break;
		}

		stepTime += fresh;
	}

	public void jump() {
		jumpState = true;
	}
	
	public Rectangle getfeetBounds() {
		return new Rectangle(x+40, y+90, 85, 27);
	}
	

}



**

## FreshThread.java类:

**


import java.awt.Container;

import javax.swing.JOptionPane;

public class FreshThread extends Thread {
	GamePanel p;
	
	public FreshThread(GamePanel p) {
		this.p=p;
	}

	@Override
	public void run() {
		while(!p.isFinish()) {//判断是否重启游戏
			p.repaint();
			try {
				Thread.sleep(p.FRESH);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		Container c=p.getParent();
		while(!(c instanceof MainFrame)) {
			c=c.getParent();
		}
		MainFrame f=(MainFrame) c;
		
		JOptionPane.showMessageDialog(f, "游戏结束"	);
		f.restart();
	}

}



**

## BackgroundImage.java类:

**

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class BackgroundImage {
	BufferedImage image;
	BufferedImage image1, image2;
	Graphics2D g2;
	int x1, x2;
	static final int SPEED = 4;

	public BackgroundImage() {
		try {
			image1 = ImageIO.read(new File("src/background.jpg"));
			image2 = ImageIO.read(new File("src/background.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		image = new BufferedImage(1000, 735, BufferedImage.TYPE_INT_BGR);
		g2 = image.createGraphics();
		x1 = 0;
		x2 = 1000;
		g2.drawImage(image1, x1, 0, null);
		g2.drawImage(image2, x2, 0, null);
	}

	public void roll() {
		x1 -= SPEED;
		x2 -= SPEED;
		if(x1<=-1000) {
			x1=1000;
		}
		if(x2<=-1000) {
			x2=1000;
		}
		g2.drawImage(image1, x1, 0, null);
		g2.drawImage(image2, x2, 0, null);
	}

}



**

## Obstacle.java类

**

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class Obstacle {
	int x,y;
	BufferedImage image;	
	BufferedImage dog,cacti;
	int speed;
	
	public Obstacle() {
		try {
			dog=ImageIO.read(new File("src/b43538a4a9ba48ef562ac71bb0350bc3.gif"));
			cacti=ImageIO.read(new File("src/仙人掌_看图王.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Random r=new Random();
		int temp=r.nextInt(2);//创建一个随机数,随机出现一个障碍物
		if(temp==0) {
			image=dog;
		}else {
			image=cacti;
		}
		speed=BackgroundImage.SPEED;//背景滚动的速度
		x=1000;
		y=620;
		
		
	}
	
	public void move() {
		x-=speed;
	}
	
	public Rectangle getBounds() {
		if(image==cacti) {
			return new Rectangle(x, y,image.getWidth(), image.getHeight());
		}else {
			return new Rectangle(x, y,image.getWidth()-17, image.getHeight());
		}

	}
	



图片素材:![在这里插入图片描述]()
![在这里插入图片描述]()
![在这里插入图片描述]()
![在这里插入图片描述]()