python怎么实现俄罗斯方块游戏

这篇文章给大家分享的是有关python怎么实现俄罗斯方块游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

截图如下:

python怎么实现俄罗斯方块游戏

代码如下:

#coding=utf-8
fromTkinterimport*;
fromrandomimport*;
importthread;
fromtkMessageBoximportshowinfo;
importthreading;
fromtimeimportsleep;
classBrickGame(object):

#是否开始
start=True;
#是否到达底部
isDown=True;

#窗体
window=None;
#frame
frame1=None;
frame2=None;

#按钮
btnStart=None;

#绘图类
canvas=None;
canvas1=None;

#标题
title="BrickGame";
#宽和高
width=450;
height=670;

#行和列
rows=20;
cols=10;

#下降方块的线程
downThread=None;

#几种方块
brick=[

[
[
[1,1,1],
[0,0,1],
[0,0,0]
],
[
[0,0,1],
[0,0,1],
[0,1,1]
],
[
[0,0,0],
[1,0,0],
[1,1,1]
],
[
[1,1,0],
[1,0,0],
[1,0,0]
]
],
[
[
[0,0,0],
[0,1,1],
[0,1,1]
],
[
[0,0,0],
[0,1,1],
[0,1,1]
],
[
[0,0,0],
[0,1,1],
[0,1,1]
],
[
[0,0,0],
[0,1,1],
[0,1,1]
]
],
[
[
[1,1,1],
[0,1,0],
[0,1,0]
],
[
[0,0,1],
[1,1,1],
[0,0,1]
],
[
[0,1,0],
[0,1,0],
[1,1,1]
],
[
[1,0,0],
[1,1,1],
[1,0,0]
]
],
[
[
[0,1,0],
[0,1,0],
[0,1,0]
],
[
[0,0,0],
[1,1,1],
[0,0,0]
],
[
[0,1,0],
[0,1,0],
[0,1,0]
],
[
[0,0,0],
[1,1,1],
[0,0,0]
]
]
];

#当前的方块
curBrick=None;
#当前方块数组
arr=None;
arr1=None;
#当前方块形状
shape=-1;
#当前方块的行和列(最左上角)
curRow=-10;
curCol=-10;

#背景
back=list();
#格子
gridBack=list();
preBack=list();

#初始化
definit(self):

foriinrange(0,self.rows):

self.back.insert(i,list());
self.gridBack.insert(i,list());

foriinrange(0,self.rows):

forjinrange(0,self.cols):

self.back[i].insert(j,0);
self.gridBack[i].insert(j,self.canvas.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black"));

foriinrange(0,3):

self.preBack.insert(i,list());

foriinrange(0,3):

forjinrange(0,3):

self.preBack[i].insert(j,self.canvas1.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black"));

#绘制游戏的格子
defdrawRect(self):

foriinrange(0,self.rows):

forjinrange(0,self.cols):


ifself.back[i][j]==1:

self.canvas.itemconfig(self.gridBack[i][j],fill="blue",outline="white");

elifself.back[i][j]==0:

self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white");

#绘制预览方块
foriinrange(0,len(self.arr1)):

forjinrange(0,len(self.arr1[i])):

ifself.arr1[i][j]==0:

self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white");

elifself.arr1[i][j]==1:

self.canvas1.itemconfig(self.preBack[i][j],fill="orange",outline="white");


#绘制当前正在运动的方块
ifself.curRow!=-10andself.curCol!=-10:

foriinrange(0,len(self.arr)):

forjinrange(0,len(self.arr[i])):

ifself.arr[i][j]==1:

self.canvas.itemconfig(self.gridBack[self.curRow+i][self.curCol+j],fill="blue",outline="white");

#判断方块是否已经运动到达底部
ifself.isDown:

foriinrange(0,3):

forjinrange(0,3):

ifself.arr[i][j]!=0:

self.back[self.curRow+i][self.curCol+j]=self.arr[i][j];

#判断整行消除
self.removeRow();

#判断是否死了
self.isDead();

#获得下一个方块
self.getCurBrick();

#判断是否有整行需要消除
defremoveRow(self):

foriinrange(0,self.rows):

tag1=True;
forjinrange(0,self.cols):

ifself.back[i][j]==0:

tag1=False;
break;

iftag1==True:

#从上向下挪动
forminxrange(i-1,0,-1):

forninrange(0,self.cols):

self.back[m+1][n]=self.back[m][n];

#获得当前的方块
defgetCurBrick(self):

self.curBrick=randint(0,len(self.brick)-1);
self.shape=0;
#当前方块数组
self.arr=self.brick[self.curBrick][self.shape];
self.arr1=self.arr;

self.curRow=0;
self.curCol=1;

#是否到底部为False
self.isDown=False;

#监听键盘输入
defonKeyboardEvent(self,event):

#未开始,不必监听键盘输入
ifself.start==False:

return;

#记录原来的值
tempCurCol=self.curCol;
tempCurRow=self.curRow;
tempShape=self.shape;
tempArr=self.arr;
direction=-1;

ifevent.keycode==37:

#左移
self.curCol-=1;
direction=1;
elifevent.keycode==38:
#变化方块的形状
self.shape+=1;
direction=2;

ifself.shape>=4:

self.shape=0;
self.arr=self.brick[self.curBrick][self.shape];
elifevent.keycode==39:

direction=3;
#右移
self.curCol+=1;
elifevent.keycode==40:

direction=4;
#下移
self.curRow+=1;

ifself.isEdge(direction)==False:

self.curCol=tempCurCol;
self.curRow=tempCurRow;
self.shape=tempShape;
self.arr=tempArr;

self.drawRect();

returnTrue;

#判断当前方块是否到达边界
defisEdge(self,direction):

tag=True;

#向左,判断边界
ifdirection==1:

foriinrange(0,3):

forjinrange(0,3):

ifself.arr[j][i]!=0and(self.curCol+i<0orself.back[self.curRow+j][self.curCol+i]!=0):

tag=False;
break;
#向右,判断边界
elifdirection==3:

foriinrange(0,3):

forjinrange(0,3):

ifself.arr[j][i]!=0and(self.curCol+i>=self.colsorself.back[self.curRow+j][self.curCol+i]!=0):

tag=False;
break;
#向下,判断底部
elifdirection==4:

foriinrange(0,3):

forjinrange(0,3):

ifself.arr[i][j]!=0and(self.curRow+i>=self.rowsorself.back[self.curRow+i][self.curCol+j]!=0):

tag=False;
self.isDown=True;
break;
#进行变形,判断边界
elifdirection==2:

ifself.curCol<0:

self.curCol=0;

ifself.curCol+2>=self.cols:

self.curCol=self.cols-3;

ifself.curRow+2>=self.rows:

self.curRow=self.curRow-3;


returntag;

#方块向下移动
defbrickDown(self):

whileTrue:

ifself.start==False:

print("exitthread");
break;

tempRow=self.curRow;
self.curRow+=1;

ifself.isEdge(4)==False:

self.curRow=tempRow;

self.drawRect();

#每一秒下降一格
sleep(1);

#点击开始
defclickStart(self):

self.start=True;

foriinrange(0,self.rows):

forjinrange(0,self.cols):

self.back[i][j]=0;
self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white");

foriinrange(0,len(self.arr)):

forjinrange(0,len(self.arr[i])):

self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white");

self.getCurBrick();
self.drawRect();

self.downThread=threading.Thread(target=self.brickDown,args=());
self.downThread.start();

#判断是否死了
defisDead(self):

forjinrange(0,len(self.back[0])):

ifself.back[0][j]!=0:

showinfo("提示","你挂了,再来一盘吧!");
self.start=False;
break;

#运行
def__init__(self):

self.window=Tk();
self.window.title(self.title);
self.window.minsize(self.width,self.height);
self.window.maxsize(self.width,self.height);

self.frame1=Frame(self.window,width=300,height=600,bg="black");
self.frame1.place(x=20,y=30);

self.frame2=Frame(self.window,width=90,height=90,bg="black");
self.frame2.place(x=340,y=60);

self.canvas=Canvas(self.frame1,width=300,height=600,bg="black");
self.canvas1=Canvas(self.frame2,width=90,height=90,bg="black");

self.btnStart=Button(self.window,text="开始",command=self.clickStart);
self.btnStart.place(x=340,y=400,width=80,height=25);

self.init();

#获得当前的方块
self.getCurBrick();

#按照数组,绘制格子
self.drawRect();

self.canvas.pack();
self.canvas1.pack();

#监听键盘事件
self.window.bind("<KeyPress>",self.onKeyboardEvent);

#启动方块下落线程
self.downThread=threading.Thread(target=self.brickDown,args=());
self.downThread.start();

self.window.mainloop();

self.start=False;

pass;

if__name__=='__main__':

brickGame=BrickGame();

感谢各位的阅读!关于“python怎么实现俄罗斯方块游戏”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

发布于 2021-03-24 01:19:49
收藏
分享
海报
0 条评论
157
上一篇:python实现QQ邮箱发送邮件的方法 下一篇:如何借助Paramiko通过Python实现linux远程登陆
目录

    推荐阅读

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码