当前位置:   article > 正文

Python俄罗斯方块改进版

python俄罗斯方块改进

1.加了方块预览部分

2.加了开始按钮

在公司实习抽空写的,呵呵。觉得Python还不错,以前觉得像个玩具语言。希望能够用它做更多大事吧!!!加油。

截图如下:

代码如下:

  1. #coding=utf-8
  2. from Tkinter import *;
  3. from random import *;
  4. import thread;
  5. from tkMessageBox import showinfo;
  6. import threading;
  7. from time import sleep;
  8. class BrickGame(object):
  9. #是否开始
  10. start = True;
  11. #是否到达底部
  12. isDown = True;
  13. #窗体
  14. window = None;
  15. #frame
  16. frame1 = None;
  17. frame2 = None;
  18. #按钮
  19. btnStart = None;
  20. #绘图类
  21. canvas = None;
  22. canvas1 = None;
  23. #标题
  24. title = "BrickGame";
  25. #宽和高
  26. width = 450;
  27. height = 670;
  28. #行和列
  29. rows = 20;
  30. cols = 10;
  31. #下降方块的线程
  32. downThread = None;
  33. #几种方块
  34. brick = [
  35. [
  36. [
  37. [1,1,1],
  38. [0,0,1],
  39. [0,0,0]
  40. ],
  41. [
  42. [0,0,1],
  43. [0,0,1],
  44. [0,1,1]
  45. ],
  46. [
  47. [0,0,0],
  48. [1,0,0],
  49. [1,1,1]
  50. ],
  51. [
  52. [1,1,0],
  53. [1,0,0],
  54. [1,0,0]
  55. ]
  56. ],
  57. [
  58. [
  59. [0,0,0],
  60. [0,1,1],
  61. [0,1,1]
  62. ],
  63. [
  64. [0,0,0],
  65. [0,1,1],
  66. [0,1,1]
  67. ],
  68. [
  69. [0,0,0],
  70. [0,1,1],
  71. [0,1,1]
  72. ],
  73. [
  74. [0,0,0],
  75. [0,1,1],
  76. [0,1,1]
  77. ]
  78. ],
  79. [
  80. [
  81. [1,1,1],
  82. [0,1,0],
  83. [0,1,0]
  84. ],
  85. [
  86. [0,0,1],
  87. [1,1,1],
  88. [0,0,1]
  89. ],
  90. [
  91. [0,1,0],
  92. [0,1,0],
  93. [1,1,1]
  94. ],
  95. [
  96. [1,0,0],
  97. [1,1,1],
  98. [1,0,0]
  99. ]
  100. ],
  101. [
  102. [
  103. [0,1,0],
  104. [0,1,0],
  105. [0,1,0]
  106. ],
  107. [
  108. [0,0,0],
  109. [1,1,1],
  110. [0,0,0]
  111. ],
  112. [
  113. [0,1,0],
  114. [0,1,0],
  115. [0,1,0]
  116. ],
  117. [
  118. [0,0,0],
  119. [1,1,1],
  120. [0,0,0]
  121. ]
  122. ]
  123. ];
  124. #当前的方块
  125. curBrick = None;
  126. #当前方块数组
  127. arr = None;
  128. arr1 = None;
  129. #当前方块形状
  130. shape = -1;
  131. #当前方块的行和列(最左上角)
  132. curRow = -10;
  133. curCol = -10;
  134. #背景
  135. back = list();
  136. #格子
  137. gridBack = list();
  138. preBack = list();
  139. #初始化
  140. def init(self):
  141. for i in range(0,self.rows):
  142. self.back.insert(i,list());
  143. self.gridBack.insert(i,list());
  144. for i in range(0,self.rows):
  145. for j in range(0,self.cols):
  146. self.back[i].insert(j,0);
  147. self.gridBack[i].insert(j,self.canvas.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black"));
  148. for i in range(0,3):
  149. self.preBack.insert(i,list());
  150. for i in range(0,3):
  151. for j in range(0,3):
  152. self.preBack[i].insert(j,self.canvas1.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black"));
  153. #绘制游戏的格子
  154. def drawRect(self):
  155. for i in range(0,self.rows):
  156. for j in range(0,self.cols):
  157. if self.back[i][j]==1:
  158. self.canvas.itemconfig(self.gridBack[i][j],fill="blue",outline="white");
  159. elif self.back[i][j]==0:
  160. self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white");
  161. #绘制预览方块
  162. for i in range(0,len(self.arr1)):
  163. for j in range(0,len(self.arr1[i])):
  164. if self.arr1[i][j]==0:
  165. self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white");
  166. elif self.arr1[i][j]==1:
  167. self.canvas1.itemconfig(self.preBack[i][j],fill="orange",outline="white");
  168. #绘制当前正在运动的方块
  169. if self.curRow!=-10 and self.curCol!=-10:
  170. for i in range(0,len(self.arr)):
  171. for j in range(0,len(self.arr[i])):
  172. if self.arr[i][j]==1:
  173. self.canvas.itemconfig(self.gridBack[self.curRow+i][self.curCol+j],fill="blue",outline="white");
  174. #判断方块是否已经运动到达底部
  175. if self.isDown:
  176. for i in range(0,3):
  177. for j in range(0,3):
  178. if self.arr[i][j]!=0:
  179. self.back[self.curRow+i][self.curCol+j] = self.arr[i][j];
  180. #判断整行消除
  181. self.removeRow();
  182. #判断是否死了
  183. self.isDead();
  184. #获得下一个方块
  185. self.getCurBrick();
  186. #判断是否有整行需要消除
  187. def removeRow(self):
  188. for i in range(0,self.rows):
  189. tag1 = True;
  190. for j in range(0,self.cols):
  191. if self.back[i][j]==0:
  192. tag1 = False;
  193. break;
  194. if tag1==True:
  195. #从上向下挪动
  196. for m in xrange(i-1,0,-1):
  197. for n in range(0,self.cols):
  198. self.back[m+1][n] = self.back[m][n];
  199. #获得当前的方块
  200. def getCurBrick(self):
  201. self.curBrick = randint(0,len(self.brick)-1);
  202. self.shape = 0;
  203. #当前方块数组
  204. self.arr = self.brick[self.curBrick][self.shape];
  205. self.arr1 = self.arr;
  206. self.curRow = 0;
  207. self.curCol = 1;
  208. #是否到底部为False
  209. self.isDown = False;
  210. #监听键盘输入
  211. def onKeyboardEvent(self,event):
  212. #未开始,不必监听键盘输入
  213. if self.start == False:
  214. return;
  215. #记录原来的值
  216. tempCurCol = self.curCol;
  217. tempCurRow = self.curRow;
  218. tempShape = self.shape;
  219. tempArr = self.arr;
  220. direction = -1;
  221. if event.keycode==37:
  222. #左移
  223. self.curCol-=1;
  224. direction = 1;
  225. elif event.keycode==38:
  226. #变化方块的形状
  227. self.shape+=1;
  228. direction = 2;
  229. if self.shape>=4:
  230. self.shape=0;
  231. self.arr = self.brick[self.curBrick][self.shape];
  232. elif event.keycode==39:
  233. direction = 3;
  234. #右移
  235. self.curCol+=1;
  236. elif event.keycode==40:
  237. direction = 4;
  238. #下移
  239. self.curRow+=1;
  240. if self.isEdge(direction)==False:
  241. self.curCol = tempCurCol;
  242. self.curRow = tempCurRow;
  243. self.shape = tempShape;
  244. self.arr = tempArr;
  245. self.drawRect();
  246. return True;
  247. #判断当前方块是否到达边界
  248. def isEdge(self,direction):
  249. tag = True;
  250. #向左,判断边界
  251. if direction==1:
  252. for i in range(0,3):
  253. for j in range(0,3):
  254. if self.arr[j][i]!=0 and (self.curCol+i<0 or self.back[self.curRow+j][self.curCol+i]!=0):
  255. tag = False;
  256. break;
  257. #向右,判断边界
  258. elif direction==3:
  259. for i in range(0,3):
  260. for j in range(0,3):
  261. if self.arr[j][i]!=0 and (self.curCol+i>=self.cols or self.back[self.curRow+j][self.curCol+i]!=0):
  262. tag = False;
  263. break;
  264. #向下,判断底部
  265. elif direction==4:
  266. for i in range(0,3):
  267. for j in range(0,3):
  268. if self.arr[i][j]!=0 and (self.curRow+i>=self.rows or self.back[self.curRow+i][self.curCol+j]!=0):
  269. tag = False;
  270. self.isDown = True;
  271. break;
  272. #进行变形,判断边界
  273. elif direction==2:
  274. if self.curCol<0:
  275. self.curCol=0;
  276. if self.curCol+2>=self.cols:
  277. self.curCol = self.cols-3;
  278. if self.curRow+2>=self.rows:
  279. self.curRow = self.curRow-3;
  280. return tag;
  281. #方块向下移动
  282. def brickDown(self):
  283. while True:
  284. if self.start==False:
  285. print("exit thread");
  286. break;
  287. tempRow = self.curRow;
  288. self.curRow+=1;
  289. if self.isEdge(4)==False:
  290. self.curRow = tempRow;
  291. self.drawRect();
  292. #每一秒下降一格
  293. sleep(1);
  294. #点击开始
  295. def clickStart(self):
  296. self.start = True;
  297. for i in range(0,self.rows):
  298. for j in range(0,self.cols):
  299. self.back[i][j] = 0;
  300. self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white");
  301. for i in range(0,len(self.arr)):
  302. for j in range(0,len(self.arr[i])):
  303. self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white");
  304. self.getCurBrick();
  305. self.drawRect();
  306. self.downThread = threading.Thread(target=self.brickDown,args=());
  307. self.downThread.start();
  308. #判断是否死了
  309. def isDead(self):
  310. for j in range(0,len(self.back[0])):
  311. if self.back[0][j]!=0:
  312. showinfo("提示","你挂了,再来一盘吧!");
  313. self.start = False;
  314. break;
  315. #运行
  316. def __init__(self):
  317. self.window = Tk();
  318. self.window.title(self.title);
  319. self.window.minsize(self.width,self.height);
  320. self.window.maxsize(self.width,self.height);
  321. self.frame1 = Frame(self.window,width=300,height=600,bg="black");
  322. self.frame1.place(x=20,y=30);
  323. self.frame2 = Frame(self.window,width=90,height=90,bg="black");
  324. self.frame2.place(x=340,y=60);
  325. self.canvas = Canvas(self.frame1,width=300,height=600,bg="black");
  326. self.canvas1 = Canvas(self.frame2,width=90,height=90,bg="black");
  327. self.btnStart = Button(self.window,text="开始",command=self.clickStart);
  328. self.btnStart.place(x=340,y=400,width=80,height=25);
  329. self.init();
  330. #获得当前的方块
  331. self.getCurBrick();
  332. #按照数组,绘制格子
  333. self.drawRect();
  334. self.canvas.pack();
  335. self.canvas1.pack();
  336. #监听键盘事件
  337. self.window.bind("<KeyPress>",self.onKeyboardEvent);
  338. #启动方块下落线程
  339. self.downThread = threading.Thread(target=self.brickDown,args=());
  340. self.downThread.start();
  341. self.window.mainloop();
  342. self.start=False;
  343. pass;
  344. if __name__=='__main__':
  345. brickGame = BrickGame();


 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号