仙剑之十里坡
标题:
七夕小雨教程系列java_swing俄罗斯方块教程[2]
[打印本页]
作者:
七夕小雨
时间:
2011-5-18 18:03
标题:
七夕小雨教程系列java_swing俄罗斯方块教程[2]
http://player.youku.com/player.php/sid/XMjY4MDExNzEy/v.swf
这一讲的三个类
gameMain.class
package eLuoSiFangKuai;
import javax.swing.*;
public class gameMain extends JFrame{
public gameMain() {
super("俄罗斯方块1.0V---七夕小雨作品");
gameWin g = new gameWin();
add(g);
setSize(380,420);
setLocation(200, 200);
setVisible(true);
}
public static void main(String[] args) {
new gameMain();
}
}
gameWin.class
package eLuoSiFangKuai;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
public class gameWin extends JPanel implements ActionListener,KeyListener{
JButton newGame = new JButton("开始");
JButton endGame = new JButton("退出");
int fenShu=0,speed=0;
gameAct[] act = new gameAct[4];
gameAct[] actTemp = new gameAct[4];
Random r = new Random();
boolean start=false;
int[][] map = new int[10][18];
Timer t;int temp;
public gameWin() {
for (int i = 0; i < act.length; i++) {
act
= new gameAct();
}
for (int i = 0; i < act.length; i++) {
actTemp
= new gameAct();
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 18; j++) {
map
[j] = 0;
}
}
setLayout(new FlowLayout(FlowLayout.RIGHT));
add(newGame);
add(endGame);
newGame.addActionListener(this);
endGame.addActionListener(this);
addKeyListener(this);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(9, 10, 200, 360);
g.drawString("分数"+fenShu, 220, 60);
g.drawLine(220, 65, 360, 65);
g.drawString("速度"+speed, 220, 90);
g.drawLine(220, 95, 360, 95);
g.drawString("下一个方块", 250, 120);
g.drawString("俄罗斯方块1.0v", 220, 340);
g.drawString("七夕小雨", 220, 360);
////////////////////////
if (start) {
g.setColor(new Color(255,0,0));
for (int i = 0; i < 4; i++) {
g.fillRect(10+act
.x*20, 10+act
.y*20, 20, 20);
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j <18; j++) {
if (map
[j]==1) {
g.fillRect(10+i*20, 10+j*20, 20, 20);
}
}
}
g.setColor(new Color(0,0,255));
for (int i = 0; i < 4; i++) {
g.fillRect(220+actTemp
.x*20, 160+actTemp
.y*20, 20, 20);
}
}
}
private boolean newAct() {
switch (temp) {
case 0:
act[0].x =3;act[0].y=0;
act[1].x =4;act[1].y=0;
act[2].x =2;act[2].y=1;
act[3].x =3;act[3].y=1;
break;
case 1:
act[0].x =1;act[0].y=0;
act[1].x =2;act[1].y=0;
act[2].x =3;act[2].y=0;
act[3].x =4;act[3].y=0;
break;
case 2:
act[0].x =2;act[0].y=0;
act[1].x =3;act[1].y=0;
act[2].x =2;act[2].y=1;
act[3].x =3;act[3].y=1;
break;
case 3:
act[0].x =2;act[0].y=0;
act[1].x =2;act[1].y=1;
act[2].x =2;act[2].y=2;
act[3].x =3;act[3].y=2;
break;
case 4:
act[0].x =3;act[0].y=0;
act[1].x =3;act[1].y=1;
act[2].x =3;act[2].y=2;
act[3].x =2;act[3].y=2;
break;
case 5:
act[0].x =3;act[0].y=0;
act[1].x =2;act[1].y=1;
act[2].x =3;act[2].y=1;
act[3].x =4;act[3].y=1;
break;
case 6:
act[0].x =2;act[0].y=0;
act[1].x =3;act[1].y=0;
act[2].x =3;act[2].y=1;
act[3].x =4;act[3].y=1;
break;
}
for (int i = 0; i < 4; i++) {
if (maxYes(act
.x, act
.y)) {
return false;
}
}
return true;
}
public void nextAct(){
switch (temp) {
case 0:
actTemp[0].x =3;actTemp[0].y=0;
actTemp[1].x =4;actTemp[1].y=0;
actTemp[2].x =2;actTemp[2].y=1;
actTemp[3].x =3;actTemp[3].y=1;
break;
case 1:
actTemp[0].x =1;actTemp[0].y=0;
actTemp[1].x =2;actTemp[1].y=0;
actTemp[2].x =3;actTemp[2].y=0;
actTemp[3].x =4;actTemp[3].y=0;
break;
case 2:
actTemp[0].x =2;actTemp[0].y=0;
actTemp[1].x =3;actTemp[1].y=0;
actTemp[2].x =2;actTemp[2].y=1;
actTemp[3].x =3;actTemp[3].y=1;
break;
case 3:
actTemp[0].x =2;actTemp[0].y=0;
actTemp[1].x =2;actTemp[1].y=1;
actTemp[2].x =2;actTemp[2].y=2;
actTemp[3].x =3;actTemp[3].y=2;
break;
case 4:
actTemp[0].x =3;actTemp[0].y=0;
actTemp[1].x =3;actTemp[1].y=1;
actTemp[2].x =3;actTemp[2].y=2;
actTemp[3].x =2;actTemp[3].y=2;
break;
case 5:
actTemp[0].x =3;actTemp[0].y=0;
actTemp[1].x =2;actTemp[1].y=1;
actTemp[2].x =3;actTemp[2].y=1;
actTemp[3].x =4;actTemp[3].y=1;
break;
case 6:
actTemp[0].x =2;actTemp[0].y=0;
actTemp[1].x =3;actTemp[1].y=0;
actTemp[2].x =3;actTemp[2].y=1;
actTemp[3].x =4;actTemp[3].y=1;
break;
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==newGame) {
if (e.getActionCommand().equals("开始")) {
newGame.setText("重置");
requestFocus(true);
start = true;
temp = r.nextInt(7);
if (!newAct()) {
t = new Timer(1000-(100*speed),new myTimer());
t.start();
temp = r.nextInt(7);
nextAct();
repaint();
}else {
//这里是游戏失败的情况;
return;
}
}else {
start = false;
newGame.setText("开始");
}
}
if (e.getSource()==endGame) {
System.exit(0);
}
}
public void Move(int x, int y) {
if (minYes(x, y)) {
for (int i = 0; i < 4; i++) {
act
.x+=x;
act
.y+=y;
}
}
repaint();
}
private void down() {
if (minYes(0, 1)) {
for (int i = 0; i < 4; i++) {
act
.y+=1;
}
repaint();
}else {
for (int i = 0; i < 4; i++) {
map[act
.x][act
.y]=1;
}
if (!newAct()) {
temp = r.nextInt(7);
nextAct();
}else {
System.out.println("挂了");
//挂掉了!= .=
}
repaint();
}
}
public boolean minYes(int x, int y){
for (int i = 0; i < 4; i++) {
if (!maxYes(act
.x+x, act
.y+y)) {
return false;
}
}
return true;
}
public boolean maxYes(int x, int y){
if (x<0||x>=10||y<-1||y>=18) {
return false;
}
if (map[x][y]==1) {
return false;
}
return true;
}
public void keyPressed(KeyEvent e) {
if (start) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
down();
break;
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_LEFT:
Move(-1, 0);
break;
case KeyEvent.VK_RIGHT:
Move(1, 0);
break;
default:
break;
}
}
}
public void keyReleased(KeyEvent arg0) { }
public void keyTyped(KeyEvent arg0) { }
public class myTimer implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (start) {
down();
}
}
}
}
gameAct.class
package eLuoSiFangKuai;
public class gameAct {
int x;
int y;
}
作者:
御剑天河
时间:
2011-5-18 20:11
话说发了这么多,我都不知道发得是什么
作者:
七夕小雨
时间:
2011-5-18 21:43
说实话,我也没有指望十里坡除了残阳以外的人能看懂了~~我就算发基础教程都不一定能懂,更不用说这种非0基础教程了
作者:
七千
时间:
2011-5-18 22:07
回复
3#
七夕小雨
于是就不能发点普及性的么》。。我是个刚学完函数的娃啊- -
作者:
残阳泪珀
时间:
2011-5-18 23:57
回复
4#
七千
又不是知识普及的论坛 想学的话自己去学java 不想学的话不学也无妨 iOS是C-Objective开发的 Android支持JAVA和C++ 所以学好C语言其实很多东西就豁然开朗了 再说编程语言重在编程思想 语法什么的差别也不是很大
作者:
残光剑决
时间:
2011-5-25 20:23
编程……汗……只学过QBX,还没学全……
作者:
独孤琳心
时间:
2011-5-25 20:33
额。。。只想知道这个编程用来干嘛的?
欢迎光临 仙剑之十里坡 (http://palslp.com/BBS/)
Powered by Discuz! X2.5