鱼C论坛

 找回密码
 立即注册
查看: 1646|回复: 2

求一个Python俄罗斯方块有偿

[复制链接]
发表于 2021-4-5 11:04:04 From FishC Mobile | 显示全部楼层 |阅读模式

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

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

x
求一个Python俄罗斯方块代码 有偿
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-4-5 11:16:37 | 显示全部楼层
本帖最后由 考不好不改名 于 2021-4-5 11:24 编辑

我的帖,看看?(记得顶一顶
俄罗斯方块
https://fishc.com.cn/thread-192607-1-1.html
(出处: 鱼C论坛)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-4-5 11:22:54 | 显示全部楼层
网上大把大把的代码
  1. import pygame, sys, random, time


  2. # 第二版
  3. def new_draw():
  4.     screen.fill(white)

  5.     for i in range(1, 21):
  6.         for j in range(10):
  7.             bolck = background[i][j]
  8.             if bolck:
  9.                 pygame.draw.rect(screen, blue, (j * 25 + 1, 500 - i * 25 + 1, 23, 23))

  10.     x, y = centre
  11.     for i, j in active:
  12.         i += x
  13.         j += y
  14.         pygame.draw.rect(screen, blue, (j * 25 + 1, 500 - i * 25 + 1, 23, 23))

  15.     pygame.display.update()


  16. def move_LR(n):
  17.     """n=-1代表向左,n=1代表向右"""
  18.     x, y = centre
  19.     y += n
  20.     for i, j in active:
  21.         i += x
  22.         j += y
  23.         if j < 0 or j > 9 or background[i][j]:
  24.             break
  25.     else:
  26.         centre.clear()
  27.         centre.extend([x, y])


  28. def rotate():
  29.     x, y = centre
  30.     l = [(-j, i) for i, j in active]
  31.     for i, j in l:
  32.         i += x
  33.         j += y
  34.         if j < 0 or j > 9 or background[i][j]:
  35.             break
  36.     else:
  37.         active.clear()
  38.         active.extend(l)


  39. def move_down():
  40.     x, y = centre
  41.     x -= 1
  42.     for i, j in active:
  43.         i += x
  44.         j += y
  45.         if background[i][j]:
  46.             break
  47.     else:
  48.         centre.clear()
  49.         centre.extend([x, y])
  50.         return
  51.     # 如果新位置未被占用 通过return结束
  52.     # 如果新位置被占用则继续向下执行
  53.     x, y = centre
  54.     for i, j in active:
  55.         background[x + i][y + j] = 1

  56.     l = []
  57.     for i in range(1, 20):
  58.         if 0 not in background[i]:
  59.             l.append(i)
  60.     # l装 行号,鉴于删去后,部分索引变化,对其降序排列,倒着删除
  61.     l.sort(reverse=True)

  62.     for i in l:
  63.         background.pop(i)
  64.         background.append([0 for j in range(10)])
  65.         # 随删随补

  66.     score[0] += len(l)
  67.     pygame.display.set_caption("分数:%d" % (score[0]))

  68.     active.clear()
  69.     active.extend(list(random.choice(all_block)))
  70.     # all_block保存7种形状的信息,手打出来的
  71.     centre.clear()
  72.     centre.extend([20, 4])

  73.     x, y = centre
  74.     for i, j in active:
  75.         i += x
  76.         j += y
  77.         if background[i][j]:
  78.             break
  79.     else:
  80.         return
  81.     alive.append(1)


  82. pygame.init()
  83. screen = pygame.display.set_mode((250, 500))
  84. pygame.display.set_caption("俄罗斯方块")
  85. fclock = pygame.time.Clock()

  86. all_block = (((0, 0), (0, -1), (0, 1), (0, 2)),
  87.              ((0, 0), (0, 1), (-1, 0), (-1, 1)),
  88.              ((0, 0), (0, -1), (-1, 0), (-1, 1)),
  89.              ((0, 0), (0, 1), (-1, -1), (-1, 0)),
  90.              ((0, 0), (0, 1), (1, 0), (0, -1)),
  91.              ((0, 0), (1, 0), (-1, 0), (1, -1)),
  92.              ((0, 0), (1, 0), (-1, 0), (1, 1)))
  93. background = [[0 for i in range(10)] for j in range(24)]
  94. background[0] = [1 for i in range(10)]
  95. active = list(random.choice(all_block))
  96. centre = [20, 4]
  97. score = [0]


  98. black = 0, 0, 0
  99. white = 255, 255, 255
  100. blue = 0, 0, 255

  101. times = 0
  102. alive = []
  103. press = False
  104. while True:
  105.     for event in pygame.event.get():
  106.         if event.type == pygame.QUIT:
  107.             sys.exit()
  108.         elif event.type == pygame.KEYDOWN:
  109.             if event.key == pygame.K_LEFT:
  110.                 move_LR(-1)
  111.             elif event.key == pygame.K_RIGHT:
  112.                 move_LR(1)
  113.             elif event.key == pygame.K_UP:
  114.                 rotate()
  115.             elif event.key == pygame.K_DOWN:
  116.                 press = True
  117.         elif event.type == pygame.KEYUP:
  118.             if event.key == pygame.K_DOWN:
  119.                 press = False
  120.     if press:
  121.         times += 10

  122.     if times >= 50:
  123.         move_down()
  124.         times = 0
  125.     else:
  126.         times += 1

  127.     if alive:
  128.         pygame.display.set_caption("over分数:%d" % (score[0]))
  129.         time.sleep(3)
  130.         break
  131.     new_draw()
  132.     fclock.tick(100)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-14 04:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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