游戏简介:这是一个躲避上方落下障碍物的游戏,碰到上方障碍物就会减少一点生命值,3次碰到障碍物游戏结束。
游戏玩法:由 ↑,↓,←,→键来控制方向,空格键表示暂停。
完整代码部分
package demo01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Runbox006 extends JFrame implements KeyListener, ActionListener, MouseListener {
// 定义一个内部类用来创建坐标
class Point{
public int x;
public int y;
public Point (int x,int y){
this.x=x;
this.y=y;
}
}
// 行数(高)
int height=18;
// 列数(宽)
int width=15;
// 格子的大小
int size=40;
// 标题栏的高度
int title=100;
// 分数
int fenshu=0;
// 状态 0表示正常 1表示暂停 2表示游戏结束
int zhuangtai=0;
// 设置小球的生命值
int life=2;
// 设置墙
int wall1[]= newwall();
int wall2[]= newwall();
int wall3[]= newwall();
// 单个的钉子墙
int wall4[]= xiaowall();
int wall5[]= xiaowall();
int wall6[]= xiaowall();
// 初始墙的点坐标
Point point1=new Point(0,1);
Point point2=new Point(0,-6);
Point point3=new Point(0,-13);
Point point4=new Point(0,-2);
Point point5=new Point(0,-9);
Point point6=new Point(0,-16);
// 设置方块的初始位置
Point block=new Point(width/2,height-2);
// 设置图片
private Image image=null;
Graphics gf=null;
// 设置计时器
Timer timer=new Timer(500,this);
public void Tian(){
//设置窗口的宽高
this.setSize(width*size ,height*size+title);
// 设置标题
this.setTitle("躲避障碍");
//设置窗口能看见
this.setVisible(true);
// 设置窗口顶点坐标的位置
this.setLocation(100,100);
//定时器的开关
timer.start();
//设置键盘监听事件(也就是当键盘有什么反应都会调用此函数)
this.addKeyListener(this);
}
public void paint(Graphics g){
//初始化临时图片//解决闪烁问题
if(image==null){
image=this.createImage(width*size,height*size+title);
}
if(this.gf ==null){
this.gf =image.getGraphics();
}
// 擦除背景
gf.setColor(Color.white);
gf.fill3DRect(0,0,width*size,title+height*size,true);
画块
if(life==2){
gf.setColor(Color.green);
gf.fillOval(block.x*size,block.y*size+title,size,size);
}else if(life==1){
gf.setColor(Color.yellow);
gf.fillOval(block.x*size,block.y*size+title,size,size);
}else if(life==0){
gf.setColor(Color.red);
gf.fillOval(block.x*size,block.y*size+title,size,size);
}
// 画钉子墙
for (int i = 0; i < wall1.length; i++) {
if(wall1[i]==1){
gf.setColor(Color.pink);
Point point=new Point((point1.x+i)*size,(point1.y)*size+title);
int x[]={point.x,point.x+size/2,point.x+size};
int y[]={point.y,point.y+size,point.y};
gf.fillPolygon(x,y,3);
}
}
if(point2.y>=0){
for (int i = 0; i < wall2.length; i++) {
if(wall2[i]==1){
gf.setColor(Color.pink);
Point point=new Point((point2.x+i)*size,(point2.y)*size+title);
int x[]={point.x,point.x+size/2,point.x+size};
int y[]={point.y,point.y+size,point.y};
gf.fillPolygon(x,y,3);
}
}
}
if (point3.y>=0){
for (int i = 0; i < wall3.length; i++) {
if(wall3[i]==1){
gf.setColor(Color.pink);
Point point=new Point((point3.x+i)*size,(point3.y)*size+title);
int x[]={point.x,point.x+size/2,point.x+size};
int y[]={point.y,point.y+size,point.y};
gf.fillPolygon(x,y,3);
}
}
}
if (point4.y>=0){
for (int i = 0; i < wall4.length; i++) {
if(wall4[i]==1){
gf.setColor(Color.pink);
Point point=new Point((point4.x+i)*size,(point4.y)*size+title);
int x[]={point.x,point.x+size/2,point.x+size};
int y[]={point.y,point.y+size,point.y};
gf.fillPolygon(x,y,3);
}
}
}
if (point5.y>=0){
for (int i = 0; i < wall5.length; i++) {
if(wall5[i]==1){
gf.setColor(Color.pink);
Point point=new Point((point5.x+i)*size,(point5.y)*size+title);
int x[]={point.x,point.x+size/2,point.x+size};
int y[]={point.y,point.y+size,point.y};
gf.fillPolygon(x,y,3);
}
}
}
if (point6.y>=0){
for (int i = 0; i < wall6.length; i++) {
if(wall6[i]==1){
gf.setColor(Color.pink);
Point point=new Point((point6.x+i)*size,(point6.y)*size+title);
int x[]={point.x,point.x+size/2,point.x+size};
int y[]={point.y,point.y+size,point.y};
gf.fillPolygon(x,y,3);
}
}
}
if(zhuangtai==0||zhuangtai==1){
gf.setColor(Color.darkGray);
gf.fill3DRect(0,0,width*size,title,true);
gf.setFont(new Font("华文新魏", 10, 55)); //设置字体
gf.setColor(Color.white);
gf.drawString("分数:"+fenshu+"分",width*size/2-150,title/2+40);
if(life==2){
gf.setFont(new Font("华文新魏", 10, 40)); //设置字体
gf.setColor(Color.green);
gf.drawString(" ★★★",0,title/2+40);
} else if(life==1){
gf.setFont(new Font("华文新魏", 10, 40)); //设置字体
gf.setColor(Color.yellow);
gf.drawString(" ★★",0,title/2+40);
}else if(life==0){
gf.setFont(new Font("华文新魏", 10, 40)); //设置字体
gf.setColor(Color.red);
gf.drawString(" ★",0,title/2+40);
}
}else if(zhuangtai==2){
gf.setColor(Color.darkGray);
gf.fill3DRect(0,0,width*size,title,true);
gf.setFont(new Font("华文新魏", 10, 55)); //设置字体
gf.setColor(Color.white);
gf.drawString("最终分数:"+fenshu+"分",width*size/2-200,title/2+40);
}
g.drawImage(image,0,0,null);
}
//处理定时器
public void actionPerformed(ActionEvent e) {
// 钉子墙下落
if(point1.y<=height){
point1.y+=1;
}else if(point1.y>height){
// 钉子墙位置重置
wall1=newwall();
point1.y=0;
// 分数+1
fenshu++;
}
if(point2.y<=height){
point2.y+=1;
}else if(point2.y>height){
point2.y=0;
wall2=newwall();
fenshu++;
}
if(point3.y<=height){
point3.y+=1;
}else if(point3.y>height){
point3.y=0;
wall3=newwall();
fenshu++;
}
if(point4.y<=height){
point4.y+=1;
}else if(point4.y>height){
point4.y=0;
wall4=xiaowall();
fenshu++;
}
if(point5.y<=height){
point5.y+=1;
}else if(point5.y>height){
point5.y=0;
wall5=xiaowall();
fenshu++;
}
if(point6.y<=height){
point6.y+=1;
}else if(point6.y>height){
point6.y=0;
wall6=xiaowall();
fenshu++;
}
// 检测是否撞上钉子墙
if(ispengzhuang()){
timer.stop();
zhuangtai=2;
}
repaint();
}
public void keyTyped(KeyEvent e) {
}
// 按下键盘--控制游戏的开始以及方块的移动方向
public void keyPressed(KeyEvent e) {
// 获取从键盘输入的键
int key = e.getKeyCode();
if(key == KeyEvent.VK_SPACE) {//空格键
// 0表示正常 1表示暂停 2表示游戏结束
if (zhuangtai == 0) {
// 从正常状态变为暂停状态
zhuangtai = 1;
timer.stop();
} else if (zhuangtai == 1) {
// 从暂停状态变为正常状态
zhuangtai = 0;
timer.start();
} else if(zhuangtai==2){
// 从结束状态变为正常状态
zhuangtai=0;
timer.start();
fenshu=0;
life=2;
// 初始墙的点坐标
point1=new Point(0,0);
point2=new Point(0,-6);
point3=new Point(0,-13);
point4=new Point(0,-2);
point5=new Point(0,-9);
point6=new Point(0,-16);
// 初始块坐标
block=new Point(width/2,height-2);
}
}
if(zhuangtai==0){
// 上
if (key == KeyEvent.VK_UP&&block.y>0) {
block.y--;
if(ispengzhuang()){
zhuangtai=2;
timer.stop();
}
// 下
}else if (key == KeyEvent.VK_DOWN&&block.y<height-1) {
// 预测下一步是否会碰到钉子,如果碰到钉子就不向下移动
Point point=new Point(block.x,block.y+1);
if( isdingzi(point)==false) {
block.y++;
if (ispengzhuang()) {
zhuangtai = 2;
timer.stop();
}
}
// 左
}else if(key == KeyEvent.VK_LEFT&&block.x>0){
block.x--;
if(ispengzhuang()){
zhuangtai=2;
timer.stop();
}
// 右
}else if (key == KeyEvent.VK_RIGHT&&block.x<width-1) {
block.x++;
if(ispengzhuang()){
zhuangtai=2;
timer.stop();
}
}
}
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
// 判断块是否撞上钉子墙
public boolean ispengzhuang(){
if(isdingzi(block)==true)
{
life--;
if(life<0){
return true;
}
}
return false;
}
// 随机生成钉子墙
public int [] newwall(){
Random rd=new Random();
int wall[]=new int[width];
for (int i = 0; i < wall.length; i++) {
wall[i]=1;
}
// 挖洞(一次挖连续的两块墙)
int index= rd.nextInt(width-1);
wall[index]=0;
wall[index+1]=0;
// 再挖一次洞(一次挖连续的两块墙)
index= rd.nextInt(width-1);
wall[index]=0;
wall[index+1]=0;
return wall;
}
// point表示钉子墙的位置 wall表示钉子墙 point2表示某个点
// 这个函数的作用的某个点上是否有钉子
public boolean isdingzi(Point point,int[] wall,Point point2){
for (int i = 0; i < wall1.length; i++) {
if((point.x+i)==point2.x&&point.y==point2.y){
if(wall[i]==1){
return true;
}
}
}
return false;
}
public boolean isdingzi(Point point2){
if(isdingzi(point1,wall1,point2)==true){
return true;
}else if(isdingzi(this.point2,wall2,point2)==true){
return true;
}else if(isdingzi(point3,wall3,point2)==true){
return true;
}else if(isdingzi(point4,wall4,point2)==true){
return true;
}else if(isdingzi(point5,wall5,point2)==true){
return true;
}else if(isdingzi(point6,wall6,point2)==true){
return true;
}else{
return false;
}
}
public int[] xiaowall(){
Random rd=new Random();
int wall[]=new int[width];
for (int i = 0; i < wall.length; i++) {
wall[i]=0;
}
// 生成钉子(随机生成1-3个钉子)
int index= rd.nextInt(width-3);
int geshu=rd.nextInt(3)+1;
for (int i = 0; i < geshu; i++) {
wall[index+i]=1;
}
// 判断能否再次生成随机点
if(index%2==0){
index= rd.nextInt(width-3);
geshu=rd.nextInt(3)+1;
for (int i = 0; i < geshu; i++) {
wall[index+i]=1;
}
}
return wall;
}
public static void main(String[] args) {
Runbox006 runbox=new Runbox006();
runbox.Tian();
}
}
测试的次数不多,如果有bug还希望发现的朋友们留言提醒。