昨天在B站大神那边,学习了一个用Java变成的小游戏,大鱼吃小鱼。
给大家把游戏的想换代码给大家分享一下。
GameWin---游戏主窗口
package com.sxt;
/*
* @author 加墨少言
*@version 2021100100001
*
* */
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWin extends JFrame {
/*游戏状态 0未开始、1游戏中、2游戏通关、3游戏失败、4暂停*/
//游戏默认状态
static int state =0;
//定义成员变量
Image offScreenImage;
//定义长和宽
int width = 1180;
int height =640;
double random;
//计数器
int time=0;
Bg bg=new Bg();
//敌方鱼类
Enamy enamy;
//敌方BOSS类
Enamy boss;
//是否生成BOSS
boolean isboss=false;
//我方鱼类
MyFish myFish=new MyFish();
public void launch(){
this.setVisible(true);//显示窗口
this.setSize(width,height);//调用窗口尺寸大小
this.setLocationRelativeTo(null);//窗口居中显示
this.setResizable(false);//窗口大小不可调整
this.setTitle("小鱼成长历险记"+" "+"2021100100001");//定义窗口名称
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口,自动停止运行
//创建鼠标点击事件,点击为1,
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if(e.getButton()==1&&state==0){
state=1;
repaint();
}
if(e.getButton()==1&&(state==2||state==3)){
reGame();
state=1;
}
}
});
//键盘移动
this.addKeyListener(new KeyAdapter() {
@Override//按压
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
//WASD
if(e.getKeyCode()==87){
GameUtils.UP=true;
}
if(e.getKeyCode()==83){
GameUtils.DOWN=true;
}
if(e.getKeyCode()==65){
GameUtils.LEFT=true;
}
if(e.getKeyCode()==68){
GameUtils.RIGHT=true;
}
//监听键盘空格键(32),暂停游戏,在1和4之间切换
if(e.getKeyCode()==32){
switch (state){
case 1:
state=4;
GameUtils.drawWord(getGraphics(),"游戏暂停!!!",Color.GREEN,120,350,500);
break;
case 4:
state=1;
break;
}
}
}
@Override//抬起
public void keyReleased(KeyEvent e){
super.keyReleased(e);
if(e.getKeyCode()==87){
GameUtils.UP=false;
}
if(e.getKeyCode()==83){
GameUtils.DOWN=false;
}
if(e.getKeyCode()==65){
GameUtils.LEFT=false;
}
if(e.getKeyCode()==68){
GameUtils.RIGHT=false;
}
}
});
//创建while循环,40毫秒,调用一次repaint
while (true){
repaint();
time++;
try {
Thread.sleep(40);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
//创建画笔,绘画背景
public void paint(Graphics g){
//加载模式初始化对象
offScreenImage =createImage(width,height);
Graphics gImage=offScreenImage.getGraphics();
bg.paintSelf(gImage,myFish.level);
switch (state){
case 0:
break;
case 1:
myFish.paintSelf(gImage);
logic();
for(Enamy enamy:GameUtils.EnamyList){
enamy.painSelf(gImage);
}
//添加BOSS
if(isboss){
boss.x=boss.x+boss.dir*boss.speed;
boss.painSelf(gImage);
//BOSS出现预警
if(boss.x<0){
gImage.setColor(Color.red);
gImage.fillRect(boss.x,boss.y,2400,boss.height%30);
}
}
break;
case 2:
for(Enamy enamy:GameUtils.EnamyList){
enamy.painSelf(gImage);
}
if(isboss){
boss.painSelf(gImage);
}
break;
case 3:
myFish.paintSelf(gImage);
break;
case 4:
return;
default:
}
g.drawImage(offScreenImage,0,0,null);
}
void logic(){
//关卡难度
if(GameUtils.count<5){
GameUtils.level=0;
myFish.level=1;
//积分为15及15以下时,等级为1
}else if(GameUtils.count<=15){
GameUtils.level=1;
//积分为50及50以下时,等级为2
//鱼类等级为2
}else if (GameUtils.count<=50){
GameUtils.level=2;
myFish.level=2;
//积分为150及150以下时,等级为3
//鱼类等级为3
}else if(GameUtils.count<=150){
GameUtils.level=3;
myFish.level=3;
//积分为300及300以下时,等级为4
//鱼类等级为3
}else if (GameUtils.count<=300){
GameUtils.level=4;
myFish.level=3;
//当积分超过300,跳转界面3(胜利)
}else if (GameUtils.count>300){
state =3;
}
//定义一个随机函数
random=Math.random();
//通过难度等级,生成对应的鱼
switch (GameUtils.level) {
case 4:
//90秒时,开始出现BOSS鱼类
if(time%90==0){
if (random>0){
boss=new Enamy_Boss();
isboss=true;
}
}
case 3:
case 2:
//60秒时,开始出现3号鱼类
if(time%60==0){
if(random>0.5){
enamy=new Enamy_3_L();
}else {
enamy=new Enamy_3_R();
}
GameUtils.EnamyList.add(enamy);
}
case 1:
//30秒时,开始出现2号鱼类
if(time%30==0){
if(random>0.5){
enamy=new Enamy_2_L();
}else {
enamy=new Enamy_2_R();
}
GameUtils.EnamyList.add(enamy);
}
case 0:
//10秒时,开始添加1号鱼类
if (time%10 == 0) {
if (random > 0.5) {
enamy = new Enamy_1_L();
} else {
enamy = new Enamy_1_R();
}
GameUtils.EnamyList.add(enamy);
}
break;
default:
}
//鱼类移动方向
for(Enamy enamy:GameUtils.EnamyList){
enamy.x=enamy.x+enamy.dir*enamy.speed;
if(isboss){
//如果BOSS矩形和敌方鱼类矩形碰撞,敌方鱼类扭转到制定位置
if(boss.getRec().intersects(enamy.getRec())){
enamy.x=-200;
enamy.y=-200;
}
//如果BOSS矩形和玩家鱼矩形碰撞,跳转到界面2(失败)
if(boss.getRec().intersects(myFish.getRec())){
state=2;
}
}
//我方鱼与敌方鱼碰撞检测
if(myFish.getRec().intersects(enamy.getRec())){
//如果我的鱼类等级,高于鱼类类型,吃掉鱼,获得积分,地方鱼类重新出现在指定位置
if(myFish.level>=enamy.type){
System.out.println("吃啦");
System.out.println("获得"+GameUtils.count+"积分");
enamy.x=-200;
enamy.y=-200;
GameUtils.count=GameUtils.count+enamy.count;
}else {
//不然,跳转到界面2(失败)
state=2;
}
}
}
}
//重新开始
void reGame(){
GameUtils.EnamyList.clear();
time=0;
myFish.level=1;
GameUtils.count=0;
myFish.x=700;
myFish.y=500;
myFish.width=50;
myFish.height=50;
boss=null;
isboss=false;
}
//主方法
public static void main(String[] args){
GameWin gameWin =new GameWin();
gameWin.launch();
}
}
Bg---游戏背景图片
package com.sxt;
import java.awt.*;
public class Bg {
void paintSelf(Graphics g,int fishlevel){
g.drawImage(GameUtils.bgimg,0,0,null);
switch (GameWin.state){
case 0:
GameUtils.drawWord(g,"开始",Color.red,80,450,350);
break;
case 1:
GameUtils.drawWord(g,"积分"+GameUtils.count,Color.ORANGE,30,200,70);
GameUtils.drawWord(g,"难度"+GameUtils.level,Color.ORANGE,30,600,70);
GameUtils.drawWord(g,"等级"+fishlevel,Color.ORANGE,30,1000,70);
break;
case 2:
GameUtils.drawWord(g,"积分"+GameUtils.count,Color.ORANGE,30,200,70);
GameUtils.drawWord(g,"难度"+GameUtils.level,Color.ORANGE,30,600,70);
GameUtils.drawWord(g,"等级"+fishlevel,Color.ORANGE,30,1000,70);
GameUtils.drawWord(g,"失败",Color.red,200,350,350);
break;
case 3:
GameUtils.drawWord(g,"积分"+GameUtils.count,Color.ORANGE,30,200,70);
GameUtils.drawWord(g,"难度"+GameUtils.level,Color.ORANGE,30,600,70);
GameUtils.drawWord(g,"等级"+fishlevel,Color.ORANGE,30,1000,70);
GameUtils.drawWord(g,"胜利",Color.red,150,700,500);
break;
case 4:
break;
default:
}
}
}
GameUtil----工具类
package com.sxt;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class GameUtils {
//方向控制
static boolean UP=false;
static boolean DOWN=false;
static boolean LEFT=false;
static boolean RIGHT=false;
//分数
static int count=0;
//关卡等级
static int level=0;
//敌方鱼类集合
public static List<Enamy>EnamyList=new ArrayList<>();
//定义固定的静态图片
public static Image bgimg=Toolkit.getDefaultToolkit().createImage("img/sea.jpg");
//敌方鱼类
public static Image enemyl_img=Toolkit.getDefaultToolkit().createImage("img/enemyfish/fish1_l.jpg");
public static Image enemyr_img=Toolkit.getDefaultToolkit().createImage("img/enemyfish/fish1_r.jpg");
public static Image enemyl_2img=Toolkit.getDefaultToolkit().createImage("img/enemyfish/fish2_l.jpg");
public static Image enemyr_2img=Toolkit.getDefaultToolkit().createImage("img/enemyfish/fish2_r.jpg");
public static Image enemyl_3img=Toolkit.getDefaultToolkit().createImage("img/enemyfish/fish3_l.jpg");
public static Image enemyr_3img=Toolkit.getDefaultToolkit().createImage("img/enemyfish/fish3_r.jpg");
public static Image bossimg=Toolkit.getDefaultToolkit().createImage("img/enemyfish/boss.jpg");
//我方鱼
public static Image MyFishimg_L=Toolkit.getDefaultToolkit().createImage("img/myfish/myfish_left.jpg");
public static Image MyFishimg_R=Toolkit.getDefaultToolkit().createImage("img/myfish/myfish_right.jpg");
//绘制文字的工具类
public static void drawWord(Graphics g,String str,Color color,int size,int x,int y){
g.setColor(color);
g.setFont(new Font("仿宋",Font.BOLD,size));
g.drawString(str,x,y);
}
}
MyFish----我的鱼
package com.sxt;
import java.awt.*;
public class MyFish {
//图片
Image img=GameUtils.MyFishimg_L;
//坐标
int x=700;
int y=500;
int width=94;
int height=57;
int speed=20;
int level=1;
//对我方鱼类的控制
void logic(){
if(GameUtils.UP){
y=y-speed;
}
if(GameUtils.DOWN){
y=y+speed;
}
if(GameUtils.LEFT){
x=x-speed;
img=GameUtils.MyFishimg_L;
}
if(GameUtils.RIGHT){
x=x+speed;
img=GameUtils.MyFishimg_R;
}
}
//绘制自身的方法
public void paintSelf(Graphics g){
logic();
g.drawImage(img,x,y,width+GameUtils.count,height+GameUtils.count,null);
}
//获取自身矩形,用于碰撞检测
public Rectangle getRec(){
return new Rectangle(x,y,width+GameUtils.count,height+GameUtils.count);
}
}
Enamy----敌方鱼类
package com.sxt;
import java.awt.*;
public class Enamy {
//定义图片
Image img;
//定义物体坐标
int x;
int y;
int width;
int height;
//移动速度
int speed;
//方向
int dir = 1;
//类型
int type;
//分值
int count;
//绘制自身的方法
public void painSelf(Graphics g){
g.drawImage(img,x,y,width,height,null);
}
//获取自身矩形用于碰撞检测
public Rectangle getRec(){
return new Rectangle(x,y,width,height);
}
}
//敌方鱼左类
class Enamy_1_L extends Enamy{
Enamy_1_L(){
this.x=-57;
this.y=(int)(Math.random()*700+100);
this.width=57;
this.height=30;
this.speed=5;
this.count=1;
this.type=1;
this.img=GameUtils.enemyl_img;
}
}
class Enamy_1_R extends Enamy_1_L{
Enamy_1_R(){
this.x=1400;
dir=-1;
this.img=GameUtils.enemyr_img;
}
}
//定义第二类鱼
class Enamy_2_L extends Enamy{
Enamy_2_L(){
this.x=-100;
this.y=(int)(Math.random()*700+100);
this.width=69;
this.height=37;
this.speed=7;
this.count=2;
this.type=2;
this.img=GameUtils.enemyl_2img;
}
}
class Enamy_2_R extends Enamy_2_L{
Enamy_2_R(){
this.x=1400;
dir=-1;
this.img=GameUtils.enemyr_2img;
}
}
//定义第三类鱼
class Enamy_3_L extends Enamy{
Enamy_3_L(){
this.x=-300;
this.y=(int)(Math.random()*700+100);
this.width=64;
this.height=39;
this.speed=13;
this.count=4;
this.type=4;
this.img=GameUtils.enemyl_3img;
}
@Override//因为鱼体积过大,从新定义碰撞矩形
public Rectangle getRec() {
return new Rectangle(x+40,y+30,width+80,height+60);
}
}
class Enamy_3_R extends Enamy_3_L{
Enamy_3_R(){
this.x=1400;
dir=-1;
this.img=GameUtils.enemyr_3img;
}
}
//定义BOSS类
class Enamy_Boss extends Enamy{
Enamy_Boss(){
this.x=-1000;
this.y=(int)(Math.random()*700+100);
this.width=300;
this.height=300;
this.speed=20;
this.type=10;
this.count=0;
this.img=GameUtils.bossimg;
}
}
链接:https://pan.baidu.com/s/1XcoB1Ve5xrs1Le0rWlj1rw
提取码:aabb