java俄罗斯方块完整代码
package tes;
import java.awt.Image;
class Cell {//定义属性
private int row;//行
private int col;//列
private Image image;//格子的贴图
public Cell() {
}
public Cell(int row, int col, Image image) {//构造器super();
this.row = row;
this.col = col;
this.image = image;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public void moveRight(){
col++;
}
public void moveLeft(){
col--;
}
public void moveDown(){
row++;
}
public String toString() {
return this.row + "," + this.col;
}
}
package tes;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 俄罗斯方块类
*/
public class Tetris extends JPanel {//让Tetris继承JPanel private int score;//分数
private int lines;//行数
private Cell[][] wall;//墙
private Tetromino tetromino;//下落的方块
private Tetromino nextOne;//下一个方块
private static BufferedImage background;
private static BufferedImage overImage;
public static BufferedImage T;
public static BufferedImage I;
public static BufferedImage J;
public static BufferedImage L;
public static BufferedImage S;
public static BufferedImage Z;
public static BufferedImage O;
public static final int ROWS = 20;//初始化行
public static final int COLS = 10;//初始化列
public static final int CELL_SIZE = 32;//格子尺寸
private Timer timer;//定时器
private boolean pause;//暂停
private boolean gameOver;//结束
private long interval = 600l;//间隔时间
public static final int FONT_COLOR = 0x667799;//字体颜色public static final int FONT_SIZE = 30;//字体大小
//使用静态代码块加载静态的图片
static {
try {
background = ImageIO.read(Tetris.class.getResource("ttetris.png"));
overImage = ImageIO.read(Tetris.class.getResource("GAMEOVER.png"));
T = ImageIO.read(Tetris.class.getResource("tT.png"));
I = ImageIO.read(Tetris.class.getResource("II.png"));
S = ImageIO.read(Tetris.class.getResource("SS.png"));
Z = ImageIO.read(Tetris.class.getResource("ZZ.png"));
J = ImageIO.read(Tetris.class.getResource("JJ.png"));
L = ImageIO.read(Tetris.class.getResource("LL.png"));
O = ImageIO.read(Tetris.class.getResource("OO.png"));
}catch(Exception e) {
e.printStackTrace();
}
}
//重写父类JPanel方法,重写paint()修改原有绘制方法
public void paint(Graphics g) {
//画背景
g.drawImage(background,9,9,null);
//平移坐标系
g.translate(15,15);
//画墙
paintWall(g);
//画下落的方块
paintTetromino(g);
//画下一个方块
paintNextOne(g);
//画分数
paintScore(g);
if(gameOver) {
g.drawImage(overImage,0,0,null);
}
}
//画分数
private void paintScore(Graphics g) {
int x = 390;//基线位置
int y = 190;//基线位置
g.setColor(new Color(FONT_COLOR));//颜色
Font font = g.getFont();//取得g当前字体
font = new Font(font.getName(),font.getStyle(),FONT_SIZE);//设置字体
g.setFont(font);//更改g当前字体
String str = "SCORE:"+score;
g.drawString(str, x, y);
//画行数
y += 80;
str = "LINES::"+lines;
g.drawString(str, x, y);
//画提示
y += 80;
str = "[P]Pause";
if(pause) {
str = "[C]Contine";
}
if(gameOver) {
str = "[S]Strat";
}
g.drawString(str, x, y);
}
//画下一个方块
private void paintNextOne(Graphics g) {
if(nextOne==null) {//如果没有正在下落的方块,结束
return;
}
//将每个格子的row,col换算成x,y然后贴图
Cell[] cells = nextOne.cells;
for(int i=0; i Cell cell = cells[i];//cell每一个格子 int x = (cell.getCol()+10)*CELL_SIZE;//向右移动10的位置 int y = (cell.getRow()+1)*CELL_SIZE;//向下移动1的位置 g.drawImage(cell.getImage(),x-1,y-1,null);//贴图 } } //画下落的方块 private void paintTetromino(Graphics g) { if(tetromino==null) {//如果没有正在下落的方块,结束 return; } //将每个格子的row,col换算成x,y然后贴图 Cell[] cells = tetromino.cells; for(int i=0; i Cell cell = cells[i];//cell每一个格子 int x = cell.getCol()*CELL_SIZE;//计算长度 int y = cell.getRow()*CELL_SIZE;//计算长度 g.drawImage(cell.getImage(),x-1,y-1,null);//贴图 } } //画墙 private void paintWall(Graphics g) { for(int row=0; row Cell[] line = wall[row];//line代表墙上的每一行 for(int col=0; col Cell cell = line[col];//cell代表墙上的每个格子 int x = col*CELL_SIZE;//计算长度 int y = row*CELL_SIZE;//计算长度 if(cell==null) { g.drawRect(x, y, CELL_SIZE, CELL_SIZE);//没格子画框 }else { g.drawImage(cell.getImage(),x-1,y-1,null);//有格子贴图 } } } } //添加启动方法 public void action() { wall = new Cell[ROWS][COLS]; //启动 startAction(); //处理键盘按下事件 KeyAdapter l = new KeyAdapter(){ //key 按键Pressed 按下了 public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if(key==KeyEvent.VK_Q) {//如果按Q System.exit(0);//结束Java进程 } if(gameOver) {//如果游戏结束 if(key==KeyEvent.VK_S) {//如果按S startAction();//重新开始 repaint(); } return; } if(pause) {//如果暂停 if(key==KeyEvent.VK_C) {//如果按C continueAction();//继续 repaint(); } return;//按其他都执行暂停 } switch (key) { case KeyEvent.VK_DOWN : softDropAction(); break; case KeyEvent.VK_LEFT : moveLeftAction(); break; case KeyEvent.VK_RIGHT : moveRightAction(); break; case KeyEvent.VK_SPACE : hardDropAction(); break; case KeyEvent.VK_UP : rotateRightAction(); break; case KeyEvent.VK_P ://暂停 pauseAction(); break; } repaint();//再画一次 } }; //绑定事件到当前面板 this.requestFocus(); this.addKeyListener(l); } //向右移动的流程控制 public void moveRightAction() { //尝试先向右移动,如果发现超出边界,就向左移动,修正回来 tetromino.moveRight(); if(outOfBounds()||coincide()) {//出界或者重合 tetromino.moveLeft(); } } //向左移动的流程控制 public void moveLeftAction() { //尝试先向右移动,如果发现超出边界,就向左移动,修正回来 tetromino.moveLeft(); if(outOfBounds()||coincide()) {//出界或者重合 tetromino.moveRight(); } } //检查当前正在下落的方块是否出界了 //迭代正在下落的方块,其中某一个格子列坐标出界,就移回 private boolean outOfBounds() { Cell[] cells = tetromino.cells;//cells每一个格 for(int i=0; i Cell cell = cells[i];//cell某一个格 int col = cell.getCol(); if(col<0 || col>=COLS) {//列坐标小于0,大于9 return true;//出界 } } return false; } //检查正在下落的方块和墙上的方块是否重叠 private boolean coincide() { Cell[] cells = tetromino.cells;//cells每一个格 for(int i=0; i Cell cell = cells[i];//cell某一个格 int row = cell.getRow(); int col = cell.getCol(); //如果墙上有格子 if(row>=0&&row return true;//重叠 } } return false; } //下落流程控制 public void softDropAction() { if(canDrop()) {//如果能下落 tetromino.softDrop();//下落一步 }else { landIntoWall();//进墙 destoryLines();//销毁满的行 checkGameOverAction();//检查是否结束 tetromino = nextOne; nextOne = Tetromino.randomOne();//入墙一个方块,出现下一个方块} } //检查当前的方块是否能够下落,返回true能下落 private boolean canDrop() { Cell[] cells = tetromino.cells;//cells每一个格 for(int i=0; i Cell cell = cells[i];//cells某一个格 int row = cell.getRow(); if(row == ROWS-1) {//ROWS-1是19 return false;//不能下落 } } for(int i=0; i Cell cell = cells[i]; int row = cell.getRow()+1; int col = cell.getCol(); if(row>=0&&row } } return true; } //进墙 private void landIntoWall() { Cell[] cells = tetromino.cells;//cells每一个格 for(int i=0; i Cell cell = cells[i];//cells某一个格 int row = cell.getRow(); int col = cell.getCol(); wall[row][col] = cell;//格子进到墙上对应的位置 } } //得分表 private static int[] scoreTable = {0,1,10,50,100}; //销毁满的行 private void destoryLines() { int lines = 0; for(int row=0; row if(fullCells(row)) {//满格 deleteRow(row);//删行 lines++; } } this.score += scoreTable[lines];//得分 this.lines+=lines;//加行 } //消除行 private void deleteRow(int row) { for(int i=row; i>=1; i--) {//逐行查找 System.arraycopy(wall[i-1],0,wall[i],0,COLS); } Arrays.fill(wall[0], null);//满了删除 } //检查当前行的每个格子,如果有null,就返回false,否则返回true private boolean fullCells(int row) { Cell[] line = wall[row];//从墙上取一行 for(Cell cell:line) {//逐行检查 if(cell==null) {//有空 return false;//没满 } } return true;//满了 } //下落流程,下落到不能下落为止,快速下落public void hardDropAction() { while(canDrop()) { tetromino.softDrop(); } landIntoWall(); destoryLines(); checkGameOverAction(); tetromino = nextOne; nextOne = Tetromino.randomOne(); } //添加旋转流程控制方法 public void rotateRightAction() { tetromino.rotateRight(); if(outOfBounds() || coincide()){ tetromino.rotateLeft(); } } //添加开始流程 public void startAction() { pause = false; gameOver = false; score = 0; lines = 0; clearWall(); tetromino = Tetromino.randomOne(); nextOne = Tetromino.randomOne(); timer = new Timer(); timer.schedule(new TimerTask(){ public void run() { softDropAction(); repaint(); } },interval,interval); } //清空墙 private void clearWall() { for(int i=0; i Arrays.fill(wall[i], null); } } //暂停流程 public void pauseAction() { timer.cancel(); pause = true; } //继续流程 public void continueAction() { timer = new Timer(); timer.schedule(new TimerTask() { public void run() { softDropAction(); repaint(); } }, interval, interval); pause = false; } //结束流程 public void checkGameOverAction() { if(wall[0][4]!=null) { gameOver = true; timer.cancel(); } } //主方法 public static void main(String[] args) { JFrame frame = new JFrame("俄罗斯方块");//创建窗体 Tetris tetris = new Tetris();//创建面板 tetris.setBackground(new Color(255)); frame.add(tetris);//添加到窗体中 frame.setSize(680,700);//设置尺寸 frame.setResizable(false); frame.setLocationRelativeTo(null);//居中 frame.setDefaultCloseOperation(3);//关窗口退程序 frame.setVisible(true);//显示 tetris.action(); } } package tes; import java.util.Arrays; import java.util.Random; /** * 四格方块类:包含七个子类T I J L S Z O */ public abstract class Tetromino { //创建四个格子 protected Cell[] cells = new Cell[4]; protected State[] states;//旋转状态 protected int index = 10000;//旋转状态的序号state[index%state.length] //旋转内部类 protected class State { int row0,col0,row1,col1,row2,col2,row3,col3;//转一圈8个数 //构造器 public State(int row0, int col0, int row1, int col1, int row2, int col2, int row3, int col3) { super(); this.row0 = row0; this.col0 = col0; this.row1 = row1; this.col1 = col1; this.row2 = row2; this.col2 = col2; this.row3 = row3; this.col3 = col3; } } /** 向右转*/ public void rotateRight() { //取得变换的下个数据状态state[n] index++; State s = states[index%states.length]; //取得当前轴的row,col Cell o = cells[0]; int row = o.getRow(); int col = o.getCol(); //旋转以后的数据=(row,col) + state[n] cells[1].setRow(row + s.row1); cells[1].setCol(col + s.col1); cells[2].setRow(row + s.row2); cells[2].setCol(col + s.col2); cells[3].setRow(row + s.row3); cells[3].setCol(col + s.col3); /** 向左转*/ public void rotateLeft() { //取得变换的下个数据状态state[n] index--; State s = states[index%states.length]; //取得当前轴的row,col Cell o = cells[0]; int row = o.getRow(); int col = o.getCol(); //旋转以后的数据=(row,col) + state[n] cells[1].setRow(row + s.row1); cells[1].setCol(col + s.col1); cells[2].setRow(row + s.row2); cells[2].setCol(col + s.col2); cells[3].setRow(row + s.row3); cells[3].setCol(col + s.col3); } //工厂方法,随机产生四个格子 public static Tetromino randomOne() { //随机产生七种四格方块 Random random = new Random(); int type = random.nextInt(7); switch(type) { case 0 : return new T(); case 1 : return new I(); case 2 : return new J(); case 3 : return new L(); case 4 : return new S(); case 5 : return new Z(); case 6 : return new O(); } return null; } //移动方法 public void softDrop() { for(int i=0; i cells[i].moveDown();//使每一个方块下落} } public void moveLeft() { for(int i=0; i cells[i].moveLeft();//使每一个方块左移} public void moveRight() { for(int i=0; i cells[i].moveRight();//使每一个方块右移 } } //重写toString public String toStrig() { return Arrays.toString(cells); } } //创建子类T继承父类Tetromino class T extends Tetromino { public T() { cells[0] = new Cell(0,4,Tetris.T); cells[1] = new Cell(0,3,Tetris.T); cells[2] = new Cell(0,5,Tetris.T); cells[3] = new Cell(1,4,Tetris.T); states = new State[4]; states[0] = new State(0,0, 0,-1, 0,1, 1,0); states[1] = new State(0,0, -1,0, 1,0, 0,-1); states[2] = new State(0,0, 0,1, 0,-1, -1,0); states[3] = new State(0,0, 1,0, -1,0, 0,1); } } class I extends Tetromino { public I() { cells[0] = new Cell(0,4,Tetris.I); cells[1] = new Cell(0,3,Tetris.I); cells[2] = new Cell(0,5,Tetris.I); cells[3] = new Cell(0,6,Tetris.I); states = new State[] {new State(0,0, 0,1, 0,-1, 0,-2), new State(0,0, -1,0, 1,0, 2,0)}; } } class J extends Tetromino { public J() { cells[0] = new Cell(0,4,Tetris.J); cells[1] = new Cell(0,3,Tetris.J); cells[2] = new Cell(0,5,Tetris.J); cells[3] = new Cell(1,5,Tetris.J); states = new State[]{ new State(0,0, 0,-1, 0,1, 1,1), new State(0,0, -1,0, 1,0, 1,-1), new State(0,0, 0,1, 0,-1, -1,-1), new State(0,0, 1,0, -1,0, -1,1)}; } } class L extends Tetromino { public L() { cells[0] = new Cell(0,4,Tetris.L); cells[1] = new Cell(0,3,Tetris.L); cells[2] = new Cell(0,5,Tetris.L); cells[3] = new Cell(1,3,Tetris.L); states = new State[]{ new State(0,0, 0,-1, 0,1, 1,-1), new State(0,0, -1,0, 1,0, -1,-1), new State(0,0, 0,1, 0,-1, -1,1), new State(0,0, 1,0, -1,0, 1,1)}; } } class S extends Tetromino { public S() { cells[0] = new Cell(0,4,Tetris.S); cells[1] = new Cell(0,5,Tetris.S); cells[2] = new Cell(1,3,Tetris.S); cells[3] = new Cell(1,4,Tetris.S); states = new State[]{ new State(0,0, 0,1, 1,-1, 1,0), new State(0,0, -1,0, 1,1, 0,1)}; } } class Z extends Tetromino { public Z() { cells[0] = new Cell(1,4,Tetris.Z); cells[1] = new Cell(0,3,Tetris.Z); cells[2] = new Cell(0,4,Tetris.Z); cells[3] = new Cell(1,5,Tetris.Z); states = new State[]{ new State(0,0, -1,-1, -1,0, 0,1), new State(0,0, -1,1, 0,1, 1,0)}; } } class O extends Tetromino { public O() { cells[0] = new Cell(0,4,Tetris.O); cells[1] = new Cell(0,5,Tetris.O); cells[2] = new Cell(1,4,Tetris.O); cells[3] = new Cell(1,5,Tetris.O); states = new State[]{ new State(0,0, 0,1, 1,0, 1,1), new State(0,0, 0,1, 1,0, 1,1)}; } } 【转载】88行代码实现俄罗斯方块游戏(含讲解) 来源:https://www.360docs.net/doc/ef3820755.html,/p/8 在正式阅读本文之前,请你记得你应该用娱乐的心态来看, 本代码所使用到的技巧,在工作了的人眼里会觉得很纠结,很蛋疼,很不可理喻,很丑, 注意,是你蛋疼,不关我的事 通常,写一个俄罗斯方块,往往动不动就几百行,甚至上千行,而这里只有88行 正所谓头脑风暴,打破常规。这里将使用很多不平常的手段来减少代码 以下是Win-TC可以成功编译并执行的代码(代码保证单行长度不超过80字符,如果你是Win7系统,那请看后文): 程序代码: #include"graphics.h" #include 注意:本源代码包含 1、新建“.h”头文件,将“头文件” 代码粘贴至其中, 2、新建“.c”源文件,将“源代码” 代码粘贴到其中。 3、新建空白工程,将头文件和源代码 添加进去,调试使用。 //头文件 //1.自定义枚举类型,定义7种形态的游戏方块 typedef enum tetris_shape { ZShape=0, SShape, LineShape, TShape, SquareShape, LShape, MirroredLShape }shape; //2.函数声明 //(1)操作方块函数 int maxX();//取得当前方块的最大x坐标 int minX();//取得当前方块的最小x坐标 void turn_left();//当前方块逆时针旋转90度 void turn_right(); int out_of_table(); void transform(); int leftable(); int rightable(); int downable(); void move_left(); void move_right(); //(2)操作游戏桌面的函数 int add_to_table(); void remove_full(); //(3)控制游戏函数 void new_game(); void run_game(); void next_shape(); int random(int seed); //(4)绘图函数 void paint(); void draw_table(); //(5)其他功能函数 void key_down(WPARAM wParam); void resize(); void initialize(); void finalize(); //(6)回调函数,用来处理Windows消息 LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM); //源代码 //1.文件包含 #include C语言课程设计报告 俄罗斯方块程序设计报告 一、问题描述 俄罗斯方块(Tetris,俄文:Тетрис)是一款电视游戏机和掌上游戏机游戏,它由俄罗斯人阿列克谢·帕基特诺夫发明,故得此名。俄罗斯方块的基本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分。 在本次设计中,要求支持键盘操作和若干种不同类型方块的旋转变换,并且界面上显示下一个方块的提示以及当前的玩家的得分,随着游戏的进行,等级越高,游戏难度越大,即方块的下落速度越快,相应的等级,等级越高,为玩家提供了不同的选择。 二、功能分析 I、俄罗斯方块游戏需要解决的问题包括: ⑴、随机产生方块并自动下移 ⑵、用Esc键退出游戏 ⑶、用键变体 ⑷、用键和键左右移动方块 ⑸、用空格键使游戏暂停 ⑹、能正确判断满行并消行、计分、定级别 ⑺、设定游戏为不同级别,级别越高难度越大 II、俄罗斯方块游戏需要设计的功能函数包括: ⑴、声明俄罗斯方块的结构体 ⑵、函数原型声明 ⑶、制作游戏窗口 ⑷、制作俄罗斯方块 ⑸、判断是否可动 ⑹、随机产生俄罗斯方块类型的序号 ⑺、打印俄罗斯方块 ⑻、清除俄罗斯方块的痕迹 ⑼、判断是否满行并删除满行的俄罗斯方块 三、程序设计 1、程序总体结构设计 (1)、游戏方块预览功能。在游戏过程中,游戏界面右侧会有预览区。由于在此游戏中存在多种不同的游戏方块,所以在游戏方块预览区域中显示随机生成的游戏方块有利于游戏玩家控制游戏的策略。 (2)、游戏方块控制功能。通过各种条件的判断,实现对游戏方块的左移、右移、自由下落、旋转功能,以及行满消除行的功能。 (3)、游戏数据显示功能。在游戏玩家进行游戏过程中,需要按照一定的游戏规则给玩家计算游戏分数。例如,消除一行加100分,游戏分数达到一定数量 1. using System; using System.Collections.Generic; using System.Text; using System.Drawing;//add namespace俄罗斯方块 { public class Block { private short width; private short height; private short top; private short left; private int ID; //方块部件的ID public int[,] shape;//存储方块部件的形状,0为空白,1为有砖块 public Block()//构造函数 { Random randomGenerator = new Random(); int randomBlock = randomGenerator.Next(1, 6);//产生1—4的数 this.ID = randomBlock; switch (this.ID) { case 1: //横条形 this.Width = 4; this.Height = 1; this.Top = 0; this.Left = 3; shape = new int[this.Width, this.Height]; shape[0, 0] = 1; shape[1, 0] = 1; shape[2, 0] = 1; shape[3, 0] = 1; break; case 2://正方形 this.Width = 2; this.Height = 2; this.Top = 0; this.Left = 4; // Creates the new shape for this block. shape = new int[this.Width, this.Height]; shape[0, 0] = 1; shape[0, 1] = 1; shape[1, 0] = 1;shape[1, 1] = 1; break; case 3://T形 this.Width = 3; this.Height = 3; this.Top = 0; this.Left = 4; // Creates the new shape for this block. shape = new int[this.Width, this.Height]; shape[0, 0] = 1; shape[1, 0] = 1; shape[2, 0] = 1; shape[1, 1] = 1; shape[1, 2] = 1; break; case 4://L形 this.Width = 2; this.Height = 3; this.Top = 0; this.Left = 4; //不多说,直接可以拷贝下面的东西,就可以运行。 package day04; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; import https://www.360docs.net/doc/ef3820755.html,ng.String.*; import https://www.360docs.net/doc/ef3820755.html,ng.*; import java.io.*; public class ERSBlock extends JPanel implements ActionListener,KeyListener//应该是继承JPanel { static Button but[] = new Button[6]; static Button noStop = new Button("取消暂停"); static Label scoreLab = new Label("分数:"); static Label infoLab = new Label("提示:"); static Label speedLab = new Label("级数:"); static Label scoreTex = new Label("0"); static Label infoTex = new Label(" "); static Label speedTex = new Label("1"); static JFrame jf = new JFrame(); static MyTimer timer; static ImageIcon icon=new ImageIcon("resource/Block.jpg"); static JMenuBar mb = new JMenuBar(); static JMenu menu0 = new JMenu("游戏 "); static JMenu menu1 = new JMenu("帮助 "); static JMenuItem mi0 = new JMenuItem("新游戏"); static JMenuItem mi1 = new JMenuItem("退出"); static JMenuItem mi1_0 = new JMenuItem("关于"); static JDialog dlg_1; static JTextArea dlg_1_text = new JTextArea(); static int startSign= 0;//游戏开始标志 0 未开始 1 开始 2 暂停 static String butLab[] = {"开始游戏","重新开始","降低级数","提高级数","游戏暂停","退出游戏"}; static int game_body[][] = new int[19][10]; static int game_sign_x[] = new int[4];//用于记录4个方格的水平位置 static int game_sign_y[] = new int[4];//用于记录4个方格的垂直位置 不多说,,直接可以拷贝下面的东西,然后记得把那个BLOCK的名字改成你自己的类名,这个很关键哦,不然是错的可别怪我,呵呵~~ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; import https://www.360docs.net/doc/ef3820755.html,ng.String.*; import https://www.360docs.net/doc/ef3820755.html,ng.*; import java.io.*; public class Block extends JPanel implements ActionListener,KeyListener//应该是继承JPanel { static Button but[] = new Button[6]; static Button noStop = new Button("取消暂停"); static Label scoreLab = new Label("分数:"); static Label infoLab = new Label("提示:"); static Label speedLab = new Label("级数:"); static Label scoreTex = new Label("0"); static Label infoTex = new Label(" "); static Label speedTex = new Label("1"); static JFrame jf = new JFrame(); static MyTimer timer; static ImageIcon icon=new ImageIcon("resource/Block.jpg"); static JMenuBar mb = new JMenuBar(); static JMenu menu0 = new JMenu("游戏 "); static JMenu menu1 = new JMenu("帮助 "); static JMenuItem mi0 = new JMenuItem("新游戏"); static JMenuItem mi1 = new JMenuItem("退出"); static JMenuItem mi1_0 = new JMenuItem("关于"); static JDialog dlg_1; static JTextArea dlg_1_text = new JTextArea(); static int startSign = 0;//游戏开始标志 0 未开始 1 开始 2 暂停 static String butLab[] = {"开始游戏","重新开始","降低级数","提高级数","游戏暂停","退出游戏"}; static int game_body[][] = new int[19][10]; static int game_sign_x[] = new int[4];//用于记录4个方格的水平位置 static int game_sign_y[] = new int[4];//用于记录4个方格的垂直位置 static boolean downSign = false;//是否落下 static int blockNumber = 1;//砖块的编号 static int gameScore = 0;//游戏分数 static int speedMark = 1; 俄罗斯方块游戏的开发 组长:XXX 组员:XXX XXX XXX XXX 05软件工程一班 一、课程设计的目的和意义 俄罗斯方块游戏是一个经典的小游戏,由于它简单有趣,因而得到了广泛的流行,男女老幼都适合。而俄罗斯方块游戏的设计工作复杂且富有挑战性,它包含的内容多,涉及的知识广泛,与图形界面联系较大,包括界面的显示与更新、数据收集等,在设计的过程中,必将运用到各方面的知识,这对于visualbasi语 言设计者而言,是个很好的锻炼机会。 二、系统功能设计 本系统主要设计以下几种功能 1、游戏难度选择功能 游戏难度选择界面设置在程序运行开始时,一共有九种难度供玩家选择,每选一级难度,都会相应地显示出代表该难度的图片。开始时不设置任何默认的难度,如果玩家不选难度直接按“Enter”进入,将会弹出提示框,提示其先选难度再 进入。 2、方块下落、变形功能 在整个俄罗斯方块游戏中,方块的设计是核心。这里设计了一个方块类:Square( ),用来生成方块以及实现块的左移、右移、向下、变形、重画、同步显 示、初始化新块等。 3、自动升级功能 当分数累积到一定大小时,系统将自动为玩家提高难度。这里设置了每消除10行方块,就增加一级难度。当难度增加的时候,方块会相应地改变颜色,以作为 对玩家的提示。 4、游戏音乐功能 游戏开始音乐就自动播放,游戏暂停与结束时音乐相应消除。 5、获取帮助功能 这里设置了一个类,用来显示帮助,按F1键就能弹出窗口,显示游戏规则。 三、系统功能设计分析 俄罗斯方块游戏根据功能的不同,设置了如下12个类:Square, Command, GameArea, GameSetting, GameOver, Help, ImagePanel, JieMian, MyPanel, MyTimer, PlayMidi, WinListener,每个类的描述如下: 1、Square,方块类。这个类中定义了生成方块的方法,用二维数组int[][] pattern,存放7种方块的四种状态。在构造方法中以随机的形式生成方块,同时提供了以下几种方法:reset( ),leftTurn( ),leftMove( ),rightMove( ),fallDown( ),assertValid(int t,int s,int row,int col),dispBlock(int s)。分别实现方块的重画、翻转、 左移、右移、下落、同步显示等功能。 2、Command,处理控制类。这是一个实现ActionListener接口的类,主要处理 点击按钮事件。类中定义了三个int型变量:button_play,button_quit,button_pause,和一个boolean型的变量:pause_resume,并赋值。在GameArea 类中通过事件响应,在按钮执行方法中调用其值,使用switch语句,根据不同 按钮不同的值,来响应不同的事件。 3、GameArea,游戏界面类。GameArea继承了JFrame,是俄罗斯方块的主要游 戏界面。这个类定义了GameSetting类的gameScr对象和ImagePanel类的imagepanel对象作为游戏区域面板和控制区域面板。在游戏区域,主要是根据相应格子的设置标志来显示相应的图形图片,这样就实现了俄罗斯方块的实时显 示。 4、GameSetting, 游戏画布类。这个类生成的对象将作为游戏界面的方块下落区域,画布的设置为15行10列,当中的方格边长为30,类中还定义了一个二维数组int [][] scrArr作为屏幕数组,表示每一个方格。游戏区域中每一个方格是否存在游戏方块是由该方格的值来决定的,如果该方格的值为1,则表示该方格中存在游戏方块;如果该方格中的值为0,则表示该方格中不存在游戏方块,因此二维数组用于记录游戏区域中每个小方格的值。此外,类中还定义了画方块的方法,根据不同的难度画出不同颜色的方块。单击Play按钮时,系统调用initScr( )方法,初始化屏幕,将屏幕数组清零。当满足满行删除的条件时,系统调用deleteFullLine( )方法,进行删行加分,而且每删除十行,难度自动增加一级,方块颜色改变,并在难度显示框中相应显示。 5、GameOver,游戏结束弹出提示框类。当游戏结束时,系统弹出提示,包括玩 家分数以及询问玩家要继续游戏还是退出。 6、Help,帮助类。在游戏界面,按F1键,弹出提示窗口,获取帮助。 7、ImagePanel,背景图片类。这个类继承了JPanel类,用来作为游戏界面中控 制区域的容器,并添加图片。 8、JieMian,主界面类。这个类继承了JPanel类,作为游戏的第一个界面,也是难度选择界面。定义了9个单选按钮,当玩家未选任何难度就按Enter时,系统会弹出一个提示框,提示玩家先选难度再进入。 9、MyPanel,重写MyPanel类,使Panel的四周留空间。 /*学无止境*/ #include <> #include <> #include <> #define ESC 27 #define UP 328 #define DOWN 336 #define LEFT 331 #define RIGHT 333 #define BLANK 32 #define BOTTOM 2 #define CANNOT 1 #define CAN 0 #define MAX 30 #define F1 315 #define ADD 43 #define EQUAL 61 #define DEC 45 #define SOUNDs 115 #define SOUNDS 83 #define PAUSEP 80 #define PAUSEp 112 void Init(); void Down(); void GoOn(); void ksdown(); void Display(int color); void Give(); int Touch(int x,int y,int dx,int dy); int GeyKey(); void Select(); void DetectFill(); void GetScores(); void Fail(); void Help(); void Quit(); void DrawBox(int x,int y,int Color); void OutTextXY(int x,int y,char *String); void DispScore(int x,int y,char Ch); void DrawNext(int Color); int Heng=12,Shu=20; /*横竖*/ int Position[MAX][MAX]; int middle[MAX][MAX]; int ActH,ActS; **** 届毕业设计Java小游戏俄罗斯方块 ┊┊┊┊┊┊┊┊┊┊┊┊┊装┊┊┊┊┊订┊┊┊┊┊线┊┊┊┊┊┊┊┊┊┊┊┊┊ 摘要 在现今电子信息高速发展的时代,电子游戏已经深入人们的日常生活,成为老少皆宜的娱乐方式。但是游戏设计结合了日新月异的技术,在一个产品中整合了复杂的设计、艺术、声音和软件,所以并不是人人皆知。直到今天,在中国从事游戏设计的人仍然很少,但是游戏行业的发展之快,远超如家电、汽车等传统行业,也正因为如此,游戏人才的教育、培养远落后于产业的发展。 俄罗斯方块是个老幼皆宜的小游戏,它实现由四块正方形的色块组成,然后存储在一个数组的四个元素中,计算机随机产生不同七种类型的方块,根据计算机时钟控制它在一定的时间不停的产生,用户根据键盘的四个方向键控制翻转、向左、向右和向下操作,(控制键的实现是由键盘的方向键的事件处理实现)。然后程序根据这七种方块堆叠成各种不同的模型。 论文描述了游戏的历史,开发此游戏的环境,游戏开发的意义。遵循软件工程的知识,从软件问题定义开始,接着进行可行性研究、需求分析、概要设计、详细设计,最后对软件进行了测试,整个开发过程贯穿软件工程的知识体系。 此次设计在Microsoft Windows 7系统下,以Java为开发语言,在eclipse开发平台上进行游戏的设计与实践。从游戏的基本玩法出发,主要就是俄罗斯方块的形状和旋转,我在设计中在一个图片框中构造了一些的网状小块,由这些小块组合成新的形状,每四个小块连接在一起就可以构造出一种造型,因此我总共设计了7中造型,每种造型又可以通过旋转而变化出2到4种形状,利用随机函数在一个欲览窗体中提前展示形状供用户参考,在游戏窗体中用户就可以使用键盘的方向键来控制方块的运动,然后利用递归语句对每一行进行判断,如果有某行的方块是满的,则消除这行的方块,并且使上面的方块自由下落,最后就可以得出用户的分数。 关键词:游戏设计,算法,数组,事件 //这是一个Windows程序,简单的俄罗斯方块程序。最下面附有截图 //这是一个Win32 Application程序 // ToyBricks.cpp : Defines the entry point for the application. // #include "stdafx.h" #include 先是效果图:主菜单: 游戏: 设置: 错误处理: 627行,代码如下: #include #define DOWN 1 #define LEFT 2 #define RIGHT 3 //错误码 #define no_enough_memory 0 #define set_no_found 1 #define dat_no_found 2 #define error_argument 3 //函数声明 //模块 void play();//开始游戏 void sets();//设置 void highscores();//排行榜 void copyright();//作者 //功能 void mapsetup();//准备地图 bool newsqr();//放置方块,返回是否游戏结束 int move(int direction);//移动方块,返回定义表 void movetomap();//把当前方块移动到地图上 int wholeline();//检查是否组成了一层,返回层数,-1表示没有 void deleteline(int which);//删除一行 void endup();//结束游戏,清理内存 //显示 void show();//刷新画面 void showmenu(char* menu);//显示菜单 //文件 void loadset();//加载设置 void saveset();//保存设置 void loadhs();//加载排行榜 bool addscores(int score,char name[50]);//增加一个分数,返回是否是高分void savehs();//保存排行榜 //坐标变换 int get(int x,int y); void set(int x,int y,int date); //结构 //设置 struct{ int xs,ys;//屏幕大小 int speed;//速度 char sqr[3],no[3],frame[3];//方块、空白处、边框的样式 }gameset; //排行榜 N = 20; //行数 WIDTH = 20; //方块边长 level = 0; //开始等级(下落速度) ret = new Array(); //当前出现的方块 nextret = new Array(); //下一个出现的方块 bg = new Array(); //背景数组 createEmptyMovieClip("panel", 1048575); //所有方块都在此mc里 for (i=0; i<5; i++) { //初始化方块数组,2*5格式,前四行代表每个方块的4个小块的位置坐标,最后一行第一列是方块形状,第二列是方块旋转方向 ret.push(new Array(2)); nextret.push(new Array(2)); } for (i=0; i<20; i++) { //初始化背景数组,10*20格式 bg.push(new Array(10)); } X = Y=panel._x=panel._y=0; //换为X、Y表示 function reach(x:Number, y:Number, ret:Object) { //x、y为方块位置,ret为方块形状,若方块ret下落一格碰到边界或者方块返回1 var i:Number, j:Number, k:Number; for (i=0; i 在正式阅读本文之前,请你记得你应该用娱乐的心态来看, 本代码所使用到的技巧,在工作了的人眼里会觉得很纠结,很蛋疼,很不可理喻,很丑,注意,是你蛋疼,不关我的事 通常,写一个俄罗斯方块,往往动不动就几百行,甚至上千行,而这里只有88行 正所谓头脑风暴,打破常规。这里将使用很多不平常的手段来减少代码 但为了能更通用,一份控制台版本的代码,同样是88行,直接复制到VC即可编译: #include OpenGL实现俄罗斯方块 一、设计思路 本程序采用以C++为基础并利用OpenGl库函数的方式实现俄罗斯方块程序,实现俄罗斯方块中正方形,T形,L形,反L形,直线型,Z字形,反Z字形七种形状的变换操作,七种形状的坐标被存储在一个三维数组中,每次随机选择一个形状生成并下落,在下落过程中监听键盘事件。 二、详细设计说明 本程序中主要的函数及其作用说明如下: down:定时下落函数,由glutTimerFunc(1000,down,1);函数设置每隔1000毫秒即调用一次该函数,在函数中将方块的所有纵坐标减一个单位。 key:键盘事件监听函数,当键盘上有按键被触发的时候即调用该函数,函数内部支持w,a,s,d四个键的响应,依次代表方向键上,左,右,下,其中w键是用来 控制图形变换的,每次按w键时,图形在现有基础上顺时针变换一次。 CheckConflict:冲突检测函数,检测方块下一次将要移动的位置是否会碰到已有的方块或者左右两边的墙壁。 CheckDelete:每一次方块落到底部之后,调用该函数检查是否有满行,如果有则调用Delete函数删除该行。 myDisplay1:作图函数。 Change:变换函数,主要是通过计算变换后的图形与之前图形的坐标关系来实现。 三、源代码 #include 88行代码实现俄罗斯方块游戏(含讲解) 在正式阅读本文之前,请你记得你应该用娱乐的心态来看, 本代码所使用到的技巧,在工作了的人眼里会觉得很纠结,很蛋疼,很不可理喻,很丑,注意,是你蛋疼,不关我的事 通常,写一个俄罗斯方块,往往动不动就几百行,甚至上千行,而这里只有88行 正所谓头脑风暴,打破常规。这里将使用很多不平常的手段来减少代码 但为了能更通用,一份控制台版本的代码,同样是88行,直接复制到VC即可编译: #include 课程设计报告文档 题目:俄罗斯方块 一.引言 1、编写目的: 通过本课程设计,使学生巩固面向对象程序设计的基本概念、原理和技术,学会使用Visual C++开发工具进行简单面向对象程序的开发,将理论与实际相结合,完成一个小型面向对象程序的设计与实现,并在此基础上强化学生的实践意识,提高其实际动手能力和创新能力。 2、.定义:无 3、参考资料: 郑莉.C++语言程序设计.北京:清华大学出版社,2003 罗建军.大学Visual C++程序设计案例教程.北京:高等教育出版社,2004 刘路放.Visual C++与面向对象程序设计教程.北京:高等教育出版社,2000 David J.Visual C++技术内幕.潘爱民印译.北京:清华大学出版社,1999 侯俊杰.深入浅出MFC.武汉:华中科技大学出版社,2001 二.任务的描述: 1.目标:编写基于VC++开发工具并能通过键盘和鼠标进行人机交流俄罗斯方块游戏,界面友好,容易操作的游戏。 2.功能描述:通过控制键盘可以进行游戏的开始、暂停、结束;能够控制方块的移动、变形;具有进行经典的俄罗斯方块游戏的一般功能。 3.性能描述 2、键盘响应函数流程图 Y N Y 4 Y (1)类1:CAboutDlg()类是建立MFC程序时自动生成的类,俄罗斯方块游戏基本上用不到这个类,故不做解释。 (2)类2:CMainFrame()框架类主要是对游戏的框架进行设计。 数据成员:C StatusBar m_wndStatusBar; 工具栏 CToolBar m_wndToolBar;状态栏 成员函数:1、CMainFrame()构造函数:初始化游戏; 2、~CMainFrame()析构函数:删除游戏数据; 3、OnCreate():工具栏的实现; 4、PreCreateWindow()游戏窗口的规格实现; (3)类3:COptionDlg()游戏状态显示类:主要实现对游戏在进行中的一些状态显示比如:级别、下一个方块。 数据成员: int m_oldArea; 区域大小 int m_o ldLevel;等级 int m_oldBlockSytle;背景风格 BOOL m_oldDrawGrid;俄罗斯方块C语言代码
C.C++语言-俄罗斯方块源码
C语言课程设计俄罗斯方块源代码
俄罗斯方块C语言程序设计报告
俄罗斯方块源代码
俄罗斯方块完整源代码
JAVA俄罗斯方块源代码
俄罗斯方块游戏的开发需求分析
C语言俄罗斯方块游戏源代码
Java小游戏俄罗斯方块附完整源代码_毕业设计
俄罗斯方块源程序
C++俄罗斯方块源码(完整功能版)
FLASH游戏之俄罗斯方块源代码
简单俄罗斯方块程序实现,只有88行代码
OpenGL实现俄罗斯方块源代码
88行代码实现俄罗斯方块游戏(含讲解)
VC++俄罗斯方块课程设计报告含源代码