当前位置:   article > 正文

pygame实现俄罗斯方块_pygame俄罗斯方块代码

pygame俄罗斯方块代码

使用pygame实现简单的俄罗斯方块,实现了强降、预降位置显示等功能。若键盘无法控制,将输入法调为英文。

俄罗斯方块1.0演示

  1. import os
  2. import pygame,sys,random,copy,time
  3. pygame.init()
  4. pygame.mixer.init()
  5. pygame.display.set_caption('俄罗斯方块1.0')
  6. '''操作设置'''
  7. left = pygame.K_LEFT
  8. right = pygame.K_RIGHT
  9. down = pygame.K_DOWN
  10. spi_r = pygame.K_UP #右旋-上键
  11. spi_l = pygame.K_g #左旋-G
  12. change = pygame.K_d #保留-D
  13. plunge = pygame.K_f #强降-F
  14. again = pygame.K_2 #重新游戏
  15. leve = 1 #难度等级,数字越大,下落越快
  16. '''定义屏幕大小'''
  17. width = 500
  18. height = 700
  19. screen = pygame.display.set_mode((500,700))#屏幕大小
  20. '''基础方块绘制'''
  21. size=30#方块规格
  22. sore = 10#分数单位
  23. sores = [0]#记录总分
  24. #基础方块的绘制
  25. face = pygame.Surface((size,size),flags=pygame.HWSURFACE)
  26. # face.fill("white")
  27. pygame.draw.rect(face, (0, 0, 0), [0,0,size,size],1)
  28. # pygame.draw.circle(face, (255, 0, 0), (15,15), 15, width=1)
  29. #基础方块的投影绘制--增加透明度
  30. face1 = pygame.Surface((size,size),flags=pygame.HWSURFACE)
  31. # face1.fill((255,255,255,0))
  32. pygame.draw.rect(face1, (0, 0, 0,0), [0,0,size,size],1)
  33. # pygame.draw.circle(face1, (255, 0, 0,0), (15,15), 15, width=1)
  34. face1.set_alpha(100)#设置透明度
  35. #字体设置
  36. f = pygame.font.SysFont(['方正粗黑宋简体'],30)#分数显示等需要用的字体
  37. f1 = pygame.font.SysFont(['方正粗黑宋简体'],20)
  38. f2 = pygame.font.SysFont(['方正粗黑宋简体'],15)
  39. '''地图参数设置'''
  40. origin=(5,5) #原点位置,边框的宽度
  41. set = [4*size+origin[0],origin[1]+0] #初始生成方块位置,该坐标全局控制当前形状的位置
  42. c_list=(2,4,6,7,14,16,19)#分别为7种大类型的方块,见方块类型函数
  43. ch1 = [ c_list[random.randint(0,6)] for i in range(4)] #初始方块产生随机函数
  44. ch1.append(0)#ch1为一个有装4个值的列表,前三个为即将出现的三个形状的标号,第四个为保留的形状的标号,开始时没有,定为0
  45. filled = []#落地方块储存列表,将一落地的每一个形状的信息存入
  46. n=[0]#控制是否能更换当前手中方块
  47. control = [1]#控制游戏结束,0为结束
  48. '''方块类型函数'''
  49. #每个形状表示为四个坐标,取其中一个方块为原点算出其他方块坐标
  50. #c为方块类型标号,a为形状的坐标。
  51. # 该函数最终输出一个列表,其中包含6个坐标,即为一个形状(包含四个基础方块)的四个方块坐标和四个方块中最小的x,y和最大的x,y,方便之后的碰撞判断
  52. #还要输出形状标号c(方便进行不同方块的不同颜色绘制)
  53. def forms(c,a):
  54. #长条
  55. if c == 1:
  56. x0 = 0
  57. y0 = 0
  58. x1 = 0
  59. y1 = -1
  60. x2 = 0
  61. y2 = 2
  62. x3 = 0
  63. y3 = 1
  64. if c == 2:#横条
  65. x0 = 0
  66. y0 = 0
  67. x1 = -1
  68. y1 = 0
  69. x2 = 1
  70. y2 = 0
  71. x3 = 2
  72. y3 = 0
  73. # S
  74. if c==3:
  75. x0 = 0
  76. y0 = 0
  77. x1 = 0
  78. y1 = 1
  79. x2 = -1
  80. y2 = 0
  81. x3 = -1
  82. y3 = -1
  83. if c == 4:
  84. x0 = 0
  85. y0 = 0
  86. x1 = -1
  87. y1 = 0
  88. x2 = 0
  89. y2 = -1
  90. x3 = 1
  91. y3 = -1
  92. #Z
  93. if c==5:
  94. x0 = 0
  95. y0 = 0
  96. x1 = 1
  97. y1 = 0
  98. x2 = 0
  99. y2 = 1
  100. x3 = 1
  101. y3 = -1
  102. if c==6:
  103. x0 = 0
  104. y0 = 0
  105. x1 = -1
  106. y1 = 0
  107. x2 = 0
  108. y2 = 1
  109. x3 = 1
  110. y3 = 1
  111. #T
  112. if c==7:
  113. x0 = 0
  114. y0 = 0
  115. x1 = 1
  116. y1 = 0
  117. x2 = -1
  118. y2 = 0
  119. x3 = 0
  120. y3 = -1
  121. if c==8:
  122. x0 = 0
  123. y0 = 0
  124. x1 = 0
  125. y1 = -1
  126. x2 = 0
  127. y2 = 1
  128. x3 = 1
  129. y3 = 0
  130. if c==9:
  131. x0 = 0
  132. y0 = 0
  133. x1 = -1
  134. y1 = 0
  135. x2 = 0
  136. y2 = 1
  137. x3 = 1
  138. y3 = 0
  139. if c==10:
  140. x0 = 0
  141. y0 = 0
  142. x1 = 0
  143. y1 = 1
  144. x2 = 0
  145. y2 = -1
  146. x3 = -1
  147. y3 = 0
  148. #L
  149. if c==11:
  150. x0 = 0
  151. y0 = 0
  152. x1 = 0
  153. y1 = -1
  154. x2 = 0
  155. y2 = 1
  156. x3 = 1
  157. y3 = 1
  158. if c==12:
  159. x0 = 0
  160. y0 = 0
  161. x1 = -1
  162. y1 = 0
  163. x2 = 1
  164. y2 = 0
  165. x3 = -1
  166. y3 = 1
  167. if c==13:
  168. x0 = 0
  169. y0 = 0
  170. x1 = 0
  171. y1 = -1
  172. x2 = 0
  173. y2 = 1
  174. x3 = -1
  175. y3 = -1
  176. if c==14:
  177. x0 = 0
  178. y0 = 0
  179. x1 = -1
  180. y1 = 0
  181. x2 = 1
  182. y2 = 0
  183. x3 = 1
  184. y3 = -1
  185. #J
  186. if c==15:
  187. x0 = 0
  188. y0 = 0
  189. x1 = 0
  190. y1 = -1
  191. x2 = 0
  192. y2 = 1
  193. x3 = -1
  194. y3 = 1
  195. if c==16:
  196. x0 = 0
  197. y0 = 0
  198. x1 = -1
  199. y1 = -1
  200. x2 = -1
  201. y2 = 0
  202. x3 = 1
  203. y3 = 0
  204. if c==17:
  205. x0 = 0
  206. y0 = 0
  207. x1 = 0
  208. y1 = 1
  209. x2 = 0
  210. y2 = -1
  211. x3 = 1
  212. y3 = -1
  213. if c==18:
  214. x0 = 0
  215. y0 = 0
  216. x1 = -1
  217. y1 = 0
  218. x2 = 1
  219. y2 = 0
  220. x3 = 1
  221. y3 = 1
  222. # 田
  223. if c == 19:
  224. x0 = 0
  225. y0 = 0
  226. x1 = 0
  227. y1 = 1
  228. x2 = -1
  229. y2 = 0
  230. x3 = -1
  231. y3 = 1
  232. mx = max(x0,x1,x2,x3)
  233. my = max(y0,y1,y2,y3)
  234. mx1 = min(x0, x1, x2,x3)
  235. my1 = min(y0, y1, y2,y3)
  236. Z=[[x0*size+a[0],y0*size+a[1]],[x1*size+a[0],y1*size+a[1]],[x2*size+a[0],y2*size+a[1]],[x3*size+a[0],y3*size+a[1]],[mx1*size+a[0],my1*size+a[1]],[mx*size+a[0],my*size+a[1]],c]
  237. return Z
  238. '''屏幕更新函数'''
  239. """
  240. 屏幕动态更新的核心函数
  241. 实时更新形状位置,分数,预览形状等都在该函数完成
  242. 大概思路:
  243. 1每一次都使用新的空白页面(粉色覆盖,绘制边框)覆盖之前的内容,再
  244. 2再绘制新的实时内容
  245. a、根据filled列表中储存的形状坐标信息绘制已经降落的形状
  246. b、根据ch1列表中储存的实时形状(正在下落的形状,预降位置,预览的形状)
  247. c、根据sores列表记录的分数绘制新的分数
  248. """
  249. def update():
  250. # 更新前进行碰撞判断
  251. isclear(filled)
  252. # 游戏框绘制
  253. screen.fill("pink")
  254. pygame.draw.line(screen, (0, 0, 250), (0, 0), (size * 10 + 10, 0), 10)
  255. pygame.draw.line(screen, (0, 0, 250), (0, 0), (0, size * 20 + 10), 10)
  256. pygame.draw.line(screen, (0, 0, 250), (size * 10+10 , 0), (size * 10 +10, size * 20+15 ), 10)
  257. pygame.draw.line(screen, (0, 0, 250), (0, 10+size * 20), (size * 10 + 10, size * 20+10 ), 10)
  258. pygame.draw.line(screen, (0, 0, 250), (10*size+origin[0]+10,100+size), (width,100+size), 5)
  259. pygame.draw.line(screen, (0, 0, 250), (10 * size + origin[0] + 10, 100 + 5*size), (width, 100 + 5*size), 5)
  260. pygame.draw.line(screen, (0, 0, 250), (10 * size + origin[0] + 10, 100 + 9 * size), (width, 100 + 9 * size), 5)
  261. pygame.draw.line(screen, (0, 0, 100), (10 * size + origin[0] + 10, 100 + 13 * size), (width, 100 + 13 * size), 5)
  262. pygame.draw.line(screen, (0, 0, 100), (10 * size + origin[0] + 10, 100 + 17 * size), (width, 100 + 17 * size), 5)
  263. # 操作解释绘制
  264. sor = f2.render("操作介绍:", True, (0, 100, 10))
  265. screen.blit(sor, (20, 610))
  266. sor = f1.render("左:" + pygame.key.name(left), True, (0, 0, 0))
  267. screen.blit(sor, (50, 630))
  268. sor = f1.render("右:" + pygame.key.name(right), True, (0, 0, 0))
  269. screen.blit(sor, (150, 630))
  270. sor = f1.render("下:" + pygame.key.name(down), True, (0, 0, 0))
  271. screen.blit(sor, (250, 630))
  272. sor = f1.render("右旋:" + pygame.key.name(spi_r), True, (0, 0, 0))
  273. screen.blit(sor, (50, 660))
  274. sor = f1.render("左旋:" + pygame.key.name(spi_l), True, (0, 0, 0))
  275. screen.blit(sor, (150, 660))
  276. sor = f1.render("强降:" + pygame.key.name(plunge), True, (0, 0, 0))
  277. screen.blit(sor, (250, 660))
  278. sor = f1.render("保留:" + pygame.key.name(change), True, (0, 0, 0))
  279. screen.blit(sor, (360, 660))
  280. # 方块颜色控制函数
  281. '''
  282. c表示方块的编号
  283. face表示前面定义的基础方块
  284. '''
  285. def d_color(c, face=face):
  286. a = -1
  287. colors = ((0, 160, 233), (34, 172, 56), (230, 0, 18), (146, 7, 131), (235, 97, 0), (0, 71, 157), (243, 152, 0))
  288. if c == 1 or c == 2 or c == -2:
  289. a = 0
  290. face.fill(colors[0])
  291. if c == 3 or c == 4:
  292. face.fill(colors[1])
  293. a = 1
  294. if c == 5 or c == 6:
  295. a = 2
  296. face.fill(colors[2])
  297. if c >= 7 and c <= 10:
  298. a = 3
  299. face.fill(colors[3])
  300. if c >= 11 and c <= 14:
  301. a = 4
  302. face.fill(colors[4])
  303. if c >= 15 and c <= 18:
  304. a = 5
  305. face.fill(colors[5])
  306. if c == 19:
  307. a = 6
  308. face.fill(colors[6])
  309. pygame.draw.rect(face, (255,255,255), [0, 0, size, size], 1)
  310. #当前正在下落的方块绘制
  311. d_color(ch1[0], face=face)
  312. for i in range(4):
  313. screen.blit(face,forms(ch1[0],set)[i])
  314. #计算预降位置,找到当前形状与已落地方块的距离
  315. a = 19 * size - forms(ch1[0], set)[5][1] + origin[1]
  316. for i in filled:
  317. for j in range(4):
  318. for k in range(4):
  319. if i[j][0] == forms(ch1[0], set)[k][0]:#x相同的时候,找到符合要求的最y值,即当前形状与已落地方块的距离
  320. if a > i[j][1] - forms(ch1[0], set)[k][1] - size:
  321. a = i[j][1] - forms(ch1[0], set)[k][1] - size
  322. # 方块预降位置绘制
  323. d_color(ch1[0], face=face1)
  324. for i in range(4):
  325. screen.blit(face1,forms(ch1[0],(set[0],set[1]+a))[i])
  326. #将出现的方块预览绘制,可以预览3个形状
  327. d_color(ch1[1], face=face)
  328. for i in range(4):
  329. screen.blit(face,forms(ch1[1],(330+2*size,120+2*size))[i])
  330. d_color(ch1[2], face=face)
  331. for i in range(4):
  332. screen.blit(face,forms(ch1[2],(330+2*size,120+6*size))[i])
  333. d_color(ch1[3], face=face)
  334. for i in range(4):
  335. screen.blit(face,forms(ch1[3],(330+2*size,120+10*size))[i])
  336. # 有保留的形状时绘制,其值为0时没有保留的形状
  337. sor = f.render("retain", True, (0, 0, 0))
  338. screen.blit(sor, (10*size+20, 90 + 13 * size))
  339. if ch1[4]!=0:
  340. d_color(ch1[4], face=face)
  341. for i in range(4):
  342. screen.blit(face,forms(ch1[4],(330+2*size,120+14*size))[i])
  343. #已经落地方块绘制
  344. for i in filled:
  345. d_color(i[-1], face=face)
  346. for j in range(4):
  347. screen.blit(face, i[j])
  348. #分数绘制
  349. sor = f.render("sore:"+str(sores[0]),True,(0,0,0),(246,127,240))
  350. screen.blit(sor, (335, 50))
  351. #判断游戏是否结束
  352. if iscrash(forms(ch1[0],set),filled)[0]==0 and forms(ch1[0],set)[5][1]==origin[1]:
  353. control[0]=0
  354. #绘制游戏结束界面并判断游戏是否继续
  355. if control[0]==0:
  356. sor1 = f1.render("game over!!", True, (255, 0, 0), (255, 255, 255))
  357. screen.blit(sor1, (100, 300))
  358. for event in pygame.event.get():
  359. if event.type == pygame.QUIT:
  360. pygame.quit()
  361. sys.exit()
  362. if event.type == pygame.KEYDOWN:
  363. if event.key == again:
  364. filled.clear()
  365. set[0]=origin[0]+4*size
  366. set[1]=origin[1]+0
  367. sores[0]=0
  368. control[0]=1
  369. pygame.display.flip()#更新屏幕
  370. """方块控制函数"""
  371. '''
  372. 控制原理为改变set列表中的横纵坐标对形状的位置进行控制
  373. 每次移动完成要及时进行碰撞判断(无论是否有按键事件都要判断)
  374. 但该版本还未解决长按连续移动的问题
  375. '''
  376. def move():
  377. #获取键盘事件
  378. for event in pygame.event.get():
  379. if event.type == pygame.QUIT:
  380. pygame.quit()
  381. sys.exit()
  382. if event.type == pygame.KEYDOWN:
  383. if event.key== left:#左移动
  384. set[0] -= size
  385. if iscrash(forms(ch1[0], set), filled)[2] == 2:
  386. set[0] += size
  387. if event.key == right:
  388. set[0] += size
  389. if iscrash(forms(ch1[0], set), filled)[1] == 1:
  390. set[0] -= size
  391. if event.key == down:#加速下降
  392. set[1] += size
  393. if event.key == spi_r:#右旋
  394. m = ch1[0]
  395. spin(0)
  396. for i in filled:
  397. for j in range(4):
  398. for k in range(4):
  399. if i[j][0]==forms(ch1[0],set)[k][0] and i[j][1]==forms(ch1[0],set)[k][1]:
  400. ch1[0]=m
  401. if event.key == spi_l:#左旋
  402. m = ch1[0]
  403. spin(1)
  404. for i in filled:
  405. for j in range(4):
  406. for k in range(4):
  407. if i[j][0]==forms(ch1[0],set)[k][0] and i[j][1]==forms(ch1[0],set)[k][1]:
  408. ch1[0]=m
  409. if event.key == change:#保留
  410. if n[0]==0:
  411. if ch1[4]==0:
  412. if ch1[0] == 19:
  413. ch1[4]=19
  414. if ch1[0]>=1 and ch1[0]<=2:
  415. ch1[4] = 2
  416. if ch1[0]>=3 and ch1[0]<=4:
  417. ch1[4] = 4
  418. if ch1[0]>=5 and ch1[0]<=6:
  419. ch1[4] = 6
  420. if ch1[0]>=7 and ch1[0]<=10:
  421. ch1[4] = 7
  422. if ch1[0]>=11 and ch1[0]<=14:
  423. ch1[4] = 14
  424. if ch1[0]>=15 and ch1[0]<=18:
  425. ch1[4] = 16
  426. ch1[0] = ch1[1]
  427. ch1[1]=ch1[2]
  428. ch1[2]=ch1[3]
  429. ch1[3]=c_list[random.randint(0,6)]
  430. set[0] =4 * size + origin[0]
  431. set[1]=origin[1] + 0
  432. n[0]=1
  433. else:
  434. v=ch1[0]
  435. ch1[0] = ch1[4]
  436. if v == 19:
  437. ch1[4]=19
  438. if v>=1 and v<=2:
  439. ch1[4] = 2
  440. if v>=3 and v<=4:
  441. ch1[4] = 4
  442. if v>=5 and v<=6:
  443. ch1[4] = 6
  444. if v>=7 and v<=10:
  445. ch1[4] = 7
  446. if v>=11 and v<=14:
  447. ch1[4] = 14
  448. if v>=15 and v<=18:
  449. ch1[4] = 16
  450. set[0] = 4 * size + origin[0]
  451. set[1] = origin[1] + 0
  452. n[0] = 1
  453. if event.key == plunge:#强降
  454. #原理也是计算当前方块落地的最小距离
  455. a=20*size-forms(ch1[0],set)[5][1]+origin[1]
  456. for i in filled:
  457. for j in range(4):
  458. for k in range(4):
  459. if i[j][0]==forms(ch1[0],set)[k][0]:
  460. if a>i[j][1]-forms(ch1[0],set)[k][1]:
  461. a=i[j][1]-forms(ch1[0],set)[k][1]
  462. set[1] += a
  463. # 判断碰撞情况
  464. if forms(ch1[0], set)[4][0] < origin[0]:
  465. set[0] += size
  466. if forms(ch1[0], set)[5][0] > origin[0] + 9 * size:
  467. set[0] -= size
  468. # 落地判断
  469. if forms(ch1[0], set)[5][1] > origin[1] + 19 * size:
  470. set[1] -= size
  471. filled.append(copy.deepcopy(forms(ch1[0], set)))
  472. set[0] = 4 * size + origin[0]
  473. set[1] = origin[1] + 0
  474. ch1[0] = ch1[1]
  475. ch1[1] = ch1[2]
  476. ch1[2] = ch1[3]
  477. ch1[3] = c_list[random.randint(0, 6)]
  478. n[0] = 0
  479. #方块间的碰撞判断
  480. if iscrash(forms(ch1[0], set), filled)[0] == 0:
  481. set[1] -= size
  482. filled.append(copy.deepcopy(forms(ch1[0], set)))
  483. set[0] = 4 * size + origin[0]
  484. set[1] = origin[1] + 0
  485. ch1[0] = ch1[1]
  486. ch1[1] = ch1[2]
  487. ch1[2] = ch1[3]
  488. ch1[3] = c_list[random.randint(0, 6)]
  489. n[0] = 0
  490. """方块间的碰撞判断函数"""
  491. def iscrash(form, form1):
  492. a, b, m = 10, 10, 10
  493. # 上下碰撞
  494. for i in form1:
  495. for c in range(4):
  496. for j in range(4):
  497. if i[c][0] == form[j][0] and i[c][1] - form[j][1] == 0:
  498. a = 0
  499. # 碰right
  500. for i in form1:
  501. for c in range(4):
  502. for j in range(4):
  503. if i[c][0] - form[j][0] == 0 and i[c][1] - form[j][1] == 0:
  504. b = 1
  505. # 碰left
  506. for i in form1:
  507. for c in range(4):
  508. for j in range(4):
  509. if form[j][0] - i[c][0] == 0 and i[c][1] - form[j][1] == 0:
  510. m = 2
  511. return a, b, m
  512. """形状旋转命令函数"""
  513. """"
  514. 输入旋转命令(在move函数中调用),
  515. 直接改变ch1[0]的值(正在下落的形状标号),及直接改变当前形状
  516. """
  517. def spin(a):
  518. if a==0:#右旋
  519. if ch1[0]==1:
  520. ch1[0]=2
  521. return ch1[0]
  522. if ch1[0]==2:
  523. ch1[0]=1
  524. return ch1[0]
  525. if ch1[0]==3:
  526. ch1[0]=4
  527. return ch1[0]
  528. if ch1[0]==4:
  529. ch1[0]=3
  530. return ch1[0]
  531. if ch1[0]==5:
  532. ch1[0]=6
  533. return ch1[0]
  534. if ch1[0]==6:
  535. ch1[0]=5
  536. return ch1[0]
  537. if ch1[0]==7:
  538. ch1[0]=8
  539. return ch1[0]
  540. if ch1[0] == 8:
  541. ch1[0] = 9
  542. return ch1[0]
  543. if ch1[0] == 9:
  544. ch1[0] = 10
  545. return ch1[0]
  546. if ch1[0] == 10:
  547. ch1[0] = 7
  548. return ch1[0]
  549. if ch1[0] == 11:
  550. ch1[0] = 12
  551. return ch1[0]
  552. if ch1[0] == 12:
  553. ch1[0] = 13
  554. return ch1[0]
  555. if ch1[0] == 13:
  556. ch1[0] = 14
  557. return ch1[0]
  558. if ch1[0] == 14:
  559. ch1[0] = 11
  560. return ch1[0]
  561. if ch1[0] == 15:
  562. ch1[0] = 16
  563. return ch1[0]
  564. if ch1[0] == 16:
  565. ch1[0] = 17
  566. return ch1[0]
  567. if ch1[0] == 17:
  568. ch1[0] = 18
  569. return ch1[0]
  570. if ch1[0] == 18:
  571. ch1[0] = 15
  572. return ch1[0]
  573. if ch1[0] == 19:
  574. ch1[0] = 19
  575. return ch1[0]
  576. if a==1:#左旋
  577. if ch1[0]==1:
  578. ch1[0]=2
  579. return ch1[0]
  580. if ch1[0]==2:
  581. ch1[0]=1
  582. return ch1[0]
  583. if ch1[0]==3:
  584. ch1[0]=4
  585. return ch1[0]
  586. if ch1[0]==4:
  587. ch1[0]=3
  588. return ch1[0]
  589. if ch1[0]==5:
  590. ch1[0]=6
  591. return ch1[0]
  592. if ch1[0]==6:
  593. ch1[0]=5
  594. return ch1[0]
  595. if ch1[0]==7:
  596. ch1[0]=10
  597. return ch1[0]
  598. if ch1[0] == 10:
  599. ch1[0] = 9
  600. return ch1[0]
  601. if ch1[0] == 9:
  602. ch1[0] = 8
  603. return ch1[0]
  604. if ch1[0] == 8:
  605. ch1[0] = 7
  606. return ch1[0]
  607. if ch1[0] == 11:
  608. ch1[0] = 14
  609. return ch1[0]
  610. if ch1[0] == 14:
  611. ch1[0] = 13
  612. return ch1[0]
  613. if ch1[0] == 13:
  614. ch1[0] = 12
  615. return ch1[0]
  616. if ch1[0] == 12:
  617. ch1[0] = 11
  618. return ch1[0]
  619. if ch1[0] == 15:
  620. ch1[0] = 18
  621. return ch1[0]
  622. if ch1[0] == 18:
  623. ch1[0] = 17
  624. return ch1[0]
  625. if ch1[0] == 17:
  626. ch1[0] = 16
  627. return ch1[0]
  628. if ch1[0] == 16:
  629. ch1[0] = 15
  630. return ch1[0]
  631. if ch1[0] == 19:
  632. ch1[0] = 19
  633. return ch1[0]
  634. '''#满行判断'''
  635. #通过及算每一行的横坐标之和来进行满行判断
  636. def isclear(form1):
  637. sor=0
  638. for j in range(20):
  639. sum = 0#保存一行的横坐标和
  640. c = j * size+origin[1]
  641. for i in form1:
  642. for k in range(4):
  643. if i[k][1] == c:
  644. sum+=i[k][0]
  645. if sum >= 45*size + origin[0]*10:#判断是否满行
  646. sor+=sore
  647. for i in form1:
  648. for k in range(4):
  649. if i[k][1]==c:
  650. i[k][1]=size*30#满行后将满行的y值赋值为size*30,在屏幕上就看不见了
  651. if i[k][1]<c:
  652. i[k][1]+=size#在满行上方的所有方块下移一格
  653. sores[0] += sor#记录满行数
  654. '''游戏主函数'''
  655. def main():
  656. while 1:
  657. #游戏总循环
  658. if control[0]==1:
  659. #游戏进行中循环
  660. # 此处使用两个循环解决操作与自动下落冲突问题
  661. while 1:
  662. t = time.time()
  663. while 1:
  664. move()
  665. update()
  666. if control[0] == 0:
  667. break
  668. if time.time() - t >= leve:#每隔leve下降一格
  669. t = time.time()
  670. set[1] += size
  671. break
  672. move()
  673. update()
  674. if control[0]==0:break
  675. if control[0] == 0:
  676. #游戏结束界面
  677. while 1:
  678. update()
  679. if control[0] == 1:break
  680. if __name__=="__main__":
  681. main()

还有很多不足之处,欢迎留言。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/832592
推荐阅读
相关标签
  

闽ICP备14008679号