java俄罗斯方块游戏代码
java俄罗斯方块游戏代码:
package com; import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random; import javax.swing.JFrame;
import javax.swing.JPanel; public class Eluos extends JFrame{ private Eluo_panel jPanel; private int this_width=500,this_height=500;
public Eluos(){ this.setSize(this_width, this_height); jPanel=new Eluo_panel();
this.add(jPanel); this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true); this.addKeyListener(new KeyListener() { @Override
public void keyTyped(KeyEvent e) {
} @Override
public void keyReleased(KeyEvent e) {
System.out.println("type");
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT: Eluos.this.jPanel.moveOther(Eluo_panel.MOVE_RIGHT, Eluos.this.jPanel.curt_xingzhuang);
break;
case KeyEvent.VK_RIGHT: Eluos.this.jPanel.moveOther(Eluo_panel.MOVE_LEFT, Eluos.this.jPanel.curt_xingzhuang);
break; case KeyEvent.VK_UP:
System.out.println(Eluos.this.jPanel.curt_xingzhuang);
Eluos.this.jPanel.curt_xingzhuang=
Eluos.this.jPanel.bianXing(Eluos.this.jPanel.fangkuai.d, Eluos.this.jPanel.curt_xingzhuang); break;
} } @Override
public void keyPressed(KeyEvent e) { }
}); } public static void main(String[] args) { new Eluos(); } } class Eluo_panel extends JPanel implements Runnable{ Fangkuai fangkuai; int huatu[][]=new int[20][20];
long now_time=0;
Random random=new Random();
Color color=new Color(0);
static final int MOVE_LEFT=1;
static final int MOVE_RIGHT=2; boolean game_over=false;
int curt_xingzhuang[][];
public Eluo_panel(){ fangkuai=createNewFangkui(); Thread thread=new Thread(this);
thread.start(); }
@Override
public void paint(Graphics g) {
super.paint(g); drawBack(g);
drawFangkui(g,curt_xingzhuang);
moveDown(curt_xingzhuang);
} /**
* 画背景
* @param g
*/
void drawBack(Graphics g){ for (int i = 0; i < huatu.length; i++) {
for (int j = 0; j < huatu[i].length; j++) {
if(huatu[i][j]!=0)
g.fillRect(j*20, i*20, Fangkuai.width-1,Fangkuai.height-1);
}
}
} /**
* 画一个方块
* @param g
* @param curt_xing
*/
void drawFangkui(Graphics g,int curt_xing[][]){ if(fangkuai==null)
{
fangkuai=createNewFangkui(); } if(curt_xing!=null){
int y=0;boolean b=false;
for (int i = 0; i < curt_xing.length; i++) {
for (int j = 0; j < curt_xing[i].length; j++) {
if(curt_xing[i][j]!=0)
{ g.setColor(fangkuai.getColor());
g.fillRect((fangkuai.run_x+j)*Fangkuai.width, (fangkuai.run_y+y)*Fangkuai.height,
Fangkuai.width-1, Fangkuai.height-1);
b=true; } }
if(b)
y++; } }
}
/**
* 创建一个方块
* @return
*/
private Fangkuai createNewFangkui(){ int index=0;
Random random=new Random();
Fangkuai fangkuai=new Fangkuai();
Color color=new Color(random.nextInt(255),
random.nextInt(255),random.nextInt(255)); index=random.nextInt(4);
fangkuai.setColor(color);
curt_xingzhuang=Fangkuai.xingzhuangs[index]; return fangkuai;
} /**
* 判断是否能够向下移动
* @param xingzhuang
* @return
*/
boolean isCan_down(int xingzhuang[][]){ int y=0;boolean b=false;
for (int i = 0; i < xingzhuang.length; i++) {
for (int j = 0; j < xingzhuang[i].length; j++) {
if(xingzhuang[i][j]!=0)
{
b=true;
if(fangkuai.run_y+y>=19||huatu[fangkuai.run_y+y+1][fangkuai.run_x+j]!=0){
return false;
} } }
if(b)
y++; } return true;
}
/**
* 变形
*/ public int[][] bianXing(int d,int arr[][]){ if(arr==null||arr[0]==null)
return null; int arr2[][]=new int[arr.length][arr[0].length]; switch (d) {
case 1: for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr2[j][arr[i].length-1-i]=arr[i][j];
}
} break; default:
break;
} for (int i = 0; i < arr2.length; i++) { for (int j = 0; j < arr2[i].length; j++) { if(arr2[i][j]!=0)
{
if(fangkuai.run_x+j>19||fangkuai.run_y+i>19||fangkuai.run_x+i<0
||huatu[fangkuai.run_y+i][fangkuai.run_x+j]!=0)
return arr;
}
}
} return arr2; }
/**
* 向下移动
* @param xingzhuang
*/
private void moveDown(int xingzhuang[][]){ if(isCan_down(xingzhuang))
fangkuai.run_y++; else
{ /**
* 如果不能向下移动就把当前方块的0和1 映射到整个面板上,重新创建一个方块
*/
int y=0;boolean b=false;
for (int i = 0; i < xingzhuang.length; i++) {
for (int j = 0; j < xingzhuang[i].length; j++) {
if(xingzhuang[i][j]!=0)
{
huatu[fangkuai.run_y+y][fangkuai.run_x+j]=1;
b=true;
} }
if(b)
y++; } xiaoChu();
for (int i = 0; i < huatu[0].length; i++) {
if(huatu[0][i]!=0)
game_over=true;
} fangkuai=createNewFangkui();
} }
public void xiaoChu(){ boolean xiao=false; for (int i = huatu.length-1; i >=0; i--) { xiao=false;
int j=0;
for ( j = 0; j < huatu[i].length; j++) {
if(huatu[i][j]==0)
break;
} if(j==huatu[i].length)
xiao=true; if(xiao){ for ( j = i; j >0; j--) {
for (int j2 = 0; j2 < huatu[j].length; j2++) {
huatu[j][j2]=huatu[j-1][j2];
}
}
for ( j = 0; j <huatu[0].length; j++) {
huatu[0][j]=0;
} } }
}
/**
* http://www.cnblogs.com/roucheng/
* @param d
* @param xingzhuang
*/
void moveOther(int d,int xingzhuang[][]){ int dx=d==MOVE_LEFT?1:-1;
if(is_CanMoveOther(d, xingzhuang)){
fangkuai.run_x+=dx;
}
}
private boolean is_CanMoveOther(int d,int xingzhuang[][]){ int dx=d==MOVE_LEFT?1:-1;
int y=0;boolean has=false;
for (int i = 0; i < xingzhuang.length; i++) {
has=false;
for (int j = 0; j < xingzhuang[i].length; j++) { if(xingzhuang[i][j]!=0)
{
if(d==MOVE_LEFT&&fangkuai.run_x+j>=19||d==MOVE_RIGHT&&fangkuai.run_x+j<=0) return false;
has=true;
if(huatu[fangkuai.run_y+y][fangkuai.run_x+j+dx]!=0){
return false;
}
}
}
if(has)
y++;
} return true;
} @Override
public void run() { while(!game_over)
{ this.repaint();
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
class Fangkuai { private Color color; int run_x=10,run_y; int d=1; static final int width=20,height=20; public static final int xingzhuangs[][][]={
{{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
{0,0,0,0,1,1,1,1},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}}, {{0,0,1,0},{0,1,1,1},{0,0,0,0},{0,0,0,0}},//土形
{{0,0,0,0},{1,0,0,0},{1,1,0,0},{0,1,0,0}},
{{1,1,1,1},{1,0,0,0},{0,0,0,0},{0,0,0,0}},//T形
{{1,1},{1,1}} }; public Color getColor() {
return color;
} public void setColor(Color color) {
this.color = color;
} public int getRun_x() {
return run_x;
} public void setRun_x(int run_x) {
this.run_x = run_x;
} public int getRun_y() {
return run_y;
} public void setRun_y(int run_y) {
this.run_y = run_y;
} }
java俄罗斯方块游戏代码的更多相关文章
- Javascript 俄罗斯方块 游戏代码解释!
俄罗斯方块代码说明 /** 名称:Javascript 俄罗斯方块! 作者:Gloot 邮箱:glootz@gmail.com QQ:345268267 网站:http://www.cnblogs.c ...
- java小游戏代码
一. 需求分析 曾几何时,游戏是海洛因的代名词,让人与玩物丧志联系在一起.一度遭到社会反感和家长抵制.可是.随着互联网的发展,和游戏潜在优点被发现.游戏的价值開始逐渐被社会认可,人们開始接受.认识和了 ...
- C++编写简单的俄罗斯方块游戏
代码地址如下:http://www.demodashi.com/demo/14593.html C++编写简单的俄罗斯方块游戏 使用C++编写一个简单的俄罗斯方块游戏. 1 环境要求 使用C++图形库 ...
- 俄罗斯方块游戏 --- java
俄罗斯方块游戏 如有疑问请查看:http://zh.wikipedia.org/zh-tw/%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97 更多疑问请参考: ...
- 教你看懂网上流传的60行JavaScript代码俄罗斯方块游戏
早就听说网上有人仅仅用60行JavaScript代码写出了一个俄罗斯方块游戏,最近看了看,今天在这篇文章里面我把我做的分析整理一下(主要是以注释的形式). 我用C写一个功能基本齐全的俄罗斯方块的话,大 ...
- 俄罗斯方块游戏JavaScript代码
JavaScript代码俄罗斯方块游戏 早就听说网上有人仅仅用60行JavaScript代码写出了一个俄罗斯方块游戏,最近看了看,今天在这篇文章里面我把我做的分析整理一下(主要是以注释的形式). 我用 ...
- 经典 HTML5 & Javascript 俄罗斯方块游戏
Blockrain.js 是一个使用 HTML5 & JavaScript 开发的经典俄罗斯方块游戏.只需要复制和粘贴一段代码就可以玩起来了.最重要的是,它是响应式的,无论你的显示屏多么宽都能 ...
- 从零开始---控制台用c写俄罗斯方块游戏(1)
从零开始---控制台用c写俄罗斯方块游戏(1) 很少写博文,一来自身知识有限,二来自己知道,已经有很多这样的博文了,三就是因为懒,文笔也一般,四来刚出来工作,时间也不多 之所以写这篇博文,是因为应群里 ...
- 1.cocos2dx存储卡的游戏代码、而游戏移植到“华为荣耀”电话、问题的总结移植
1记忆卡片游戏代码 CardItem.h #pragmaonce #ifndef__CardItem_H__ #define__CardItem_H__ #include"cocos2 ...
随机推荐
- VirtualBox的四种网络连接方式
VirtualBox中有4中网络连接方式:a. NAT 网络地址转换模式(Network Address Translation)b. Bridged ...
- GPUImage 内置滤镜解析
#pragmamark - 调整颜色 Handle Color GPUImageBrightnessFilter //亮度GPUImageExposureFilter //曝光GPUImageCont ...
- JSON处理
var ajax = function () { mui.ajax(projectPath+'/goods/goodsprice.do', { dataType: 'json', type: 'pos ...
- mongodb_查询操作使用_条件查询、where子句等(转)
<?php /* mongodb_查询操作使用_条件查询.where子句等(转并学习) 1.find()/findOne() mongodb数据库的查询操作即使用find()或者findO ...
- 【笔记】《DirectX 9.0 3D游戏开发编程基础》:Direct3D初始化
Direct3D初始化大概分为4个步骤: 1.获取接口IDirect3D9的指针.(Direct3DCreate9函数调用). 该接口用户获取系统中物理硬件设备的信息并创建接口IDirect3DDev ...
- Linux下动态链接库 与gcc 选项
-L 编译时查找动态链接库的路径 -lxxx(小写) e.g -lcudart = link libcudart.so , -I(大写) 头文件的路径 -rpath (-R), 编译时指定链接 ...
- (转)Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条(三十一)
异步任务相信大家应该不会陌生,那么本章内容MOMO将带领大家学习Unity中的一些异步任务.在同步加载游戏场景的时候通常会使用方法 Application.LoadLevel(“yourScene ...
- 【转载】S2SH
说说最多人用的SSH或SSI吧,现在用的比较多的应该就是struts2.x+spring3.X+hibernate4.X或hibernate3.X了吧,mybatis用的人也有,方便有DBA的公司. ...
- Java知多少(106)程序与数据库连接
一个网络关系数据库应用系统是一个三层次结构.客户机与服务器采用网络连接,客户机端应用程序按通信协议与服务器端的数据库程序通信:数据库服务程序通过SQL命令与数据库管理系统通信. Java程序与数据库连 ...
- WWDC2015 结束.新一波更新以及bug即将来袭.
WWDC结束.新一波更新以及bug即将来袭. HTTPS 将成为标准链接. http被报错. GamePlayKit 这是搞那样. 还有ReplayKit 那些什么录像分享什么的还有活路么? Mod ...