查色盲俄罗斯方块源码
//第一个文件:Game.java
package realgame;
import javax.swing.UIManager;
import java.awt.*;
public class Game {
boolean packFrame = false;
//Construct the application
public Game() {
GameFrame frame = new GameFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
frame.setDefaultCloseOperation(3);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch(Exception e) {
e.printStackTrace();
}
new Game();
}
}
//====================================第二个文件GameFrame.java编程时请另建一文件,将下面的内容放入.
package realgame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.Timer;
import java.awt.geom.*;
class GameFrame extends JFrame{
private static final int WIDTH=225;
private static final int HEIGHT=295;
GamePanel gamePanel;
public GameFrame(){
setTitle("查色盲!俄罗斯方块");
setSize(WIDTH,HEIGHT);
this.setResizable(false);
GamePanel gamePanel=new GamePanel();
Container contentPane=getContentPane();
contentPane.add(gamePanel);
// gamePanel.start();
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setResizable(false);
}
}
class GamePanel extends JPanel{
private static int clockInterval=500;
private static final int ROWS=20;
private static final int COLS=10;
private static int keyEventValid=1;
Timer t;
private static int top=ROWS-1;
private static int score;
private int[][] screen=new int[ROWS][COLS];//原有屏幕上的信息,得分情况来自于此
private int[][] view=new int[ROWS][COLS];//块在screen显示所看到的视图
private Block presentBlock;//当前活动块
private Block nextBlock;//下一个块,以作预览
private String stop="";
//块在屏幕上的位置
private int row;
private int col;
private int preCol;
public GamePanel(){
KeyHandler listener =new KeyHandler();
addKeyListener(listener);
nextBlock=new Block(0);
col=(int)(Math.random()*(COLS-nextBlock.maxCol));
preCol=col;
this.setBackground(Color.BLACK);
this.setLayout(new BorderLayout());
ControlPanel cp=new ControlPanel();
cp.addKeyListener(listener);
cp.jl.addKeyListener(listener);
cp.js.addKeyListener(listener);
cp.startJB.addKeyListener(listener);
cp.endGame.addKeyListener(listener);
this.add(BorderLayout.SOUTH,cp);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
//首先画出显示区的大框
drawBigRec(g2);
//依view的内容画出相应的格子
for(int i=0;i<view.length;i++)
for(int j=0;j<view[i].length;j++)
if (view[i][j]==1) drawAnElem(i,j,g2,0,0);
//画出预览区
final float xPos=140.0F;
final float yPos=60.0F;
Font f=new Font("Serif",Font.BOLD,18);
g2.setFont(f);
g2.setPaint(Color.red);
g2.drawString("SCORE",xPos,yPos);
g2.setPaint(Color.green);
g2.drawString(""+score,xPos,yPos+20);
g2.setPaint(Color.green);
g2.drawString(stop,xPos,yPos-30);
final double preXPos=140.0F;
final double preYPos=140.0F;
double preEdge=60.0;
Rectangle2D rect=new Rectangle2D.Double(preXPos,preYPos,preEdge,preEdge);
g2.setColor(Color.orange);
g2.fill(rect);
Rectangle2D innerRect=new Rectangle2D.Double(preXPos+10,preYPos+10,preEdge-20,preEdge-20);
g2.setColor(Color.white);
g2.fill(innerRect);
for(int i=0;i<nextBlock.maxRow;i++)
for(int j=0;j<nextBlock.maxCol;j++)
if(nextBlock.bodyInfo[i][j]==1)
drawAnElem(i,j,g2,preXPos,preYPos);
}//~panel的绘制结束
public boolean moveLeft(){
return(go(row,col-1));
}
public boolean moveRight(){
return(go(row,col+1));
}
public boolean moveDown(){
return(go(row+1,col));
}
//如果发生碰撞则返回true;
private boolean crash(int r,int c,Block temp){
boolean flag=false;
if((r+temp.maxRow>ROWS)||(c+temp.maxCol>COLS)) return true;
for(int i=0;i<temp.maxRow;i++)
for(int j=0;j<temp.maxCol;j++)
if((temp.bodyInfo[i][j]==1)&&(screen[r+i][c+j]==1))
flag=true;
return flag;
}
private boolean go(int r,int c){
boolean flag=true;
if(((r+presentBlock.maxRow)>ROWS)||((c+presentBlock.maxCol)>COLS)||(c<0))
return false;
for(int i=0;i<presentBlock.maxRow;i++)
for(int j=0;j<presentBlock.maxCol;j++)
if((presentBlock.bodyInfo[i][j]==1)&&(screen[r+i][c+j]==1))
flag=false;
if(flag){
row=r;col=c;
setView();
}
return flag;
}
//画出一个单元格;
private void drawAnElem(int i,int j,Graphics2D g2,double xPos,double yPos){
final int edge=10;
final int margin=10;
final int innerMargin=1;
Rectangle2D rect=new Rectangle2D.Double(xPos+margin+j*10,yPos+margin+i*10,edge,edge);
g2.setPaint(Color.red);
g2.draw(rect);
rect=new Rectangle2D.Double(xPos+margin+j*10+innerMargin,yPos+margin+i*10+innerMargin,
edge-innerMargin,edge-innerMargin
);
int colorChoser=0;
colorChoser=(int)(Math.random()*11);
if (colorChoser==0)g2.setPaint(Color.blue);
else if(colorChoser==1) g2.setPaint(Color.green);
else if(colorChoser==2) g2.setPaint(Color.orange);
else if(colorChoser==3) g2.setPaint(Color.yellow);
else if(colorChoser==4) g2.setPaint(Color.magenta);
else if(colorChoser==5) g2.setPaint(Color.red);
else if(colorChoser==6) g2.setPaint(Color.yellow);
else if(colorChoser==7) g2.setPaint(Color.CYAN);
else if(colorChoser==8) g2.setPaint(new Color(200,100,150));
else if(colorChoser==9) g2.setPaint(Color.GREEN);
else if(colorChoser==10) g2.setPaint(Color.PINK);
g2.fill(rect);
}
//画出显示区的外框
private void drawBigRec(Graphics2D g2){
final double REC_WIDTH=120;
final double REC_HEIGHT=220;
Rectangle2D rect=new Rectangle2D.Double(0,0,REC_WIDTH,REC_HEIGHT);
Rectangle2D innerRect=new Rectangle2D.Double(10,10,REC_WIDTH-20,REC_HEIGHT-20);
g2.setPaint(Color.orange);
g2.fill(rect);
g2.setPaint(Color.white);
g2.fill(innerRect);
}
//设置视图
private void setView(){
//将screen中的内容复制到view中
for(int i=0;i<screen.length;i++)
for(int j=0;j<screen[i].length;j++)
view[i][j]=screen[i][j];
//将block投影到view中;
for(int i=0;i<presentBlock.maxRow;i++)
for(int j=0;j<presentBlock.maxCol;j++)
if(presentBlock.bodyInfo[i][j]==1)
view[i+row][j+col]=presentBlock.bodyInfo[i][j];
//每次设置视图后要求画面重绘
repaint();
}
//事件处理
private class KeyHandler implements KeyListener{
Block temp=null;
public void keyPressed(KeyEvent event){
int keyCode =event.getKeyCode();
if(keyCode==KeyEvent.VK_LEFT) {
if(keyEventValid==1)
if(moveLeft()) setView();}
else if(keyCode==KeyEvent.VK_RIGHT) {
if(keyEventValid==1)
if(moveRight()) setView();}
else if(keyCode==KeyEvent.VK_DOWN) {
if(keyEventValid==1)
if(moveDown()) setView();}
else if(keyCode==KeyEvent.VK_UP){
if(keyEventValid==1){
temp=new Block(presentBlock.bodyType);
temp.rotate();
if (!crash(row,col,temp))
{
presentBlock.rotate();
setView();
}
temp=null;
} }//~UP
}
public void keyReleased(KeyEvent event){}
public void keyTyped(KeyEvent event){}
}
public boolean isFocusTraversable(){
return true;
}
private int winScore(){
int inc=0;
int cur=ROWS-1;
while(cur>=top){
if(allDone(cur)){
inc+=100;
score+=100;
removeMe(cur,top);
top++;
}else cur--;
}//~while
return inc;
}
private boolean allDone(int R){
boolean flag=true;
for(int c=0;c<COLS;c++)
if(screen[R][c]==0) flag=false;
return flag;
}
private void removeMe(int R,int T){
for(int r=R;r>T;r--){
for(int c=0;c<COLS;c++)
screen[r][c]=screen[r-1][c];//将上方的向下座
}
for(int c=0;c<COLS;c++)
screen[T][c]=0;//清除第T行;
}
private class RegularGo implements ActionListener{
public void actionPerformed(ActionEvent event){
boolean flag=moveDown();
if(!flag){
keyEventValid=0;//先给键盘上锁
anchor();
setTop();
int tempScore=winScore();
if((tempScore>=100)&&(tempScore)<300)
stop="嗯加油";
else if (tempScore>=300)
stop="不错啊";
else
stop="";
setView();
presentBlock=nextBlock;
row=0;
//保证每次进入点不同
do{col=(int)(Math.random()*(COLS-presentBlock.maxCol));}
while(col==preCol);
preCol=col;
keyEventValid=1;
nextBlock=new Block(0);
setView();
System.gc();
}
if(top==0) {keyEventValid=0;stop="玩完了";setView();t.stop();}
}
}
public void start(){
keyEventValid=1;
for(int r=0;r<ROWS;r++)
for(int c=0;c<COLS;c++)
screen[r][c]=0;
top=ROWS-1;
score=0;
stop="开始了";
// nextBlock=new Block(0);
presentBlock=nextBlock;
nextBlock=new Block(0);
repaint();
RegularGo go=new RegularGo();
t=new Timer(clockInterval,go);
t.start();
}//~start
private void anchor(){
for(int r=0;r<presentBlock.maxRow;r++)
for(int c=0;c<presentBlock.maxCol;c++)
if(presentBlock.bodyInfo[r][c]==1)
screen[row+r][col+c]=1;
}
private void setTop(){
int myTop=0;
int c=0;
while(myTop<ROWS-1){
c=0;
while(c<COLS){
if (screen[myTop][c]==1) break;
else c++;
}
if (c<COLS) break;
else myTop++;
}
top=myTop;
}//setTop
private void setSpeed(int speed){
t.setDelay(clockInterval);
}
private class ControlPanel extends JPanel{
JLabel jl=new JLabel(" 调速");
JSlider js=new JSlider(1,5,1);
JButton startJB=new JButton("开始");
JButton endGame=new JButton("结束");
public ControlPanel(){
setBackground(Color.orange);
js.setBackground(Color.orange);
startJB.setBackground(Color.orange);
endGame.setBackground(Color.orange);
setLayout(new GridLayout(2,2));
add(jl);
add(js);
js.addChangeListener(new speedTune());
Font font=new Font("Serif",Font.PLAIN,12);
add(startJB);
jl.setFont(font);
startJB.setFont(font);
startJB.addActionListener(new startAction());
add(endGame);
endGame.setFont(font);
endGame.addActionListener(new endAction());
}
private class startAction implements ActionListener{
public void actionPerformed(ActionEvent event){
start();
}
}
private class endAction implements ActionListener{
public void actionPerformed(ActionEvent event){
System.exit(3);
}
}
private class speedTune implements ChangeListener{
public void stateChanged(ChangeEvent event){
JSlider slider =(JSlider)event.getSource();
clockInterval=1000/slider.getValue();
if(t!=null)
setSpeed(slider.getValue());
startJB.requestFocus();
}
}
}//~CP
}//~
class Block{
public int[][] bodyInfo= new int[4][4];
public int maxRow;
public int maxCol;
public int bodyType;
public Block(int shape){
final int TYPES=20;
int i;
if(shape==0)
i=(int)(Math.random()*TYPES)+1;
else
i=shape;
setBody(i);
bodyType=i;
}//~con
//setBody中应设定bodyType
private void setBody(int i){
switch(i){
case 1://(竖长条形)
for(int r=0;r<bodyInfo.length;r++)
bodyInfo[r][0]=1;
maxRow=4;
maxCol=1;
break;
case 2://(横长条形)
for(int c=0;c<bodyInfo[0].length;c++)
bodyInfo[0][c]=1;
maxRow=1;
maxCol=4;
break;
case 3://顺闪电形
bodyInfo[0][0]=1;bodyInfo[1][0]=1;
bodyInfo[1][1]=1;bodyInfo[2][1]=1;
maxRow=3;
maxCol=2;
break;
case 4://顺闪平形
bodyInfo[0][1]=1;bodyInfo[0][2]=1;
bodyInfo[1][0]=1;bodyInfo[1][1]=1;
maxRow=2;
maxCol=3;
break;
case 5://反闪电形
bodyInfo[0][1]=1;bodyInfo[1][0]=1;
bodyInfo[1][1]=1;bodyInfo[2][0]=1;
maxRow=3;
maxCol=2;
break;
case 6://反闪平形
bodyInfo[0][0]=1;bodyInfo[0][1]=1;
bodyInfo[1][1]=1;bodyInfo[1][2]=1;
maxRow=2;
maxCol=3;
break;
case 7://土形
bodyInfo[0][1]=1;bodyInfo[1][0]=1;
bodyInfo[1][1]=1;bodyInfo[1][2]=1;
maxRow=2;
maxCol=3;
break;
case 8://土形(90)
bodyInfo[0][0]=1;bodyInfo[1][0]=1;
bodyInfo[1][1]=1;bodyInfo[2][0]=1;
maxRow=3;
maxCol=2;
break;
case 9://土形(180)
bodyInfo[0][0]=1;bodyInfo[0][1]=1;
bodyInfo[0][2]=1;bodyInfo[1][1]=1;
maxRow=2;
maxCol=3;
break;
case 10://土形(270)
bodyInfo[0][1]=1;bodyInfo[1][0]=1;
bodyInfo[1][1]=1;bodyInfo[2][1]=1;
maxRow=3;
maxCol=2;
break;
case 11://反7形
bodyInfo[0][0]=1;bodyInfo[0][1]=1;
bodyInfo[1][0]=1;bodyInfo[2][0]=1;
maxRow=3;
maxCol=2;
break;
case 12://反7形(90)
bodyInfo[0][0]=1;bodyInfo[0][1]=1;
bodyInfo[0][2]=1;bodyInfo[1][2]=1;
maxRow=2;
maxCol=3;
break;
case 13://反7形(180)
bodyInfo[0][1]=1;bodyInfo[1][1]=1;
bodyInfo[2][1]=1;bodyInfo[2][0]=1;
maxRow=3;
maxCol=2;
break;
case 14://反7形(270)
bodyInfo[0][0]=1;bodyInfo[1][0]=1;
bodyInfo[1][1]=1;bodyInfo[1][2]=1;
maxRow=2;
maxCol=3;
break;
case 15://7字形
bodyInfo[0][0]=1;bodyInfo[0][1]=1;
bodyInfo[1][1]=1;bodyInfo[2][1]=1;
maxRow=3;
maxCol=2;
break;
case 16://7字型(90)
bodyInfo[0][2]=1;bodyInfo[1][0]=1;
bodyInfo[1][1]=1;bodyInfo[1][2]=1;
maxRow=2;
maxCol=3;
break;
case 17://7字形(180)
bodyInfo[0][0]=1;bodyInfo[1][0]=1;
bodyInfo[2][0]=1;bodyInfo[2][1]=1;
maxRow=3;
maxCol=2;
break;
case 18://7字形(270)
bodyInfo[0][0]=1;bodyInfo[0][1]=1;
bodyInfo[0][2]=1;bodyInfo[1][0]=1;
maxRow=2;
maxCol=3;
break;
case 19://田字形
bodyInfo[0][0]=1;bodyInfo[0][1]=1;
bodyInfo[1][0]=1;bodyInfo[1][1]=1;
maxRow=2;
maxCol=2;
break;
case 20://田字型
bodyInfo[0][0]=1;bodyInfo[0][1]=1;
bodyInfo[1][0]=1;bodyInfo[1][1]=1;
maxRow=2;
maxCol=2;
break;
/* case 21:
for(int c=0;c<bodyInfo[0].length;c++)
bodyInfo[0][c]=1;
maxRow=1;
maxCol=4;
break;
case 22:
bodyInfo[0][0]=1;bodyInfo[1][0]=1;
bodyInfo[1][1]=1;bodyInfo[2][1]=1;
maxRow=3;
maxCol=2;
break;
case 23:
bodyInfo[0][1]=1;bodyInfo[1][0]=1;
bodyInfo[1][1]=1;bodyInfo[2][0]=1;
maxRow=3;
maxCol=2;
break;
*/
//如果你想加入更多的块形状,只要在这里加就是了,注意把bodyType的变量的值调为你的块种类数啊。
}
}//~setBody
//根据bodyType作相应的变换
public void rotate(){
switch(bodyType){
case 1: clear();setBody(2); bodyType=2;break;
case 2: clear();setBody(1); bodyType=1;break;
case 3: clear();setBody(4); bodyType=4;break;
case 4: clear();setBody(3); bodyType=3;break;
case 5: clear();setBody(6); bodyType=6;break;
case 6: clear();setBody(5); bodyType=5;break;
case 7: clear();setBody(8); bodyType=8;break;
case 8: clear();setBody(9); bodyType=9;break;
case 9: clear();setBody(10); bodyType=10;break;
case 10:clear();setBody(7); bodyType=7;break;
case 11:clear();setBody(12); bodyType=12;break;
case 12:clear();setBody(13); bodyType=13;break;
case 13:clear();setBody(14); bodyType=14;break;
case 14:clear();setBody(11); bodyType=11;break;
case 15:clear();setBody(16); bodyType=16;break;
case 16:clear();setBody(17); bodyType=17;break;
case 17:clear();setBody(18); bodyType=18;break;
case 18:clear();setBody(15); bodyType=15;break;
case 19:break;
case 20:break;
/* case 21:clear();setBody(1); bodyType=1;break;
case 22:clear();setBody(4); bodyType=4;break;
case 23:clear();setBody(6); bodyType=6;break;
*/
}
}
private void clear(){
for(int r=0;r<bodyInfo.length;r++)
for(int c=0;c<bodyInfo[r].length;c++)
bodyInfo[r][c]=0;
}
}
查色盲俄罗斯方块源码相关推荐
- PHP流量卡发货查单系统源码 流量卡物流发货运单号查询平台 一键安装版
介绍: PHP流量卡发货查单系统源码 流量卡物流发货运单号查询平台 一键安装版 5.新增后台填写客服QQ功能! 网盘下载地址: http://kekewl.cc/uQmbfXWuMHw 图片:
- unity3d俄罗斯方块源码教程+源码和程序下载
小时候,大家都应玩过或听说过<俄罗斯方块>,它是红白机,掌机等一些电子设备中最常见的一款游戏.而随着时代的发展,信息的进步,游戏画面从简单的黑白方块到彩色方块,游戏的玩法机制从最简单的消方 ...
- 曾经写的俄罗斯方块源码 2021-06-13
整理云盘时发现的曾经写的俄罗斯方块源码 用html + js + css写的,感觉到了自己曾经的骄傲 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...
- php导航收录源码,PHP最新响应式自动收录自带查反链导航源码
[温馨提示]源码包解压密码:www.youhutong.com 资源描述 PHP最新响应式自动收录自带查反链导航源码 源码介绍: 钥匙主题,基于flkc主题重写,功能:自动收录,自定义排序,自定义颜色 ...
- 俄罗斯方块源码分享 html+css+js
效果: [html+css+js]俄罗斯方块源码分享 这是在网上跟着黑马的视频做的,然后也加了些自己的想法. 在线试玩:http://www.beijiguang.site/game/index.ht ...
- 俄罗斯方块源码java
下载地址:俄罗斯方块项目源码.交流学习-Web服务器文档类资源-CSDN下载 俄罗斯方块源码,需要的下载 ├── Graphics │ ├── default │ │ ├── backgr ...
- python做俄罗斯方块手机版下载_Python俄罗斯方块源码
<Python俄罗斯方块源码>由会员分享,可在线阅读,更多相关<Python俄罗斯方块源码(10页珍藏版)>请在人人文库网上搜索. 1.Python俄罗斯方块源码诺基亚S60v ...
- 【SpringBoot实战】员工部门管理页面,增删改查,含源码
简介 基于SpringBoot,整合MyBatis, Hibernate, JPA, Druid, bootstrap, thymeleaf等,进行增删改查操作的Demo bootstrap-curd ...
- 增删查改html模板,dataGrid增删改查(EasyUI)示例源码
源码示例前台套用easyui,利用ajax调用sql数据库对学生信息表进行增删改查 资源下载此资源下载价格为2D币,请先登录 资源文件列表 GridDemos.sln , 907 JSonHelper ...
- (Java/JDBC)对MySQL数据库实现基础的增删改查操作(含源码)
文章目录 前言 注(常用PreparedStatement方法) 源码展示 前言 实现数据库连接 → Java连接MySQL数据库(含源码) (实现简单的增删查改更改正确的SQL语句即可) 增:ins ...
最新文章
- Knowledge Graph |(1)图数据库Neo4j简介与入门
- 40亿骚扰电话拨出,6亿用户隐私泄露,央视315曝光AI黑暗面
- 用python向mongodb插入数据_Python操作MongoDB数据库(一)
- 在Outlook 2007中查看您的Google日历
- python str translate,str.translate() --文本过滤和处理
- 微信分享接口调用(自测通过可以用)
- 【kafka】kafka单节点测试
- MySQL生产库主从重新同步操作注意事项
- A New Start
- C++字符串输入输出操作
- CHIP下游分析(仅ChIPseeker包)
- 延时电路c语言程序,rc延时电路工作原理
- 发现一本数学好书——重温微积分
- 基础的风光摄影技术控制
- android tf卡 修复工具,SD卡恢复修复工具RecoveRx 3.2中文免费版
- 积分,积分兑换,英语怎么说?
- Gerry-自定义报表组件
- 【翻译】CEDEC2014[跨越我的尸体2]跨越Stylized Rendering
- 数据与计算机通信实验报告,完整版通信工程专业综合实验报告
- python写用用户名密码程序_python写用’户登录程序‘的过程
热门文章
- 迅为工业级iMX6Q开发板全新升级兼容PLUS版本|四核商业级|工业级|双核商业级
- ‘scrapy‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。详细解决步骤来了
- 【计算机毕业设计】Java springboot大学生体质测评系统
- 数据结构-BitMap
- 大数据学习之数据仓库
- java定义形状类shape_c++ 形状类Shape(派生出圆类Circle和矩形类Rectangle)
- 贝叶斯算法、正向概率、逆向概率、先验概率、后验概率、单词拼写纠错实例
- ARM处理器异常返回地址
- 关于深度学习理论和架构的最新综述(参考文献)
- 21个省市今日高温 内蒙古最高温达44.5度