谁会做一个java游戏?
导入Java . awt . *;
导入Java . awt . event . *;
导入javax . swing . *;
公共类俄罗斯方块扩展JFrame {
公共俄罗斯方块(){
TetrisPanel a = new TetrisPanel();
addkey listener(a . listener);
增加(a);
JMenuBar menu = new JMenuBar();
JMenu game = new JMenu("游戏");
Game.add("新游戏");
game . add(" pause ");
game . add(" continue ");
game . add(" exit ");
JMenu help = new JMenu(" help ");
help . add(" about ");
menu.add(游戏);
menu.add(帮助);
this.setJMenuBar(菜单);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame。EXIT _ ON _ CLOSE);
setSize(220,275);
Settle(《俄罗斯方块beta》);
setResizable(false);
}
公共静态void main(String[] args) {
新俄罗斯方块()。set visible(true);
}
}
//创建一个俄罗斯方块类
TetrisPanel类扩展JPanel {
public timer listener listener = new timer listener();
// blockType代表盒类型。
// turnState表示盒子状态。
private int blockType
private int得分= 0;
private int turnState
私有int x;
私有int y;
int flag = 0;
//定义下拉框X = 0-11,Y = 0-21;
int[][]map = new int[13][23];
//正方形的形状第一组代表S、Z、L、J、I、O、t七种类型的正方形,第二组代表旋转多少次。第三和第四组是方阵。
私有final int shapes[][][]= new int[][][]{
//我
{ { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 } },
// S
{ { 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,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 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 } } };
//初始化构造方法
公共俄罗斯面板(){
next block();
new game();
新计时器(1000,监听器)。start();
}
//生成新方块的方法
私有void nextBlock() {
block type =(int)(math . random()* 1000)% 7;
turnState =(int)(math . random()* 1000)% 4;
x = 4;
y = 0;
if (crash(x,y,blockType,turnState) == 0) {
joptionpane . showmessagedialog(null,“游戏结束”);
new game();
}
}
//初始化地图
私有void newGame() {
得分= 0;
for(int I = 0;我& lt12;i++) {
for(int j = 0;j & lt22;j++) {
map[I][j]= 0;
map[11][j]= map[0][j]= 3;
}
map[I][21]= 3;
}
}
//旋转方法
私有空转(){
turnState = (crash(x,y,blockType,(turn state+1)% 4)+turn state)% 4;
repaint();
}
//向左移动的方法
私有void left() {
x -= crash(x - 1,y,blockType,turn state);
repaint();
}
//向右移动的方法
私有无效权利(){
x += crash(x + 1,y,blockType,turnState);
repaint();
}
//下落的方法
私有void down() {
y += crash(x,y + 1,blockType,turn state);
if (crash(x,y + 1,blockType,turnState) == 0) {
add(x,y,blockType,turn state);
next block();
}
repaint();
}
//方法是否冲突
private int crash(int x,int y,int blockType,int turnState) {
for(int a = 0;a & lt4;a++) {
for(int b = 0;b & lt4;b++) {
if((shapes[block type][turnState][a* 4+b]& amp;map[x + b + 1][y
+ a]) == 1) {
返回0;
}
}
}
返回1;
}
//试试消除线条的方法。
private void tryDelLine() {
for(int b = 0;b & lt21;b++) {
int c = 1;
for(int a = 0;a & lt12;a++) {
c & amp= map[a][b];
}
if (c == 1) {
得分+= 10;
for(int d = b;d & gt0;d - ) {
for(int e = 0;e & lt11;e++) {
map[e][d]= map[e][d-1];
}
}
}
}
}
//添加当前添加的地图
private void add(int x,int y,int blockType,int turnState) {
for(int a = 0;a & lt4;a++) {
for(int b = 0;b & lt4;b++) {
map[x+b+1][y+a]| = shapes[block type][turnState][a * 4+b];
}
}
try delline();
}
//画正方形的方法
public void paint component(Graphics g){
super . paint component(g);
//绘制当前正方形
for(int j = 0;j & lt16;j++) {
if(shapes[block type][turnState][j]= = 1){
g . fill rect((j % 4+x+1)* 10,(j / 4 + y) * 10,10,10);
}
}
//画一个固定的正方形。
for(int j = 0;j & lt22;j++) {
for(int I = 0;我& lt12;i++) {
if (map[i][j] == 1) {
g.fillRect(i * 10,j * 10,10,10);
} else if (map[i][j] == 3) {
g.drawRect(i * 10,j * 10,10,10);
}
}
}
g.drawString("score=" + score,125,10);
G.drawString("抵制不良游戏," 125,50);
G.drawString(“拒绝盗版游戏。”, 125, 70);
g .拉绳("注意自我保护," 125,90);
G.drawString(“谨防被骗。”, 125, 110);
G.drawString(“适度游戏益脑”,125,130);
G.drawString(“沉迷游戏伤身。”, 125, 150);
g .拉绳("合理安排时间," 125,170);
G.drawString(“享受健康生活。”, 125, 190);
}
//定时器监控和键盘监控
类TimerListener扩展KeyAdapter实现ActionListener {
public void action performed(action event e){
down();
}
公共void按键(按键事件e) {
switch (e.getKeyCode()) {
案例关键事件。VK _唐:
down();
打破;
案例关键事件。VK_UP:
转();
打破;
案例关键事件。VK右:
right();
打破;
案例关键事件。VK _左:
left();
}
}
}
}