package com.Brick;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class myBrick extends JFrame implements Runnable{
login login=null;
myPanel mp=null;
endTobegin etb=null;
//鼠标标志
int flag_cursor=0;
public myBrick(){
login=new login();
Thread t=new Thread(login);
t.start();
// mp=new myPanel();
// Thread t=new Thread(mp);
// t.start();
// this.add(mp);
this.add(login);
this.setSize(800,600);
this.setLocation(40, 10);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stubl,l,
myBrick mb=new myBrick();
Thread t=new Thread(mb);
t.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//判断鼠标的形式
switch(this.flag_cursor){
case 0:
this.setCursor(Cursor.DEFAULT_CURSOR);
break;
case 1:
this.setCursor(Cursor.CROSSHAIR_CURSOR);
break;
case 2:
this.setCursor(Cursor.DEFAULT_CURSOR);
break;
}
if(this.login.flag1==false){
this.remove(login);
if(this.mp==null){
mp=new myPanel();
Thread t=new Thread(mp);
t.start();
}
else this.mp.init();
this.add(mp);
this.setVisible(true);
this.login.flag1=true;
this.flag_cursor=1;
// System.out.println("一");
}
if(this.mp!=null&&this.mp.hero.islive==false){
System.out.println(this.mp.hero.islive);
if(this.etb==null){
this.etb=new endTobegin();
Thread t=new Thread(etb);
t.start();
}
else this.etb.init();
// System.out.println(this.mp.hero.islive);
this.etb.setScores(this.mp.scores);
this.remove(mp);
// System.out.println("二");
this.add(etb);
this.setVisible(true);
// System.out.println(this.mp.hero.islive);
this.mp.hero.islive=true;
// System.out.println(this.mp.hero.islive);
this.flag_cursor=2;
}
if(this.etb!=null&&this.etb.flag1==false){
this.login.init();
this.remove(etb);
this.add(login);
this.etb.flag1=true;
// System.out.println("三");
this.flag_cursor=0;
}
// if(this.etb!=null){
//
// this.remove(etb);
// this.add(mp);
// this.setVisible(true);
// }
}
}
}
//游戏panel
class myPanel extends JPanel implements Runnable,MouseMotionListener{
//控制每关的自增长速度
boolean gameover;
static int speedcontrol;
//成绩记录类
scores scores=null;
hero hero=null;
int size=10;
Vector<enemybrick> v=new Vector<enemybrick>();
//构造函数
public myPanel(){
// this.setCursor(Cursor.S_RESIZE_CURSOR);
gameover=false;
speedcontrol=1;
scores=new scores();
for(int i=0;i<this.size;i++){
enemybrick eb=new enemybrick();
Thread t=new Thread(eb);
t.start();
v.add(eb);
}
hero=new hero();
this.addMouseMotionListener(this);
Thread t=new Thread(scores);
t.start();
}
//重新初始化
public void init(){
gameover=false;
speedcontrol=1;
this.scores.init();
for(int i=0;i<this.size;i++){
enemybrick eb=new enemybrick();
Thread t=new Thread(eb);
t.start();
v.add(eb);
}
this.hero.init();
}
public void paint(Graphics g){
super.paint(g);
//画背景
g.setColor(new Color(228, 233, 214));
g.fillRect(0, 0, 800, 600);
//画score
g.setColor(new Color(255, 181, 106));
g.setFont(new Font("宋体", Font.BOLD, 60));
g.drawString("score:", 240, 50);
g.drawString(String.valueOf(scores.scores), 320, 120);
g.setColor(Color.black);
g.fill3DRect(hero.x, hero.y, hero.width, hero.height, true);
//画砖块
for(int i=0;i<v.size();i++){
enemybrick eb=v.get(i);
if(eb.islive==true){
switch(eb.color){
case 0:g.setColor(new Color(255, 81, 81));break;
case 1:g.setColor(Color.BLACK);break;
}
if(eb.rand==0){
g.fillOval(eb.x, eb.y, eb.width, eb.width);
}
else g.fillRect(eb.x, eb.y, eb.width, eb.width);
}
else v.remove(eb);
}
this.drawspeed(g);
}
//画速度提示的语句
public void drawspeed(Graphics g){
g.setColor(new Color(255, 181, 106));
g.setFont(new Font("宋体", Font.BOLD, 15));
g.drawString("speed:", 0, 10);
if(this.speedcontrol<5)g.setColor(Color.GREEN);
else if(this.speedcontrol<10)g.setColor(Color.YELLOW);
else g.setColor(Color.RED);
int s=((this.speedcontrol<17)?this.speedcontrol:17);
if(s<17){
for(int i=0;i<s;i++){
g.fill3DRect(60+(i+1)*2, 2, 2, 9, true);
}
}
else {
if(this.speedcontrol%2==0)
for(int i=0;i<16;i++){
g.fill3DRect(60+(i+1)*2, 2, 2, 9, true);
}
else for(int i=0;i<17;i++){
g.fill3DRect(60+(i+1)*2, 2, 2, 9, true);
}
}
}
//判断是否相撞相撞时设置死亡,并从vector中remove掉
public void isTouch(enemybrick eb){
//如果与黑色碰撞
if(eb.color==1){
}
//如果与红色碰撞
if(eb.color==0){
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
Thread.sleep(15);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//判断 砖块儿死亡,然后重新初始化vector<enemybrick>
if(v.size()==0){
for(int i=0;i<this.size;i++){
enemybrick eb=new enemybrick();
eb.setSpeed(eb.speed+speedcontrol);
Thread t=new Thread(eb);
t.start();
v.add(eb);
}
//每关速度控制数 +1
if(this.hero.islive==true){
speedcontrol+=1;
}
}
for (int i = 0; i < v.size(); i++) {
enemybrick eb = v.get(i);
if (eb.islive==true) {
if (eb.width > hero.width) {
if ((hero.x >= eb.x && hero.x <= eb.x + eb.width
&& hero.y >= eb.y && hero.y <= eb.y + eb.width)
|| (hero.x + hero.width >= eb.x
&& hero.x + hero.width <= eb.x
+ eb.width && hero.y >= eb.y && hero.y <= eb.y
+ eb.width)
|| (hero.x >= eb.x && hero.x <= eb.x + eb.width
&& hero.y + hero.width >= eb.y && hero.y
+ hero.width <= eb.y + eb.width)
|| (hero.x + hero.width >= eb.x
&& hero.x + hero.width <= eb.x
+ eb.width
&& hero.y + hero.width >= eb.y && hero.y
+ hero.width <= eb.y + eb.width)) {
//此时游戏结束
if (eb.color == 0) {
hero.islive = false;
eb.islive = false;
this.v.remove(eb);
myPanel.speedcontrol = 0;
} else if (eb.color == 1) {
eb.islive = false;
this.v.remove(eb);
hero.setHeight(hero.height + 1);
hero.setWidth(hero.width + 1);
scores.scores += eb.width;
}
}
;
} else {
if ((eb.x >= hero.x && eb.x <= hero.x + hero.width
&& eb.y >= hero.y && eb.y <= hero.y
+ hero.width)
|| (eb.x + eb.width >= hero.x
&& eb.x + eb.width <= hero.x
+ hero.width && eb.y >= hero.y && eb.y <= hero.y
+ hero.width)
|| (eb.x >= hero.x
&& eb.x <= hero.x + hero.width
&& eb.y + eb.width >= hero.y && eb.y
+ eb.width <= hero.y + hero.width)
|| (eb.x + eb.width >= hero.x
&& eb.x + eb.width <= hero.x
+ hero.width
&& eb.y + eb.width >= hero.y && eb.y
+ eb.width <= hero.y + hero.width)) {
//此时游戏结束
if (eb.color == 0) {
hero.islive = false;
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
动感方块.rar (12个子文件)
动感方块
src
com
Brick
myBrick.java 14KB
brick1.java 4KB
bin
com
Brick
login.class 4KB
enemybrick.class 3KB
hero.class 1KB
myPanel.class 5KB
endTobegin.class 3KB
scores.class 943B
myBrick.class 2KB
.classpath 301B
.settings
org.eclipse.jdt.core.prefs 629B
.project 388B
共 12 条
- 1
资源评论
nonoocean1
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功