master
Xiao 2 years ago
parent 4e0553c0ae
commit dea8b1ee3d

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

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

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save