2014-07-19java小成绩
来源:互联网 发布:自动阅读软件 编辑:程序博客网 时间:2024/06/11 20:25
学习java到现在已经有一段时间了,下面是我走的一个小游戏,只为记录自己的成长
这是自己写的一个小游戏——俄罗斯方块
下面是源代码:
package edu.ahjzu.com.controller;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import edu.ahjzu.com.listenter.ShapeListenter;
import edu.ahjzu.com.zheng.*;
import edu.ahjzu.com.zheng.view.GamePanel;
public class Controller extends KeyAdapter implements ShapeListenter {
private Shape shape;
private ShapeFactory shapefactory;
private Ground ground;
private GamePanel gamepanel;
public Controller(ShapeFactory shapefactory, Ground ground,
GamePanel gamepanel) {
// this.shape = shape;
this.shapefactory = shapefactory;
this.ground = ground;
this.gamepanel = gamepanel;
}
public void shapeMoveDown(Shape shape) {
gamepanel.display(ground, shape);
}
public synchronized boolean isShapeMoveDownable(Shape shape) {
if(this.shape!=shape){
return false;
}
if (ground.isMoveable(shape, Shape.DOWN))
return true;
ground.accpet(this.shape);
if (!ground.isFull()) {
this.shape = shapefactory.getShape(this);
}
return false;
}
public void newGame() {
shape = shapefactory.getShape(this);
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
// super.keyPressed(e);
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
if (ground.isMoveable(shape, Shape.ROTATE))
shape.rotate();
break;
case KeyEvent.VK_DOWN:
if (isShapeMoveDownable(shape))
shape.moveDown();
break;
case KeyEvent.VK_LEFT:
if (ground.isMoveable(shape, Shape.LEFT))
shape.moveLeft();
break;
case KeyEvent.VK_RIGHT:
if (ground.isMoveable(shape, Shape.RIGHT))
shape.moveRight();
break;
}
gamepanel.display(ground, shape);
}
}
-----------------------------------------------------------------------------------------
package edu.ahjzu.com.listenter;
import edu.ahjzu.com.zheng.Shape;
public interface ShapeListenter {
void shapeMoveDown(Shape shape);
boolean isShapeMoveDownable(Shape shape);
}
--------------------——————————-----------
package edu.ahjzu.com.test;
import javax.swing.JFrame;
import edu.ahjzu.com.controller.Controller;
import edu.ahjzu.com.zheng.Ground;
import edu.ahjzu.com.zheng.ShapeFactory;
import edu.ahjzu.com.zheng.view.GamePanel;
public class Game {
public static void main(String[] args) {
// TODO Auto-generated method stub
ShapeFactory shapeFactory = new ShapeFactory();
Ground ground = new Ground();
GamePanel gamePanel = new GamePanel();
Controller controller = new Controller(shapeFactory, ground, gamePanel);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(gamePanel.getSize().width + 16,
gamePanel.getSize().height + 35);
frame.add(gamePanel);
gamePanel.addKeyListener(controller);
frame.addKeyListener(controller);
frame.setVisible(true);
controller.newGame();
}
}
-----------------------------------------------------------------------------------------
package edu.ahjzu.com.util;
public class Global {
public static final int CELL_SIZE = 20;
public static final int WIDTH = 15;
public static final int HEIGHT = 15;
}
------------------------------------------------------------------------------------
package edu.ahjzu.com.zheng;
import java.awt.Graphics;
import edu.ahjzu.com.util.Global;
public class Ground {// 障礙物
private int[][] obstacles = new int[Global.WIDTH][Global.HEIGHT];
public void accpet(Shape shape) {
System.out.println("Ground's accpet");
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (shape.isMember(x, y, false)) {
obstacles[shape.getLeft() + x][shape.getTop() + y] = 1;
}
}
}
for (int i = 0; i < 4; i++) {
deleteFullLine();
}
}
public void drawMe(Graphics g) {
System.out.println("Ground's drawMe");
for (int x = 0; x < Global.WIDTH; x++) {
for (int y = 0; y < Global.HEIGHT; y++) {
if (obstacles[x][y] == 1) {
g.fill3DRect(x * Global.CELL_SIZE, y * Global.CELL_SIZE,
Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}
}
}
public void deleteFullLine() {
boolean full = true;
for (int y = Global.HEIGHT - 1; y >= 0; y--) {
for (int x = 0; x < Global.WIDTH; x++) {
if (obstacles[x][y] == 0) {
full = false;
}
}
if (full) {
deleteLine(y);
}
}
}
public void deleteLine(int lineNumber) {
for (int y = lineNumber; y > 0; y--) {
for (int x = 0; x < Global.WIDTH; x++) {
obstacles[x][y] = obstacles[x][y - 1];
}
}
for (int x = 0; x < Global.WIDTH; x++) {
obstacles[x][0] = 0;
}
}
public boolean isMoveable(Shape shape, int action) {
int left = shape.getLeft();
int top = shape.getTop();
switch (action) {
case Shape.LEFT:
left--;
break;
case Shape.RIGHT:
left++;
break;
case Shape.DOWN:
top++;
break;
}
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++) {
if (shape.isMember(x, y, action == Shape.ROTATE)) {
if (top + y >= Global.HEIGHT || left + x < 0
|| left + x >= Global.WIDTH
|| obstacles[left + x][top + y] == 1)
return false;
}
}
return true;
}
public boolean isFull() {
for (int x = 0; x < Global.WIDTH; x++) {
if (obstacles[x][0] == 1) {
return true;
}
}
return false;
}
}
--------------------------------------------------------------------
package edu.ahjzu.com.zheng;
import java.awt.Color;
import java.awt.Graphics;
import edu.ahjzu.com.listenter.*;
import edu.ahjzu.com.util.Global;
public class Shape {
public static final int ROTATE = 0;
public static final int LEFT = 1;
public static final int RIGHT = 2;
public static final int DOWN = 3;
private int[][] body;
private int status;
private int top;
private int left;
private ShapeListenter listener;
public void setBody(int body[][]) {
this.body = body;
}
public void setStatus(int status) {
this.status = status;
}
public void moveLeft() {
System.out.println("shape's moveleft");
left--;
}
public void moveRight() {
System.out.println("shape's moverighe");
left++;
}
public void moveDown() {
System.out.println("shape's movedown");
top++;
}
public void rotate() {
System.out.println("shape's rotate");
status = (status + 1) % body.length;
}
public void drawMe(Graphics g) {
System.out.println("shape's drawMe");
g.setColor(Color.PINK);
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++) {
if (getFlagByPoint(x, y)) {
g.fill3DRect((left + x) * Global.CELL_SIZE, (top + y)
* Global.CELL_SIZE, Global.CELL_SIZE,
Global.CELL_SIZE, true);
}
}
}
private boolean getFlagByPoint(int x, int y) {
return body[status][y * 4 + x] == 1;
}
public boolean isMember(int x, int y, boolean rotate) {
//判断图形旋转,没或旋转后的位置是否为1,是true,否false
int tempStatus = status;
if (rotate) {
tempStatus = (tempStatus + 1) % body.length;
}
return body[tempStatus][y * 4 + x] == 1;
}
private class ShapeDriver implements Runnable {
public void run() {
while (listener.isShapeMoveDownable(Shape.this)) {
moveDown();
listener.shapeMoveDown(Shape.this);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public Shape() {
new Thread(new ShapeDriver()).start();
}
public void addShapeListenter(ShapeListenter l) {
if (null != l)
this.listener = l;
}
public int getTop() {
return top;
}
public int getLeft() {
return left;
}
}
---------------------------------------------------------------------------------
package edu.ahjzu.com.zheng;
import java.util.Random;
import edu.ahjzu.com.listenter.ShapeListenter;
public class ShapeFactory {
private int shapes[][][] = new int[][][] {
{ { 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 } },
{ { 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
},
{ { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 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, 0 },
{ 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
{ { 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0, 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 },
{ 1, 1, 1, 1, 0, 0, 0, 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 },
{ 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
};
public Shape getShape(ShapeListenter listenter) {
System.out.println("FactoryShape's getShape");
Shape shape = new Shape();
shape.addShapeListenter(listenter);
int type = new Random().nextInt(shapes.length);
shape.setBody(shapes[type]);
shape.setStatus(new Random().nextInt(4));
return shape;
}
}
-----------------------------------------------------------------------------------
package edu.ahjzu.com.zheng.view;
import java.awt.Color;
import java.awt.Graphics;
import edu.ahjzu.com.util.Global;
import edu.ahjzu.com.zheng.*;
import javax.swing.JPanel;
public class GamePanel extends JPanel {
private Ground ground;
private Shape shape;
public void display(Ground ground, Shape shape) {
this.ground = ground;
this.shape = shape;
System.out.println("GamePanel's display");
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
// super.paintComponent(g);
// 重新显示
g.setColor(new Color(0xcfcfcf));
//g.setColor(Color.BLACK);
g.fillRect(0, 0, Global.WIDTH * Global.CELL_SIZE, Global.HEIGHT
* Global.CELL_SIZE);
if (shape != null && ground != null) {
shape.drawMe(g);
ground.drawMe(g);
}
}
public GamePanel() {
this.setSize(Global.WIDTH * Global.CELL_SIZE, Global.HEIGHT
* Global.CELL_SIZE);
}
}
以上是自己的成果,谢谢,继续努力
- 2014-07-19java小成绩
- 自己写的java学生成绩评定小程序
- JAVA小程序(0级)-- 学生成绩管理
- 学生成绩小管理
- 成绩录入修改小程序
- 成绩分级,MapInfo小程序
- C#小程序--成绩分析
- 小实例-实现对成绩总分由高到低的排序-Java中数组对象的排序
- java小项目之成绩管理、排课软件、局域网聊天软件
- java简易成绩管理程序
- java 成绩处理
- java 成绩统计
- java 缓存--查成绩
- java之成绩总结
- JAVA成绩分析
- Java:学生成绩转换
- Java 成绩排序
- 2008-03-27 | 一点小成绩
- TeamViewer经验介绍
- POJ 1837 Balance DP
- OpenEIM模式中的里氏代换原则【策略模代】
- Codeforces Round #1C Ancient Berland Circus
- 区间Dp 暴力枚举+动态规划 Hdu1081
- 2014-07-19java小成绩
- Corporative Network +uvalive+并查集
- 广义霍夫变换GHT
- CF 256# D. Multiplication Table 二分
- C++ Primer Plus第六版 第七章 编程练习答案
- teamviewer原理和阻止方法
- 大数相乘
- 将整形转化为字符串
- POJ 3254 Corn Fields 状压DP