以后我会再写C++代码的时候夹杂一些JAVE和Python。
比如说我要做一个俄罗斯方块小游戏,(就算给大家发福利了)我可能会这样做:
俄罗斯方块C++:
#include<iostream>
#include<math.h>
#include<Windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
enum DIR
{
UP,
RIGHT,
DOWN,
LEFT
};
time_t start = 0, finish = 0;
int _x = 6, _y = 1;//图形生成位置
int map[30][16] = { 0 };
int sharp[20][8] = {
{0,0,0,0,0,0,0,0},
//I形
{0,0,0,1,0,2,0,3},
{0,0,1,0,2,0,3,0},
//■形
{0,0,1,0,0,1,1,1},
//L形
{0,0,0,1,0,2,1,2},
{0,0,0,1,1,0,2,0},
{0,0,1,0,1,1,1,2},
{0,1,1,1,2,0,2,1},
//J形
{0,2,1,0,1,1,1,2},
{0,0,0,1,1,1,2,1},
{0,0,0,1,0,2,1,0},
{0,0,1,0,2,0,2,1},
//Z形
{0,0,1,0,1,1,2,1},
{0,1,0,2,1,0,1,1},
//S形
{0,1,1,0,1,1,2,0},
{0,0,0,1,1,1,1,2},
//T形
{0,1,1,0,1,1,2,1},
{0,0,0,1,0,2,1,1},
{0,0,1,0,1,1,2,0},
{0,1,1,0,1,1,1,2}
};
class Game
{
public:
int score;//游戏分数
int _id;//图形编号
int top;//最高点高度
int speed;//下落速度
Game();
void showMenu();//显示菜单
void showGround();//显示游戏界面
void gameOver();//游戏结束界面
void Run();//运行游戏
void sharpDraw(int id, bool show = false);//绘制图形
void keyControl();//键盘控制
bool move(int dir, int id);//移动判断
bool downSet(int id);//下落
void Turn(int id);//旋转
void clean();//消行
};
void SetPos(int i, int j)//控制光标位置, 列, 行
{
COORD pos = { i,j };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int main()
{
CONSOLE_CURSOR_INFO cursor;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
cursor.bVisible = 0; //这四行用来设置光标不显示
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
srand((unsigned)time(NULL));
Game game;
game.showMenu();
return 0;
}
Game::Game()
{
score = 0;
_id = 0;
top = 58;
speed = 1000;
}
void Game::showMenu()
{
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 26; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 25))
{
cout << "■";
}
else
{
cout << " ";
}
}
cout << endl;
}
SetPos(17, 8);
cout << "俄 罗 斯 方 块" << endl;
SetPos(13, 12);
cout << "↑旋转方块 ↓加速下滑" << endl;
SetPos(12, 14);
cout << "← →左右移动 空格 暂停" << endl;
SetPos(15, 20);
cout << "0 退出 Enter 开始" << endl;
while (1)
{
int select = _getch();
if (select == 13)
{
system("cls");
this->Run();
}
else if (select = 48)
{
system("cls");
exit(0);
}
}
}
void Game::showGround()
{
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 26; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 25 || j == 15))
{
cout << "■";
}
else if (i == 15 && j > 15)
{
cout << "■";
}
else
{
cout << " ";
}
}
cout << endl;
}
SetPos(31, 2);
cout << "下 个图形" << endl;
SetPos(31, 17);
cout << "当 前得分" << endl;
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 16; j++)
{
if ((i == 0 || i == 29) || (j == 0 || j == 15))
{
map[i][j] = 1;
}
else
{
map[i][j] = 0;
}
}
}
}
void Game::gameOver()
{
for (int i = 5; i < 15; i++)
{
SetPos(1, i);
cout << " " << endl;
}
SetPos(8, 7);
cout << "G a m e O v e r" << endl;
SetPos(3, 10);
cout << "0 退出 Enter 重新开始" << endl;
while (1)
{
int select = _getch();
if (select == 13)
{
system("cls");
this->Run();
}
else if (select == 48)
{
system("cls");
exit(0);
}
}
}
void Game::Run()
{
score = 0;
_id = 0;
top = 58;
_x = 6;
_y = 1;
showGround();
start = clock();
int new_id = rand() % 19 + 1;
while (1)
{
sharpDraw(_id);
keyControl();
if (downSet(_id))
{
sharpDraw(-new_id, 1);
_id = new_id;
new_id = rand() % 19 + 1;
sharpDraw(new_id, 1);
clean();
}
SetPos(34, 20);
cout << score << endl;
}
}
void Game::sharpDraw(int id, bool show)
{
int x, y;
if (show == true)
{
if (id > 0)
{
for (int i = 0; i < 4; i++)
{
x = 19 + sharp[id][2 * i];
y = 6 + sharp[id][2 * i + 1];
SetPos(2 * x, y);
cout << "■";
}
}
else
{
for (int i = 0; i < 4; i++)
{
x = 19 + sharp[-id][2 * i];
y = 6 + sharp[-id][2 * i + 1];
SetPos(2 * x, y);
cout << " ";
}
}
return;
}
if (id > 0)
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
SetPos(2 * x, y);
cout << "■";
}
}
else
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[-id][2 * i];
y = _y + sharp[-id][2 * i + 1];
SetPos(2 * x, y);
cout << " ";
}
}
return;
}
bool Game::downSet(int id)
{
if (id == 0)
return true;
finish = clock();
if (finish - start < speed)
{
return false;
}
start = clock();
if (!move(DOWN, _id))
{
int x, y;
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
map[y][x] = 1;
if (y < top)
{
top = y;
}
if (top <= 1)
{
gameOver();
}
}
_x = 6;
_y = 1;
return true;
}
sharpDraw(-id);
_y++;
sharpDraw(id);
return false;
}
bool Game::move(int dir, int id)
{
int x, y;
switch (dir)
{
case UP:
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x] == 1)
{
return false;
}
}
break;
case DOWN:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y + 1][x] == 1)
{
return false;
}
}
}
break;
case RIGHT:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x + 1] == 1)
{
return false;
}
}
}
break;
case LEFT:
{
for (int i = 0; i < 4; i++)
{
x = _x + sharp[id][2 * i];
y = _y + sharp[id][2 * i + 1];
if (map[y][x - 1] == 1)
{
return false;
}
}
}
break;
default:
break;
}
return true;
}
void Game::Turn(int id)
{
switch (id)
{
case 1:id++; break;
case 2:id--; break;
case 3: break;
case 4:id++; break;
case 5:id++; break;
case 6:id++; break;
case 7:id -= 3; break;
case 8:id++; break;
case 9:id++; break;
case 10:id++; break;
case 11:id -= 3; break;
case 12:id++; break;
case 13:id--; break;
case 14:id++; break;
case 15:id--; break;
case 16:id++; break;
case 17:id++; break;
case 18:id++; break;
case 19:id -= 3; break;
default:
break;
}
if (!move(UP, id))
{
return;
}
sharpDraw(-_id);
_id = id;
}
void Game::keyControl()
{
if (!_kbhit())
return;
int key = _getch();
switch (key)
{
case 72:
Turn(_id);
break;
case 80:
if (move(DOWN, _id))
{
sharpDraw(-_id);
_y++;
}
break;
case 75:
if (move(LEFT, _id))
{
sharpDraw(-_id);
_x--;
}
break;
case 77:
if (move(RIGHT, _id))
{
sharpDraw(-_id);
_x++;
}
break;
case 32:
{
for (int i = 5; i < 15; i++)
{
SetPos(1, i);
cout << " " << endl;
}
SetPos(10, 7);
cout << "游 戏 暂 停" << endl;
SetPos(3, 10);
cout << "0 返回菜单 回车 继续游戏" << endl;
while (1)
{
int select = _getch();
if (select == 13)
{
for (int i = 5; i < 15; i++)
{
SetPos(1, i);
cout << " " << endl;
}
break;
}
else if (select == 48)
{
system("cls");
showMenu();
}
}
}
default:
break;
}
}
void Game::clean()
{
int n = -1;
int line = -1;
while (1)
{
for (int i = 28; i > 0; i--)
{
for (int j = 1; j < 15; j++)
{
line = i;
if (map[i][j] == 0)
{
line = -1;
break;
}
}
if (line != -1)
break;
}
if (line == -1)
break;
for (int i = line; i > 0; i--)
{
for (int j = 1; j < 15; j++)
{
if (i == 1)
map[i][j] = 0;
else
{
map[i][j] = map[i - 1][j];
SetPos(2 * j, i);
if (map[i][j] == 1)
cout << "■";
else
cout << " ";
}
}
}
top++;
n++;
}
if (n >= 0)
{
score += n * n * 100 + 100;
if (speed > 100)
speed = 1000 - score / 10;
}
}
俄罗斯方块JAVE:
package 俄罗斯方块;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.lang.String.*;
import java.lang.*;
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;
public static void main(String args[])
{
Block myBlock = new Block();
mb.add(menu0);
mb.add(menu1);
menu0.add(mi0);
menu0.add(mi1);
menu1.add(mi1_0);
jf.setJMenuBar(mb);
myBlock.init();
jf.add(myBlock);
jf.setSize(565,501);
jf.setResizable(false);
jf.setTitle("俄罗斯方块");
jf.setIconImage(icon.getImage());
jf.setLocation(200,100);
jf.show();
timer = new MyTimer(myBlock); //启动线程
timer.setDaemon(true);
timer.start();
timer.suspend();
}
public void init()
{
setLayout(null);
for(int i = 0;i < 6;i++)
{
but[i] = new Button(butLab[i]);
add(but[i]);
but[i].addActionListener(this);
but[i].addKeyListener(this);
but[i].setBounds(360,(240 + 30 * i),160,25);
}
add(scoreLab);
add(scoreTex);
add(speedLab);
add(speedTex);
add(infoLab);
add(infoTex);
add(scoreLab);
scoreLab.setBounds(320,15,30,20);
scoreTex.setBounds(360,15,160,20);
scoreTex.setBackground(Color.white);
speedLab.setBounds(320,45,30,20);
speedTex.setBounds(360,45,160,20);
speedTex.setBackground(Color.white);
but[1].setEnabled(false);
but[4].setEnabled(false);
infoLab.setBounds(320,75,30,20);
infoTex.setBounds(360,75,160,20);
infoTex.setBackground(Color.white);
noStop.setBounds(360,360,160,25);
noStop.addActionListener(this);
noStop.addKeyListener(this);
mi0.addActionListener(this);
mi1.addActionListener(this);
mi1_0.addActionListener(this);
num_csh_game();
rand_block();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == but[0])//开始游戏
{
startSign = 1;
infoTex.setText("游戏已经开始!");
but[0].setEnabled(false);
but[1].setEnabled(true);
but[4].setEnabled(true);
timer.resume();
}
if(e.getSource() == but[1]||e.getSource() == mi0)//重新开始游戏
{
startSign = 0;
gameScore = 0;
timer.suspend();
num_csh_restart();
repaint();
rand_block();
scoreTex.setText("0");
infoTex.setText("新游戏!");
but[0].setEnabled(true);
but[1].setEnabled(false);
but[4].setEnabled(false);
}
if(e.getSource() == but[2])//降低级数
{
infoTex.setText("降低级数!");
speedMark--;
if(speedMark <= 1)
{
speedMark = 1;
infoTex.setText("已经是最低级数!");
}
speedTex.setText(speedMark + "");
}
if(e.getSource() == but[3])//提高级数
{
infoTex.setText("提高级数!");
speedMark++;
if(speedMark >= 9)
{
speedMark = 9;
infoTex.setText("已经是最高级数!");
}
speedTex.setText(speedMark + "");
}
if(e.getSource() == but[4])//游戏暂停
{
this.add(noStop);
this.remove(but[4]);
infoTex.setText("游戏暂停!");
timer.suspend();
}
if(e.getSource() == noStop)//取消暂停
{
this.remove(noStop);
this.add(but[4]);
infoTex.setText("继续游戏!");
timer.resume();
}
if(e.getSource() == but[5]||e.getSource() == mi1)//退出游戏
{
jf.dispose();
}
if(e.getSource() == mi1_0)//退出游戏
{
dlg_1 = new JDialog(jf,"关 于");
try{
FileInputStream io = new FileInputStream("resource/guanyu.txt");//得到路径
byte a[] = new byte[io.available()];
io.read(a);
io.close();
String str = new String(a);
dlg_1_text.setText(str);
}
catch(Exception g){}
dlg_1_text.setEditable(false);
dlg_1.add(dlg_1_text);
dlg_1.pack();
dlg_1.setResizable(false);
dlg_1.setSize(200, 120);
dlg_1.setLocation(400, 240);
dlg_1.show();
}
}
public void rand_block()//随机产生砖块
{
int num;
num = (int)(Math.random() * 6) + 1;//产生0~6之间的随机数
blockNumber = num;
switch(blockNumber)
{
case 1: block1(); blockNumber = 1; break;
case 2: block2(); blockNumber = 2; break;
case 3: block3(); blockNumber = 3; break;
case 4: block4(); blockNumber = 4; break;
case 5: block5(); blockNumber = 5; break;
case 6: block6(); blockNumber = 6; break;
case 7: block7(); blockNumber = 7; break;
}
}
public void change_body(int blockNumber)//改变砖块状态
{
dingwei();
if(blockNumber == 1&&downSign == false)//变换长条2种情况
{
if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[3] <= 16)//说明长条是横着的
{
if(game_body[game_sign_y[0] - 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[3] + 2][game_sign_x[3] - 2] != 2)
{
num_csh_game();
game_body[game_sign_y[0] - 1][game_sign_x[0] + 1] = 1;
game_body[game_sign_y[1]][game_sign_x[1]] = 1;
game_body[game_sign_y[2] + 1][game_sign_x[2] - 1] = 1;
game_body[game_sign_y[3] + 2][game_sign_x[3] - 2] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] >= 1&&game_sign_x[3] <= 7)//说明长条是竖着的
{
if(game_body[game_sign_y[0] + 1][game_sign_x[0]-1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3] + 2] != 2)
{
num_csh_game();
game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;
game_body[game_sign_y[1]][game_sign_x[1]]=1;
game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1;
game_body[game_sign_y[3] - 2][game_sign_x[3] + 2] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
}
if(blockNumber == 3&&downSign == false)//变换转弯1有4种情况
{
if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] == game_sign_x[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] >= 1)
{
if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2)
{
num_csh_game();
game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;
game_body[game_sign_y[1]][game_sign_x[1]] = 1;
game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1;
game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] <= 17)
{
if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2)
{
num_csh_game();
game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1;
game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;
game_body[game_sign_y[2]][game_sign_x[2]] = 1;
game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_x[1] == game_sign_x[2]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[0] == game_sign_y[1]&&game_sign_x[3] <= 8)
{
if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2)
{
num_csh_game();
game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;
game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] = 1;
game_body[game_sign_y[2]][game_sign_x[2]] = 1;
game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[0] == game_sign_x[3])
{
if(game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3]][game_sign_x[3] + 2] != 2)
{
num_csh_game();
game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] = 1;
game_body[game_sign_y[1]][game_sign_x[1]] = 1;
game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;
game_body[game_sign_y[3]][game_sign_x[3] + 2] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
}
if(blockNumber == 4&&downSign == false)//变换转弯2有4种情况
{
if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[3] <= 7)
{
if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3]][game_sign_x[3] + 2] != 2)
{
num_csh_game();
game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;
game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;
game_body[game_sign_y[2]][game_sign_x[2]] = 1;
game_body[game_sign_y[3]][game_sign_x[3] + 2] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[1] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[2])
{
if(game_body[game_sign_y[1]][game_sign_x[1] + 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2)
{
num_csh_game();
game_body[game_sign_y[0]][game_sign_x[0]] = 1;
game_body[game_sign_y[1]][game_sign_x[1] + 2] = 1;
game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1;
game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[0] >= 2)
{
if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2)
{
num_csh_game();
game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1;
game_body[game_sign_y[1]][game_sign_x[1]] = 1;
game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;
game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[0] == game_sign_y[2]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[0] <= 16)
{
if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] != 2&&game_body[game_sign_y[2]][game_sign_x[2] - 2] != 2)
{
num_csh_game();
game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;
game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] = 1;
game_body[game_sign_y[2]][game_sign_x[2] - 2] = 1;
game_body[game_sign_y[3]][game_sign_x[3]] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
}
if(blockNumber == 5&&downSign == false)//变换转弯3有4种情况
{
if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[2] == game_sign_x[3]&&game_sign_y[0] == game_sign_y[1]&&game_sign_x[1] >= 2)
{
if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[1]][game_sign_x[1] - 2] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2)
{
num_csh_game();
game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;
game_body[game_sign_y[1]][game_sign_x[1] - 2] = 1;
game_body[game_sign_y[2]][game_sign_x[2]] = 1;
game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[1]&&game_sign_y[0] <= 16)
{
if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2)
{
num_csh_game();
game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;
game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;
game_body[game_sign_y[2]][game_sign_x[2]] = 1;
game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[2] == game_sign_y[3])
{
if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[2]][game_sign_x[2] + 2] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2)
{
num_csh_game();
game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;
game_body[game_sign_y[1]][game_sign_x[1]] = 1;
game_body[game_sign_y[2]][game_sign_x[2] + 2] = 1;
game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[2] == game_sign_x[3])
{
if(game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2)
{
num_csh_game();
game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] = 1;
game_body[game_sign_y[1]][game_sign_x[1]] = 1;
game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;
game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
}
if(blockNumber == 6&&downSign == false)//变换两层砖块1的2种情况
{
if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[0] >= 2)
{
if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] -1 ] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2)
{
num_csh_game();
game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1;
game_body[game_sign_y[1]][game_sign_x[1]] = 1;
game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;
game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[3] <= 17)
{
if(game_body[game_sign_y[0]][game_sign_x[0] + 2] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] + 1][game_sign_x[3] - 1] != 2)
{
num_csh_game();
game_body[game_sign_y[0]][game_sign_x[0] + 2] = 1;
game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;
game_body[game_sign_y[2]][game_sign_x[2]] = 1;
game_body[game_sign_y[3] + 1][game_sign_x[3] - 1] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
}
if(blockNumber == 7&&downSign == false)//变换两层砖块2的2种情况
{
if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] <= 16)
{
if(game_body[game_sign_y[0]][game_sign_x[0] + 2] != 2&&game_body[game_sign_y[1] - 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2)
{
num_csh_game();
game_body[game_sign_y[0]][game_sign_x[0] + 2] = 1;
game_body[game_sign_y[1] - 1][game_sign_x[1] + 1] = 1;
game_body[game_sign_y[2]][game_sign_x[2]] = 1;
game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[2] <= 17)
{
if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[1]][game_sign_x[1] - 2] != 2&&game_body[game_sign_y[2] + 1][game_sign_x[2] + 1] != 2)
{
num_csh_game();
game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;
game_body[game_sign_y[1]][game_sign_x[1] - 2] = 1;
game_body[game_sign_y[2] + 1][game_sign_x[2] + 1] = 1;
game_body[game_sign_y[3]][game_sign_x[3]] = 1;
infoTex.setText("游戏进行中!");
repaint();
}
}
}
}
public void num_csh_game()//数组清零
{
for(int i = 0;i < 19;i++)
{
for(int j = 0;j < 10;j++)
{
if(game_body[i][j] == 2)
{
game_body[i][j] = 2;
}
else
{
game_body[i][j] = 0;
}
}
}
}
public void num_csh_restart()//重新开始时数组清零
{
for(int i = 0;i < 19;i++)
{
for(int j = 0;j < 10;j++)
{
game_body[i][j] = 0;
}
}
}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_DOWN&&startSign == 1)//处理下键
{
this.down();
}
if(e.getKeyCode() == KeyEvent.VK_LEFT&&startSign == 1)//处理左键
{
this.left();
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT&&startSign == 1)//处理右键
{
this.right();
}
if(e.getKeyCode() == KeyEvent.VK_UP&&startSign == 1)//处理上键转换
{
this.change_body(blockNumber);
}
if(startSign == 0)
{
infoTex.setText("游戏未开始或已结束!");
}
}
public void keyReleased(KeyEvent e){}
public void paint(Graphics g)
{
g.setColor(Color.black);
g.fill3DRect(0,0,300,450,true);
for(int i = 0;i < 19;i++)
{
for(int j = 0;j < 10;j++)
{
if(game_body[i][j] == 1)
{
g.setColor(Color.blue);
g.fill3DRect(30*j,30*(i-4),30,30,true);
}
if(game_body[i][j] == 2)
{
g.setColor(Color.magenta);
g.fill3DRect(30*j,30*(i-4),30,30,true);
}
}
}
}
public void left()//向左移动
{
int sign = 0;
dingwei();
for(int k = 0;k < 4;k++)
{
if(game_sign_x[k] == 0||game_body[game_sign_y[k]][game_sign_x[k] - 1] == 2)
{
sign = 1;
}
}
if(sign == 0&&downSign == false)
{
num_csh_game();
for(int k = 0;k < 4;k++)
{
game_body[game_sign_y[k]][game_sign_x[k] - 1] = 1;
}
infoTex.setText("向左移动!");
repaint();
}
}
public void right()//向右移动
{
int sign = 0;
dingwei();
for(int k = 0;k < 4;k++)
{
if(game_sign_x[k] == 9||game_body[game_sign_y[k]][game_sign_x[k] + 1] == 2)
{
sign = 1;
}
}
if(sign == 0&&downSign == false)
{
num_csh_game();
for(int k = 0;k < 4;k++)
{
game_body[game_sign_y[k]][game_sign_x[k] + 1] = 1;
}
infoTex.setText("向右移动!");
repaint();
}
}
public void down()//下落
{
int sign = 0;
dingwei();
for(int k = 0;k < 4;k++)
{
if(game_sign_y[k] == 18||game_body[game_sign_y[k] + 1][game_sign_x[k]] == 2)
{
sign = 1;
downSign = true;
changeColor();
cancelDW();
getScore();
if(game_over() == false)
{
rand_block();
repaint();
}
}
}
if(sign == 0)
{
num_csh_game();
for(int k = 0;k < 4;k++)
{
game_body[game_sign_y[k] + 1][game_sign_x[k]] = 1;
}
infoTex.setText("游戏进行中!");
repaint();
}
}
public boolean game_over()//判断游戏是否结束
{
int sign=0;
for(int i = 0;i < 10;i++)
{
if(game_body[4][i] == 2)
{
sign = 1;
}
}
if(sign == 1)
{
infoTex.setText("游戏结束!");
changeColor();
repaint();
startSign = 0;
timer.suspend();
return true;
}
else
return false;
}
public void getScore()//满行消除方法
{
for(int i = 0;i < 19;i++)
{
int sign = 0;
for(int j = 0;j < 10;j++)
{
if(game_body[i][j] == 2)
{
sign++;
}
}
if(sign == 10)
{
gameScore += 100;
scoreTex.setText(gameScore+"");
infoTex.setText("恭喜得分!");
for(int j = i;j >= 1;j--)
{
for(int k = 0;k < 10;k++)
{
game_body[j][k] = game_body[j - 1][k];
}
}
}
}
}
public void changeColor()//给已经落下的块换色
{
downSign = false;
for(int k = 0;k < 4;k++)
{
game_body[game_sign_y[k]][game_sign_x[k]] = 2;
}
}
public void dingwei()//确定其位置
{
int k = 0;
cancelDW();
for(int i = 0;i < 19;i++)
{
for(int j = 0;j < 10;j++)
{
if(game_body[i][j] == 1)
{
game_sign_x[k] = j;
game_sign_y[k] = i;
k++;
}
}
}
}
public void cancelDW()//将定位数组初始化
{
for(int k = 0;k < 4;k++)
{
game_sign_x[k] = 0;
game_sign_y[k] = 0;
}
}
public void block1()//长条
{
game_body[0][4] = 1;
game_body[1][4] = 1;
game_body[2][4] = 1;
game_body[3][4] = 1;
}
public void block2()//正方形
{
game_body[3][4] = 1;
game_body[2][4] = 1;
game_body[3][5] = 1;
game_body[2][5] = 1;
}
public void block3()//3加1(下)
{
game_body[1][4] = 1;
game_body[2][4] = 1;
game_body[3][4] = 1;
game_body[3][5] = 1;
}
public void block4()//3加1(中)
{
game_body[1][4] = 1;
game_body[2][4] = 1;
game_body[3][4] = 1;
game_body[2][5] = 1;
}
public void block5()//3加1(上)
{
game_body[1][4] = 1;
game_body[2][4] = 1;
game_body[3][4] = 1;
game_body[1][5] = 1;
}
public void block6()//转折1
{
game_body[1][5] = 1;
game_body[2][5] = 1;
game_body[2][4] = 1;
game_body[3][4] = 1;
}
public void block7()//转折2
{
game_body[1][4] = 1;
game_body[2][4] = 1;
game_body[2][5] = 1;
game_body[3][5] = 1;
}
}
//定时线程
class MyTimer extends Thread
{
Block myBlock;
public MyTimer(Block myBlock)
{
this.myBlock = myBlock;
}
public void run()
{
while(myBlock.startSign == 1)
{
try{
sleep((10-myBlock.speedMark + 1)*100);
myBlock.down();
}
catch(InterruptedException e){}
}
}
}
俄罗斯方块Python:
from pygame.locals import *
import pygame
from random import choice
import time
import sys
import copy
gridSize = (12, 17) # 宽 * 高
MinHeight = 2
GameStatus = list(enumerate(["Ready", "Gaming", "GameOver"]))
ColorList = [(250, 0, 0), (0, 250, 0), (0, 0, 250), (250, 250, 0), (0, 250, 250), (250, 0, 250), (250, 100, 0)]
IDList = list(range(7))
'''
1. □□□□ □ □ □ □□ □□ □□
□□□ □□□ □□□ □□ □□ □□
'''
def stuff_list(stuff_id):
if stuff_id == 0:
ptr = [[0, 0], [0, 1], [0, 2], [0, 3]]
elif stuff_id == 1:
ptr = [[0, 0], [0, 1], [0, 2], [1, 0]]
elif stuff_id == 2:
ptr = [[0, 1], [0, 0], [0, 2], [1, 1]]
elif stuff_id == 3:
ptr = [[0, 2], [0, 1], [0, 0], [1, 2]]
elif stuff_id == 4:
ptr = [[0, 0], [0, 1], [1, 0], [1, 1]]
elif stuff_id == 5:
ptr = [[0, 1], [0, 0], [1, 1], [1, 2]]
else:
ptr = [[0, 1], [0, 2], [1, 0], [1, 1]]
return ptr
class Stuff:
def __init__(self, stuff_id):
self.space = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 记录已固定的方块
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
self.id = stuff_id
self.ptr = []
self.new_stuff(stuff_id)
def new_stuff(self, stuff_id):
self.id = stuff_id
if self.id == 0:
self.ptr = [[0, 4], [0, 3], [0, 5], [0, 6]]
elif self.id == 1:
self.ptr = [[0, 4], [0, 3], [0, 5], [1, 3]]
elif self.id == 2:
self.ptr = [[0, 4], [0, 3], [0, 5], [1, 4]]
elif self.id == 3:
self.ptr = [[0, 4], [0, 5], [0, 3], [1, 5]]
elif self.id == 4:
self.ptr = [[0, 4], [0, 3], [1, 3], [1, 4]]
elif self.id == 5:
self.ptr = [[0, 4], [0, 3], [1, 4], [1, 5]]
else:
self.ptr = [[0, 4], [0, 5], [1, 3], [1, 4]]
def crash(self):
"""
return: 0: no crash
1: down crash
2: up crash
3: left crash
4: right crash
5: note crash
"""
for i in range(4):
if self.ptr[i][0] > gridSize[1]-1:
return 1
elif self.ptr[i][0] < 0:
return 2
elif self.ptr[i][1] > gridSize[0]-1:
return 3
elif self.ptr[i][1] < 0:
return 4
elif self.space[self.ptr[i][0]][self.ptr[i][1]] != 0:
return 5
return 0
def fix_stuff(self): # 固定
for ptr in self.ptr:
self.space[ptr[0]][ptr[1]] = self.id + 1
del_list = []
for i in range(len(self.space)): # 判断是否满行
flag = True
for g in self.space[i]:
if g == 0:
flag = False
break
if flag:
del_list.append(i)
for i in del_list:
del self.space[i]
self.space.insert(0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
# return len(del_list) 此处可加一个返回,用于方便计算分数
def down(self):
temp_ptr = copy.deepcopy(self.ptr)
for i in range(4):
self.ptr[i][0] += 1
crash_result = self.crash()
if crash_result != 0:
self.ptr = copy.deepcopy(temp_ptr)
return False
return True
def up(self):
temp_ptr = copy.deepcopy(self.ptr)
for i in range(4):
self.ptr[i][0] -= 1
crash_result = self.crash()
if crash_result != 0:
self.ptr = copy.deepcopy(temp_ptr)
return False
return True
def left(self):
temp_ptr = copy.deepcopy(self.ptr)
for i in range(4):
self.ptr[i][1] -= 1
crash_result = self.crash()
if crash_result != 0:
self.ptr = copy.deepcopy(temp_ptr)
return False
return True
def right(self):
temp_ptr = copy.deepcopy(self.ptr)
for i in range(4):
self.ptr[i][1] += 1
crash_result = self.crash()
if crash_result != 0:
self.ptr = copy.deepcopy(temp_ptr)
return False
return True
def rotate(self): # 旋转方块
temp_ptr = copy.deepcopy(self.ptr)
for i in range(1, 4):
temp_y, temp_x = temp_ptr[0][0], temp_ptr[0][1]
i_y, i_x = temp_ptr[i][0], temp_ptr[i][1]
self.ptr[i] = [temp_ptr[0][0] - temp_ptr[i][1] + temp_ptr[0][1], # 逆时针旋转
temp_ptr[0][1] + temp_ptr[i][0] - temp_ptr[0][0]]
# self.ptr[i][0] = temp_ptr[0][0] + temp_ptr[i][1] - temp_ptr[0][1]
crash_result = self.crash()
if crash_result == 0:
return True
elif crash_result == 1:
if self.up():
return True
elif crash_result == 2:
if self.down():
return True
elif crash_result == 3:
if self.left():
return True
elif crash_result == 4:
if self.right():
return True
self.ptr = copy.deepcopy(temp_ptr)
return False
def over(self): #游戏结束判断
for i in range(1):
for value in self.space[i]:
if value != 0:
return True
return False
def show_text(screen, pos, text, text_color, font_bold=False, font_size=60, font_italic=False, font_mediate=True):
# 获取系统字体,并设置文字大小
cur_font = pygame.font.SysFont("宋体", font_size)
# 设置是否加粗属性
cur_font.set_bold(font_bold)
# 设置是否斜体属性
cur_font.set_italic(font_italic)
# 设置文字内容
text_fmt = cur_font.render(text, 1, text_color)
text_pos = text_fmt.get_rect()
text_pos.midtop = pos
# 绘制文字
if font_mediate:
# 判断是否居中
screen.blit(text_fmt, text_pos)
else:
screen.blit(text_fmt, pos)
def main():
pygame.init()
ftpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("Tetris")
GAME_STATUS = 0
next_stuff = choice(IDList)
curr_stuff = choice(IDList)
stuff = Stuff(curr_stuff)
next_ptr = stuff_list(next_stuff)
gamingTime = time.time()
while True:
screen.fill((150, 150, 150))
pygame.draw.rect(screen, (50, 50, 50), (50, 50, 500, 700))
pygame.draw.rect(screen, (100, 100, 100), (60, 60, 480, 80))
if GAME_STATUS == 0: # Ready
for event in pygame.event.get(): # 事件遍历
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN: # 按键按下
if event.key in [K_RETURN, K_KP_ENTER]:
GAME_STATUS = 1
show_text(screen, (400, 400), "Press enter to start game", (250, 250, 0), font_size=80)
elif GAME_STATUS == 1: # gaming
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key in [K_UP, K_w, K_SPACE]:
stuff.rotate()
if event.key in [K_LEFT, K_a]:
stuff.left()
if event.key in [K_RIGHT, K_d]:
stuff.right()
if event.key in [K_DOWN, K_s]: # 这里因为只判断了KEYDOWN,所以无论你按多久,都只会触发一次
stuff.down() # 如果想实现长按快速下落这个效果可以和KEYUP事件一起食用
if time.time() - gamingTime > 0.5:
gamingTime = time.time()
if not stuff.down():
stuff.fix_stuff()
curr_stuff = next_stuff
next_stuff = choice(IDList)
stuff.new_stuff(curr_stuff)
next_ptr = stuff_list(next_stuff)
for pos in next_ptr:
pygame.draw.rect(screen, ColorList[next_stuff], (601 + 50 * pos[1], 101 + 50 * pos[0], 48, 48))
for i in range(gridSize[1]):
for j in range(gridSize[0]):
ID = stuff.space[i][j]
if ID != 0:
pygame.draw.rect(screen, ColorList[ID - 1], (61 + 40 * j, 61 + 40 * i, 38, 38))
for pos in stuff.ptr:
pygame.draw.rect(screen, ColorList[stuff.id], (61 + 40 * pos[1], 61 + 40 * pos[0], 38, 38))
if stuff.over():
GAME_STATUS = 2
elif GAME_STATUS == 2: # Game over
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key in [K_RETURN, K_KP_ENTER]:
GAME_STATUS = 0
next_stuff = choice(IDList)
curr_stuff = choice(IDList)
stuff = Stuff(curr_stuff)
next_ptr = stuff_list(next_stuff)
show_text(screen, (400, 350), "GameOver", (250, 250, 0), font_size=80)
show_text(screen, (400, 450), "Press enter to start game", (250, 250, 0), font_size=80)
pygame.display.flip()
ftpsClock.tick(20) # 每秒20帧
if __name__ == '__main__':
main()
THE END......