master
Xiao 2 years ago
parent 4e0553c0ae
commit dea8b1ee3d

@ -2,6 +2,9 @@ package com.sheep.model;
/* /*
Brand Brand
便
Brand
*/ */
import com.sheep.util.MapUtil; import com.sheep.util.MapUtil;
@ -20,21 +23,21 @@ public class Brand extends Component{
private Integer y;//坐标y private Integer y;//坐标y
private Integer width;//宽 private Integer width;//宽
private Integer height;//高 private Integer height;//高
private Map map; private Map map;//当前存储所在的map
EliminateBox eliminateBox = new EliminateBox(); EliminateBox eliminateBox = new EliminateBox();//将要绘制到的消除框
private Cell cell;//当前牌放置的单元格位置 private Cell cell;//当前牌放置的单元格位置
//有参构造器 //有参构造器,传入名字实现属性的赋值
public Brand(String name){ public Brand(String name){
this.name = name; this.name = name;//牌的名字
//指定图片 //指定图片
this.image = Toolkit.getDefaultToolkit().getImage("imgs\\"+name+".png"); this.image = Toolkit.getDefaultToolkit().getImage("imgs\\"+name+".png");
this.garyImage = Toolkit.getDefaultToolkit().getImage("imgs\\"+name+"_gray.png"); this.garyImage = Toolkit.getDefaultToolkit().getImage("imgs\\"+name+"_gray.png");
this.isGray = false;//默认为彩色图片 this.isGray = false;//默认为彩色图片,方便第一层的图标不用判断是否置灰
//设置宽高图片大小为50*50 //设置宽高图片大小为50*50
this.width = 50; this.width = 50;
@ -44,7 +47,7 @@ public class Brand extends Component{
this.x = 0; this.x = 0;
this.y = 0; this.y = 0;
//添加鼠标点击的监听事件 //添加鼠标点击的监听事件,灰色不可点击,否则,放到消除框
this.addMouseListener(new MouseAdapter() { this.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {

@ -3,6 +3,7 @@ package com.sheep.model;
/* /*
0 1 0 1
mapBrand
*/ */
public class Cell { public class Cell {

@ -2,6 +2,8 @@ package com.sheep.model;
/* /*
*/ */
import javax.swing.*; import javax.swing.*;
@ -44,7 +46,7 @@ public class EliminateBox {
} }
} }
paint(); paint();//图标绘制到消除框
over(brand); over(brand);
} }
@ -57,6 +59,7 @@ public class EliminateBox {
} }
} }
//当图标达到最大,游戏失败
void over(Brand brand){ void over(Brand brand){
if(slot.size() >= 7){ if(slot.size() >= 7){
JOptionPane.showMessageDialog(brand,"游戏失败"); JOptionPane.showMessageDialog(brand,"游戏失败");

@ -3,6 +3,8 @@ package com.sheep.model;
/* /*
mapBrandLayer便
*/ */
public class Layer { public class Layer {
@ -17,11 +19,11 @@ public class Layer {
private Layer parent;//上一层图层对象 private Layer parent;//上一层图层对象
private Cell[][] cells = null; private Cell[][] cells = null;//存储的二维表格
private Map map; private Map map;//当前所在的map
//有参构造 //有参构造,根据传入的行列实现表格初始化
public Layer(Integer rowNum,Integer colNum) { public Layer(Integer rowNum,Integer colNum) {
this.rowNum = rowNum; this.rowNum = rowNum;
this.colNum = colNum; this.colNum = colNum;
@ -35,6 +37,7 @@ public class Layer {
offsety = 0; offsety = 0;
} }
//打印表格,用于开发阶段测试使用
public void showCells(){ public void showCells(){
for(int row = 0;row < cells.length;row++){ for(int row = 0;row < cells.length;row++){
for(int col = 0;col<cells[row].length;col++){ for(int col = 0;col<cells[row].length;col++){

@ -4,6 +4,7 @@ package com.sheep.model;
*/ */
import com.sheep.util.MapUtil; import com.sheep.util.MapUtil;

@ -1,15 +1,23 @@
package com.sheep.music; package com.sheep.music;
/*
*/
import javax.sound.sampled.*; import javax.sound.sampled.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
public class BackgroundMusic { public class BackgroundMusic {
private Clip clip; private Clip clip;//存储音乐
//音乐名称
private String name1 = "wavs\\普通Disco.wav"; private String name1 = "wavs\\普通Disco.wav";
private String name2 = "wavs\\告白气球.wav"; private String name2 = "wavs\\告白气球.wav";
private String name3 = "wavs\\喜羊羊与灰太狼.wav"; private String name3 = "wavs\\喜羊羊与灰太狼.wav";
//播放背景音乐
public void play(String musicFilePath){ public void play(String musicFilePath){
try { try {
File musicFile = new File(musicFilePath); File musicFile = new File(musicFilePath);
@ -26,10 +34,12 @@ public class BackgroundMusic {
} }
} }
//暂停背景音乐
public void stop(){ public void stop(){
clip.stop(); clip.stop();
} }
//选择背景音乐
public void switchMusic(Integer gameMode){ public void switchMusic(Integer gameMode){
stop(); stop();
switch (gameMode){ switch (gameMode){

@ -3,6 +3,9 @@ package com.sheep.util;
/* /*
*/ */
import com.sheep.model.Brand; import com.sheep.model.Brand;
@ -11,7 +14,9 @@ import java.util.Random;
public class BrandUtil { public class BrandUtil {
public static Random random = new Random(); public static Random random = new Random();//随机数
//不同游戏主题的牌数据
public static String[] brandName1= { public static String[] brandName1= {
"刷子","剪刀","叉子","手套", "刷子","剪刀","叉子","手套",
"木桩","棉花","毛线","水桶", "木桩","棉花","毛线","水桶",
@ -30,6 +35,8 @@ public class BrandUtil {
"羊狼9","羊狼10","羊狼11","羊狼12", "羊狼9","羊狼10","羊狼11","羊狼12",
"羊狼13","羊狼14","羊狼15","羊狼16", "羊狼13","羊狼14","羊狼15","羊狼16",
}; };
//得到牌数据
public static String getBrandName(Integer gameMode){ public static String getBrandName(Integer gameMode){
int randomIndex = random.nextInt(brandName1.length); int randomIndex = random.nextInt(brandName1.length);
String retname = ""; String retname = "";

@ -1,5 +1,11 @@
package com.sheep.util; package com.sheep.util;
/*
BrandUtil
*/
import com.sheep.model.Brand; import com.sheep.model.Brand;
import com.sheep.model.Cell; import com.sheep.model.Cell;
import com.sheep.model.Layer; import com.sheep.model.Layer;

@ -1,5 +1,13 @@
package com.sheep.util; package com.sheep.util;
/*
*/
import java.awt.*; import java.awt.*;
import java.util.List; import java.util.List;
@ -76,7 +84,7 @@ public class MapUtil {
for(int row = 0;row < cells.length;row++){ for(int row = 0;row < cells.length;row++){
for(int col = 0;col <cells[row].length;col++){ for(int col = 0;col <cells[row].length;col++){
Cell cell = cells[row][col]; Cell cell = cells[row][col];
if(cell.getState()==1){ if(cell.getState()==1){//有牌,游戏继续
flag = false; flag = false;
Brand brand = cell.getBrand(); Brand brand = cell.getBrand();
boolean result = MapUtil.compare(brand,layer.getParent()); boolean result = MapUtil.compare(brand,layer.getParent());

@ -2,6 +2,7 @@ package com.sheep.view;
/* /*
*/ */
import com.sheep.model.Brand; import com.sheep.model.Brand;
@ -14,10 +15,11 @@ import javax.swing.*;
import java.util.List; import java.util.List;
public class Game extends JFrame { public class Game extends JFrame {
private Map map; private Map map;//用于存储游戏地图中的牌
//游戏界面的构造函数
public Game(Integer gameMode) { public Game(Integer gameMode) {
map = MapUtil.MapBuild(3,gameMode); map = MapUtil.MapBuild(3,gameMode);//创建地图
init();//初始化窗口 init();//初始化窗口
@ -26,6 +28,8 @@ public class Game extends JFrame {
for(int i = 0; i < list.size(); i++) { for(int i = 0; i < list.size(); i++) {
renderLayer(list.get(i)); renderLayer(list.get(i));
} }
//图标置灰
MapUtil.compareAll(map); MapUtil.compareAll(map);
//添加背景 //添加背景
@ -34,6 +38,7 @@ public class Game extends JFrame {
aotoRefresh();//自动刷新 aotoRefresh();//自动刷新
} }
//窗口初始化
private void init(){ private void init(){
this.setTitle("java羊了个羊");//标题 this.setTitle("java羊了个羊");//标题
this.setSize(425,800);//窗口大小 this.setSize(425,800);//窗口大小
@ -47,6 +52,7 @@ public class Game extends JFrame {
this.setVisible(true);//当前窗体显示 this.setVisible(true);//当前窗体显示
} }
//图标坐标位置偏移
private void renderLayer(Layer layer){ private void renderLayer(Layer layer){
Cell[][] cells = layer.getCells(); Cell[][] cells = layer.getCells();
@ -63,6 +69,7 @@ public class Game extends JFrame {
} }
} }
//根据Start传入的游戏模式添加背景
private void addBackgound(int gameMode){ private void addBackgound(int gameMode){
JLabel backgroungLabel; JLabel backgroungLabel;
switch(gameMode){ switch(gameMode){
@ -84,6 +91,7 @@ public class Game extends JFrame {
} }
} }
//重复刷新函数
private void aotoRefresh(){ private void aotoRefresh(){
JFrame start = this; JFrame start = this;

@ -1,5 +1,10 @@
package com.sheep.view; package com.sheep.view;
/*
Start
*/
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Start s = new Start(); Start s = new Start();

@ -12,15 +12,21 @@ import java.awt.event.MouseEvent;
/* /*
*/ */
public class Start extends JFrame { public class Start extends JFrame {
//存储当前游戏界面的类型,用于更换界面风格和背景音乐(游戏主题)
Integer gameMode; Integer gameMode;
//存储背景图片
JLabel backgroundLabel = new JLabel(new ImageIcon("imgs/背景1.png")); JLabel backgroundLabel = new JLabel(new ImageIcon("imgs/背景1.png"));
//存储背景音乐
BackgroundMusic music = new BackgroundMusic(); BackgroundMusic music = new BackgroundMusic();
//构造函数,调用各个函数实现初始化
public Start() { public Start() {
gameMode = 1; gameMode = 1;//初始化游戏主题
init();//初始化窗口 init();//初始化窗口
//添加背景及按钮 //添加背景及按钮
@ -29,9 +35,11 @@ public class Start extends JFrame {
//重复刷新 //重复刷新
aotoRefresh(); aotoRefresh();
//播放背景音乐
music.play("wavs\\普通Disco.wav"); music.play("wavs\\普通Disco.wav");
} }
//窗口的初始化
private void init(){ private void init(){
this.setTitle("java羊了个羊");//标题 this.setTitle("java羊了个羊");//标题
this.setSize(425,800);//窗口大小 this.setSize(425,800);//窗口大小
@ -45,28 +53,33 @@ public class Start extends JFrame {
this.setVisible(true);//当前窗体显示 this.setVisible(true);//当前窗体显示
} }
//添加背景、按钮、切换主题的图片
private void addBackgound(){ private void addBackgound(){
//经典主题
JLabel gameMode1 = new JLabel(new ImageIcon("imgs/刷子.png")); JLabel gameMode1 = new JLabel(new ImageIcon("imgs/刷子.png"));
gameMode1.setBounds(100,550,50,50); gameMode1.setBounds(100,550,50,50);
this.add(gameMode1); this.add(gameMode1);
//羊羊主题
JLabel gameMode2 = new JLabel(new ImageIcon("imgs/羊1.png")); JLabel gameMode2 = new JLabel(new ImageIcon("imgs/羊1.png"));
gameMode2.setBounds(200,550,50,50); gameMode2.setBounds(200,550,50,50);
this.add(gameMode2); this.add(gameMode2);
//喜羊羊与灰太狼主题
JLabel gameMode3 = new JLabel(new ImageIcon("imgs/羊狼1.png")); JLabel gameMode3 = new JLabel(new ImageIcon("imgs/羊狼1.png"));
gameMode3.setBounds(300,550,50,50); gameMode3.setBounds(300,550,50,50);
this.add(gameMode3); this.add(gameMode3);
//开始按钮
JButton startButton = new JButton(new ImageIcon("imgs/开始.png")); JButton startButton = new JButton(new ImageIcon("imgs/开始.png"));
startButton.setBounds(125,400,200,50); startButton.setBounds(125,400,200,50);
this.add(startButton); this.add(startButton);
//添加背景图片到窗体
backgroundLabel.setBounds(0,0,450,800); backgroundLabel.setBounds(0,0,450,800);
this.add(backgroundLabel); this.add(backgroundLabel);
//游戏模式1监听事件 //游戏模式1监听事件(经典模式)
gameMode1.addMouseListener(new MouseAdapter() { gameMode1.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
@ -78,7 +91,7 @@ public class Start extends JFrame {
} }
}); });
//游戏模式2监听事件 //游戏模式2监听事件(羊羊模式)
gameMode2.addMouseListener(new MouseAdapter() { gameMode2.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
@ -90,7 +103,7 @@ public class Start extends JFrame {
} }
}); });
//游戏模式3监听事件 //游戏模式3监听事件(喜羊羊与灰太狼模式)
gameMode3.addMouseListener(new MouseAdapter() { gameMode3.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
@ -102,16 +115,17 @@ public class Start extends JFrame {
} }
}); });
//按钮添加监听事件 //开始按钮添加监听事件
startButton.addActionListener(new ActionListener() { startButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
new Game(gameMode); new Game(gameMode);//创建游戏界面
Start.super.dispose(); Start.super.dispose();//关闭开始界面
} }
}); });
} }
//重复刷新函数
private void aotoRefresh(){ private void aotoRefresh(){
JFrame start = this; JFrame start = this;

Loading…
Cancel
Save