鱼C论坛

 找回密码
 立即注册
查看: 4699|回复: 34

[作品展示] 俄罗斯方块

[复制链接]
发表于 2021-3-20 08:18:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 考不好不改名 于 2021-4-5 08:20 编辑

在原版的基础上,自己增加了两种类型的方块

每次消除的方块越多,得分越多

代码需要安装pygame

本人能力有限,代码看上去可能流露出本人才疏学浅,还请大佬指点

代码:
  1. # 引用
  2. import pygame as pg
  3. from pygame.locals import *

  4. import random as r

  5. # 初始化
  6. pg.init()

  7. # 定义
  8. s_color = 0, 170, 250
  9. b_color = 0, 0, 0
  10. t_color = 255, 255, 255
  11. m_color = 60, 200, 160
  12. l_color = 100, 100, 100
  13. h_color = 10, 80, 200

  14. font = pg.font.SysFont('微软雅黑', 48)
  15. bg = pg.Surface((300 + 150, 600))
  16. bg.fill(b_color)
  17. tmp = font.render('A - Left', True, l_color)
  18. bg.blit(tmp, ((300 - tmp.get_rect().width) // 2, 150 + 50))
  19. tmp = font.render('D - Right', True, l_color)
  20. bg.blit(tmp, ((300 - tmp.get_rect().width) // 2, 200 + 50))
  21. tmp = font.render('W - Turn', True, l_color)
  22. bg.blit(tmp, ((300 - tmp.get_rect().width) // 2, 250 + 50))
  23. tmp = font.render('S - Quicken', True, l_color)
  24. bg.blit(tmp, ((300 - tmp.get_rect().width) // 2, 300 + 50))
  25. pg.draw.rect(bg, h_color, (300, 0, 150, 600))
  26. tmp = font.render('NEXT:', True, t_color)
  27. bg.blit(tmp, (300 + (150 - tmp.get_rect().width) // 2, 20))
  28. tmp = font.render('SCORE:', True, t_color)
  29. bg.blit(tmp, (300 + (150 - tmp.get_rect().width) // 2, 50 + 15 + 150))
  30. tmp = font.render('Coded', True, t_color)
  31. bg.blit(tmp, (300 + (150 - tmp.get_rect().width) // 2, 350 + 40))
  32. tmp = font.render('By:', True, t_color)
  33. bg.blit(tmp, (300 + (150 - tmp.get_rect().width) // 2, 390 + 40))
  34. tmp = font.render('SYZ', True, t_color)
  35. bg.blit(tmp, (300 + (150 - tmp.get_rect().width) // 2, 430 + 40))
  36. tmp = font.render('With:', True, t_color)
  37. bg.blit(tmp, (300 + (150 - tmp.get_rect().width) // 2, 470 + 40))
  38. tmp = font.render('Python', True, t_color)
  39. bg.blit(tmp, (300 + (150 - tmp.get_rect().width) // 2, 510 + 40))

  40. class Single:
  41.     def __init__(self, x, y):
  42.         self.x, self.y = x, y
  43.         
  44.     def blit(self, screen, color = s_color):
  45.         pg.draw.rect(screen, color, (self.x * 30 + 1, \
  46.                                        self.y * 30 + 1, 28, 28))

  47. '''
  48. ##
  49. ## - 1

  50. #
  51. ### - 2

  52. ##
  53. #
  54. # -3

  55. ###
  56.   # - 4

  57. #
  58. #
  59. ## - 5

  60.   #
  61. ### - 6

  62. #
  63. #
  64. ## - 7

  65. ###
  66. # - 8

  67. ##
  68. #
  69. # - 9

  70. #
  71. ### - 10

  72. #
  73. ##
  74. # - 11

  75. ###
  76. # - 12

  77. #
  78. ##
  79. # - 13

  80. #### - 14

  81. #
  82. #
  83. #
  84. # - 15

  85. ##
  86. ## - 16

  87. #
  88. ##
  89. # - 17

  90. ##
  91. ## - 18

  92. #
  93. ##
  94. # - 19

  95. # #
  96. #
  97. # # - 20

  98. # - 21
  99. '''
  100. class S:
  101.     def move(self, d = 2):
  102.         if d == 2:
  103.             self.y += 1
  104.             self.for_unmove = 8
  105.         elif d == 8:
  106.             self.y -= 1
  107.             self.for_unmove = 2
  108.         elif d == 4:
  109.             self.x -= 1
  110.             self.for_unmove = 6
  111.         elif d == 6:
  112.             self.x += 1
  113.             self.for_unmove = 4
  114.     def unmove(self):
  115.         self.move(self.for_unmove)
  116.     def blit(self, screen, color = m_color):
  117.         for each in self.get_group():
  118.             each.blit(screen, color)
  119.     def get_next(self):
  120.         tmp = eval('S' + str(self._next))()
  121.         tmp.x, tmp.y = self.x, self.y
  122.         return tmp
  123. class S1(S):
  124.     def __init__(self):
  125.         self.x, self.y = 4, -2
  126.         self._next = 1
  127.     def get_group(self):
  128.         g = set()
  129.         for j in range(2):
  130.             for k in range(2):
  131.                 g.add(Single(self.x + j, self.y + k))
  132.         return g
  133. class S2(S):
  134.     def __init__(self):
  135.         self.x, self.y = 4, -2
  136.         self._next = 3
  137.     def get_group(self):
  138.         g = set()
  139.         tmp = (0, 0), (0, 1), (1, 1), (2, 1)
  140.         for j, k in tmp:
  141.             g.add(Single(self.x + j, self.y + k))
  142.         return g
  143. class S3(S):
  144.     def __init__(self):
  145.         self.x, self.y = 4, -3
  146.         self._next = 4
  147.     def get_group(self):
  148.         g = set()
  149.         tmp = (0, 0), (1, 0), (0, 1), (0, 2)
  150.         for j, k in tmp:
  151.             g.add(Single(self.x + j, self.y + k))
  152.         return g
  153. class S4(S):
  154.     def __init__(self):
  155.         self.x, self.y = 4, -2
  156.         self._next = 5
  157.     def get_group(self):
  158.         g = set()
  159.         tmp = (0, 0), (1, 0), (2, 0), (2, 1)
  160.         for j, k in tmp:
  161.             g.add(Single(self.x + j, self.y + k))
  162.         return g
  163. class S5(S):
  164.     def __init__(self):
  165.         self.x, self.y = 4, -3
  166.         self._next = 2
  167.     def get_group(self):
  168.         g = set()
  169.         tmp = (1, 0), (1, 1), (0, 2), (1, 2)
  170.         for j, k in tmp:
  171.             g.add(Single(self.x + j, self.y + k))
  172.         return g
  173. class S6(S):
  174.     def __init__(self):
  175.         self.x, self.y = 4, -2
  176.         self._next = 7
  177.     def get_group(self):
  178.         g = set()
  179.         tmp = (2, 0), (0, 1), (1, 1), (2, 1)
  180.         for j, k in tmp:
  181.             g.add(Single(self.x + j, self.y + k))
  182.         return g
  183. class S7(S):
  184.     def __init__(self):
  185.         self.x, self.y = 4, -3
  186.         self._next = 8
  187.     def get_group(self):
  188.         g = set()
  189.         tmp = (0, 0), (0, 1), (0, 2), (1, 2)
  190.         for j, k in tmp:
  191.             g.add(Single(self.x + j, self.y + k))
  192.         return g
  193. class S8(S):
  194.     def __init__(self):
  195.         self.x, self.y = 4, -2
  196.         self._next = 9
  197.     def get_group(self):
  198.         g = set()
  199.         tmp = (0, 0), (1, 0), (2, 0), (0, 1)
  200.         for j, k in tmp:
  201.             g.add(Single(self.x + j, self.y + k))
  202.         return g
  203. class S9(S):
  204.     def __init__(self):
  205.         self.x, self.y = 4, -3
  206.         self._next = 6
  207.     def get_group(self):
  208.         g = set()
  209.         tmp = (0, 0), (1, 0), (1, 1), (1, 2)
  210.         for j, k in tmp:
  211.             g.add(Single(self.x + j, self.y + k))
  212.         return g
  213. class S10(S):
  214.     def __init__(self):
  215.         self.x, self.y = 4, -2
  216.         self._next = 11
  217.     def get_group(self):
  218.         g = set()
  219.         tmp = (1, 0), (0, 1), (1, 1), (2, 1)
  220.         for j, k in tmp:
  221.             g.add(Single(self.x + j, self.y + k))
  222.         return g
  223. class S11(S):
  224.     def __init__(self):
  225.         self.x, self.y = 4, -3
  226.         self._next = 12
  227.     def get_group(self):
  228.         g = set()
  229.         tmp = (0, 0), (0, 1), (1, 1), (0, 2)
  230.         for j, k in tmp:
  231.             g.add(Single(self.x + j, self.y + k))
  232.         return g
  233. class S12(S):
  234.     def __init__(self):
  235.         self.x, self.y = 4, -2
  236.         self._next = 13
  237.     def get_group(self):
  238.         g = set()
  239.         tmp = (0, 0), (1, 0), (2, 0), (1, 1)
  240.         for j, k in tmp:
  241.             g.add(Single(self.x + j, self.y + k))
  242.         return g
  243. class S13(S):
  244.     def __init__(self):
  245.         self.x, self.y = 4, -3
  246.         self._next = 10
  247.     def get_group(self):
  248.         g = set()
  249.         tmp = (1, 0), (0, 1), (1, 1), (1, 2)
  250.         for j, k in tmp:
  251.             g.add(Single(self.x + j, self.y + k))
  252.         return g
  253. class S14(S):
  254.     def __init__(self):
  255.         self.x, self.y = 3, -1
  256.         self._next = 15
  257.     def get_group(self):
  258.         g = set()
  259.         tmp = (0, 0), (1, 0), (2, 0), (3, 0)
  260.         for j, k in tmp:
  261.             g.add(Single(self.x + j, self.y + k))
  262.         return g
  263. class S15(S):
  264.     def __init__(self):
  265.         self.x, self.y = 4, -4
  266.         self._next = 14
  267.     def get_group(self):
  268.         g = set()
  269.         tmp = (0, 0), (0, 1), (0, 2), (0, 3)
  270.         for j, k in tmp:
  271.             g.add(Single(self.x + j, self.y + k))
  272.         return g
  273. class S16(S):
  274.     def __init__(self):
  275.         self.x, self.y = 4, -2
  276.         self._next = 17
  277.     def get_group(self):
  278.         g = set()
  279.         tmp = (0, 0), (1, 0), (1, 1), (2, 1)
  280.         for j, k in tmp:
  281.             g.add(Single(self.x + j, self.y + k))
  282.         return g
  283. class S17(S):
  284.     def __init__(self):
  285.         self.x, self.y = 4, -3
  286.         self._next = 16
  287.     def get_group(self):
  288.         g = set()
  289.         tmp = (1, 0), (0, 1), (1, 1), (0, 2)
  290.         for j, k in tmp:
  291.             g.add(Single(self.x + j, self.y + k))
  292.         return g
  293. class S18(S):
  294.     def __init__(self):
  295.         self.x, self.y = 4, -2
  296.         self._next = 19
  297.     def get_group(self):
  298.         g = set()
  299.         tmp = (1, 0), (2, 0), (0, 1), (1, 1)
  300.         for j, k in tmp:
  301.             g.add(Single(self.x + j, self.y + k))
  302.         return g
  303. class S19(S):
  304.     def __init__(self):
  305.         self.x, self.y = 4, -3
  306.         self._next = 18
  307.     def get_group(self):
  308.         g = set()
  309.         tmp = (0, 0), (0, 1), (1, 1), (1, 2)
  310.         for j, k in tmp:
  311.             g.add(Single(self.x + j, self.y + k))
  312.         return g
  313. class S20(S):
  314.     def __init__(self):
  315.         self.x, self.y = 4, -3
  316.         self._next = 20
  317.     def get_group(self):
  318.         g = set()
  319.         tmp = (0, 0), (0, 2), (1, 1), (2, 0), (2, 2)
  320.         for j, k in tmp:
  321.             g.add(Single(self.x + j, self.y + k))
  322.         return g
  323. class S21(S):
  324.     def __init__(self):
  325.         self.x, self.y = 4, -1
  326.         self._next = 21
  327.     def get_group(self):
  328.         return {Single(self.x, self.y)}
  329. ls = S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, \
  330.      S11, S12, S13, S14, S15, S16, S17, S18, S19, S20, \
  331.      S21

  332. # 主函数
  333. def main():
  334.     # 定义
  335.     screen = pg.display.set_mode((450, 600))
  336.     pg.display.set_caption('俄罗斯方块')

  337.     s_ing = r.choice(ls)()
  338.     s_ready = r.choice(ls)
  339.     s_alr = set()
  340.     score = 0

  341.     key_state = False

  342.     i = 0
  343.     clock = pg.time.Clock()
  344.     running = True

  345.     def check_if_next():
  346.         for each in s_ing.get_group():
  347.             if each.y >= 20:
  348.                 return True
  349.             for each1 in s_alr:
  350.                 if each.x == each1.x and each.y == each1.y:
  351.                     return True
  352.         return False
  353.     def check_if_move():
  354.         for each in s_ing.get_group():
  355.             if each.x < 0 or each.x >= 10:
  356.                 return False
  357.         if check_if_next():
  358.             return False
  359.         return True
  360.     def check_if_full():
  361.         nonlocal s_alr
  362.         nonlocal score
  363.         delta_score = 100
  364.         tmp = {i:set() for i in range(-10, 20)}
  365.         for each in s_alr:
  366.             tmp[each.y].add(each)
  367.         for each in tmp:
  368.             if len(tmp[each]) == 10:
  369.                 s_alr -= tmp[each]
  370.                 score += delta_score
  371.                 delta_score += 50
  372.                 for each1 in tmp:
  373.                     if each1 < each:
  374.                         for each2 in tmp[each1]:
  375.                             each2.y += 1

  376.     # 循环
  377.     while running:
  378.         # 处理事件
  379.         for each in pg.event.get():
  380.             if each.type == QUIT:
  381.                 return False

  382.             if each.type == KEYDOWN:
  383.                 key_state = each.key, 0
  384.             if each.type == KEYUP:
  385.                 key_state = False

  386.         if key_state != False:
  387.             if not(key_state[1] % 30):
  388.                 if key_state[0] in (K_w, K_UP):
  389.                     s_ing = s_ing.get_next()
  390.                     for each in range(10):
  391.                         if check_if_move():
  392.                             break
  393.                         s_ing.move(4)
  394.                     else:
  395.                         for each in range(10):
  396.                             s_ing.move(6)
  397.                         for each in range(10):
  398.                             if check_if_move():
  399.                                 break
  400.                             s_ing.move(8)
  401.                         else:
  402.                             for each in range(10):
  403.                                 s_ing.move(2)
  404.                             s_ing = s_ing.get_next().get_next().get_next()
  405.             
  406.             if not(key_state[1] % 10):
  407.                 if key_state[0] in (K_a, K_LEFT):
  408.                     s_ing.move(4)
  409.                 elif key_state[0] in (K_d, K_RIGHT):
  410.                     s_ing.move(6)
  411.                 if not check_if_move():
  412.                     s_ing.unmove()

  413.             if not(key_state[1] % 5):
  414.                 if key_state[0] in (K_s, K_DOWN):
  415.                     s_ing.move()
  416.                     if check_if_next():
  417.                         s_ing.unmove()
  418.                         for each in s_ing.get_group():
  419.                             s_alr.add(each)
  420.                         s_ing = s_ready()
  421.                         s_ready =r.choice(ls)
  422.                         check_if_full()
  423.             
  424.             key_state = key_state[0], key_state[1] + 1

  425.         if not(i % 20):
  426.             s_ing.move()
  427.             if check_if_next():
  428.                 s_ing.unmove()
  429.                 for each in s_ing.get_group():
  430.                     s_alr.add(each)
  431.                 s_ing = s_ready()
  432.                 s_ready =r.choice(ls)
  433.                 check_if_full()

  434.         # 绘制
  435.         screen.blit(bg, (0, 0))
  436.         
  437.         for each in s_alr|{s_ing}:
  438.             each.blit(screen)
  439.         
  440.         for each in s_ready().get_group():
  441.             pg.draw.rect(screen, t_color, (each.x * 15 + 1 + 300, \
  442.                                            (each.y + \
  443.                     max(abs(each1.y) for each1 in s_ready().get_group()) + 1\
  444.                                             ) * 15 + 1 + 50, 13, 13))
  445.         tmp = font.render(str(score), True, t_color)
  446.         screen.blit(tmp, (300 + (150 - tmp.get_rect().width) // 2, 215 + 40))
  447.         
  448.         # 收尾工作
  449.         pg.display.flip()
  450.         clock.tick(60)
  451.         i += 1

  452.         for each in s_alr:
  453.             if each.y < 0:
  454.                 running = False
  455.                 break

  456.     # 游戏结束
  457.     screen.fill(b_color)
  458.     b_font = pg.font.SysFont('微软雅黑', 72)
  459.     tmp = b_font.render('G A M E   O V E R', True, t_color)
  460.     screen.blit(tmp, ((450 - tmp.get_rect().width) // 2, 130))
  461.     tmp = font.render('YOUR SCORE:', True, t_color)
  462.     screen.blit(tmp, ((450 - tmp.get_rect().width) // 2, 230))
  463.     tmp = b_font.render(str(score), True, t_color)
  464.     screen.blit(tmp, ((450 - tmp.get_rect().width) // 2, 280))
  465.     pg.draw.rect(screen, t_color, (40, 380, 160, 50))
  466.     pg.draw.rect(screen, t_color, (250, 380, 160, 50), 1)
  467.     tmp = font.render('AGAIN(A)', True, b_color)
  468.     screen.blit(tmp, ((160 - tmp.get_rect().width) // 2 + 40, \
  469.                       (50 - tmp.get_rect().height) // 2 + 380))
  470.     tmp = font.render('QUIT(Q)', True, t_color)
  471.     screen.blit(tmp, ((160 - tmp.get_rect().width) // 2 + 250, \
  472.                       (50 - tmp.get_rect().height) // 2 + 380))
  473.     tmp = font.render('Coded By SYZ With Python', True, t_color)
  474.     screen.blit(tmp, (450 - tmp.get_rect().width, 600 - tmp.get_rect().height))
  475.     pg.display.flip()

  476.     while True:
  477.         for each in pg.event.get():
  478.             if each.type == QUIT:
  479.                 return False
  480.             if each.type == MOUSEBUTTONUP:
  481.                 if each.pos[1] in range(380, 430):
  482.                     if each.pos[0] in range(40, 200):
  483.                         return True
  484.                     elif each.pos[0] in range(250, 410):
  485.                         return False
  486.             if each.type == KEYUP:
  487.                 if each.key == K_a:
  488.                     return True
  489.                 elif each.key == K_q:
  490.                     return False

  491. # 主线
  492. if __name__ == '__main__':
  493.     while main():
  494.         pass
  495.     pg.quit()
复制代码

游戏截图:
1.png
2.png
3.png

评分

参与人数 6荣誉 +26 鱼币 +21 贡献 +6 收起 理由
逃兵 + 5 + 5 鱼C有你更精彩^_^
昨非 + 5 + 5 + 3 鱼C有你更精彩^_^
weiter + 5 + 5
Daniel_Zhang + 5 + 3 鱼C有你更精彩^_^
小伤口 + 5 + 5 感谢楼主无私奉献!
yayc_zcyd + 1 + 1 鱼C有你更精彩^_^

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-20 08:20:41 | 显示全部楼层
除了pygame库和python解释器外,代码没有其他任何资源文件,欢迎复制以体验游戏
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

 楼主| 发表于 2021-3-20 08:23:04 | 显示全部楼层
本帖最后由 考不好不改名 于 2021-4-5 08:23 编辑

欢迎 收藏 评分 分享 顶 还有 回复
谢谢啦.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-20 08:38:52 | 显示全部楼层

回帖奖励 +5 鱼币

太利害了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-20 10:24:56 | 显示全部楼层
自顶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-20 12:05:38 | 显示全部楼层
大佬强啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-20 12:38:59 | 显示全部楼层
厉害
66666666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-20 14:23:20 | 显示全部楼层
niubility
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-20 20:36:19 | 显示全部楼层
66
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-20 21:07:30 | 显示全部楼层
谢谢支持,晚上自顶
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-21 08:48:06 | 显示全部楼层
那个,楼主的作品很强啊
另外说一句,鱼C论坛的用户名是一次定终身,不能更改的,似乎还不存在注销的操作
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-21 09:25:32 | 显示全部楼层
weiter 发表于 2021-3-21 08:48
那个,楼主的作品很强啊
另外说一句,鱼C论坛的用户名是一次定终身,不能更改的,似乎还不存在注销的操作{ ...

啊这,,,所以我永远考不好咯
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-21 13:41:55 | 显示全部楼层
考不好不改名 发表于 2021-3-21 09:25
啊这,,,所以我永远考不好咯

考得好是肯定考的好的,只是可能改不了名了
所以会越考越好的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-22 10:00:41 | 显示全部楼层
666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-22 13:15:55 | 显示全部楼层
牛逼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-22 14:24:39 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-22 19:12:36 | 显示全部楼层
我是初学者,看的我心潮澎湃我需要学多久才能像楼主大佬这么厉害?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-23 09:17:58 | 显示全部楼层
考不好不改名 发表于 2021-3-20 08:23
欢迎 收藏 评分 分享 顶 还有 回复

这写的太厉害了,那必须踩呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-23 21:02:22 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-24 21:40:39 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 02:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表