仙剑之十里坡
标题:
七夕小雨教程系列java_swing俄罗斯方块教程[1]
[打印本页]
作者:
七夕小雨
时间:
2011-5-18 14:55
标题:
七夕小雨教程系列java_swing俄罗斯方块教程[1]
http://player.youku.com/player.php/sid/XMjY3Nzc2NTM2/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.*;
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];
Random r = new Random();
boolean start=false;
public gameWin() {
for (int i = 0; i < act.length; i++) {
act
= new gameAct();
}
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);
}
}
}
private boolean newAct() {
int temp = r.nextInt(7);
System.out.println(temp);
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;
}
return true;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==newGame) {
if (e.getActionCommand().equals("开始")) {
newGame.setText("重置");
requestFocus(true);
start = true;
if (!newAct()) {
//这里是游戏失败的情况;
return;
}else {
repaint();
}
}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();
}
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<0||y>=18) {
return false;
}
return true;
}
public void keyPressed(KeyEvent e) {
if (start) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
Move(0, 1);
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) { }
}
gameAct.class
package eLuoSiFangKuai;
public class gameAct {
int x;
int y;
}
作者:
御剑天河
时间:
2011-5-18 16:53
肯定有 下 ~~~
作者:
残阳泪珀
时间:
2011-5-18 17:08
可以写个论文了 哈哈
作者:
七夕小雨
时间:
2011-5-18 17:53
- -最近在用java做植物大战僵尸,我就先用这种方法把我之前做过的项目总结一下-v-
作者:
lzk222
时间:
2011-9-30 00:49
多谢,学着好方便。
欢迎光临 仙剑之十里坡 (http://palslp.com/BBS/)
Powered by Discuz! X2.5