鱼C论坛

 找回密码
 立即注册
查看: 1686|回复: 1

[作品展示] 也用pygame做了俄罗斯方块,150行

[复制链接]
发表于 2020-6-18 00:33:37 | 显示全部楼层 |阅读模式

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

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

x
只完成了最简单的功能,
由于存在转动数据,这个实在不好意思全部手动输入(实际代码行数超过了手动输入),强行转动(最后一个图形实在无奈,只能手动调整,转动取整后,图形位置移动了,只有这一个,耍赖了一把)

代码:

  1. import sys
  2. import pygame
  3. from pygame.locals import *
  4. import random

  5. class Block:
  6.     blk_color = [(255, 255, 255),(255, 255, 0),(255, 0, 255),(0, 255, 255),(255, 0, 0),(0, 255, 0),(0, 0, 255),(32,32,32)]
  7.     BLANK = 7
  8.     type_coord=[[[-1,0],[0,0],[1,0],[2,0]]\
  9.         ,[[-1,0],[0,0],[1,0],[0,1]]\
  10.         ,[[-1,0],[0,0],[-1,1],[0,1]]\
  11.         ,[[-1,0],[0,0],[0,1],[1,1]]\
  12.         ,[[0,0],[1,0],[-1,1],[0,1]]\
  13.         ,[[-1,0],[0,0],[1,0],[1,1]]\
  14.         ,[[-1,0],[0,0],[1,0],[-1,1]]]
  15.     type_rotate = []
  16.    
  17.     def __init__(self,x,y,blk,angle):
  18.         self.x = x
  19.         self.y = y
  20.         self.blk = blk
  21.         self.angle = angle
  22.         
  23.     @staticmethod
  24.     def rotate(no):
  25.         rt_all = []
  26.         rt = Block.type_coord[no][:]
  27.         cx,cy=0,0
  28.         for b in range(4):
  29.             rt[b][0],rt[b][1] = rt[b][0]*4,rt[b][1]*4
  30.             cx += rt[b][0]
  31.             cy += rt[b][1]
  32.         cx = (cx)//8*2 if no !=6 else (cx+4)//8*2
  33.         cy = (cy)//8*2 if no !=6 else (cy-4)//8*2
  34.         rt_all.append(rt)
  35.         for r in range(3):
  36.             rt_new = []
  37.             for b in range(4):
  38.                 rt_new.append([cx + (cy-rt[b][1]),cy-(cx-rt[b][0])])
  39.             rt_all.append(rt_new)
  40.             rt = rt_new
  41.         for r in range(4):
  42.             for b in range(4):
  43.                 rt_all[r][b][0] //= 4
  44.                 rt_all[r][b][1] //= 4
  45.         return rt_all
  46.     @staticmethod
  47.     def init_rotate():
  48.         for r in range(7):
  49.             Block.type_rotate.append(Block.rotate(r))

  50. class TRS:
  51.     screen = None
  52.     map = [[Block.BLANK]*10 for i in range(20)]
  53.     STATUS = 0
  54.     cbk = None

  55.     def __init__(self,screen):
  56.         TRS.screen = screen

  57.     @staticmethod
  58.     def action(key_pressed):
  59.         if(key_pressed[K_LEFT] and TRS.check_action(TRS.cbk.x-1,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle)):
  60.             TRS.cbk.x -= 1
  61.         elif (key_pressed[K_RIGHT] and TRS.check_action(TRS.cbk.x+1,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle)):
  62.             TRS.cbk.x += 1
  63.         elif (key_pressed[K_UP] and TRS.check_action(TRS.cbk.x,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle+1)):
  64.             TRS.cbk.angle += 1
  65.         elif (key_pressed[K_DOWN] and TRS.check_action(TRS.cbk.x,TRS.cbk.y+1,TRS.cbk.blk,TRS.cbk.angle)):
  66.             TRS.cbk.y += 1
  67.             
  68.     @staticmethod
  69.     def new_blk():
  70.         TRS.cbk = Block(5,0,random.randint(0,6),0)
  71.     @staticmethod
  72.     def check_action(x,y,blk,angle):
  73.         tr = Block.type_rotate[blk][angle%4]
  74.         for b in range(4):
  75.             bx,by = x + tr[b][0],y + tr[b][1]
  76.             if(bx<0 or bx>9 or by <0 or by>19 or TRS.map[by][bx]!=Block.BLANK):
  77.                 return False
  78.         return True
  79.     @staticmethod
  80.     def check_drop():
  81.         if TRS.check_action(TRS.cbk.x,TRS.cbk.y+1,TRS.cbk.blk,TRS.cbk.angle):
  82.             TRS.cbk.y += 1
  83.         else:
  84.             TRS.STATUS = 2
  85.             
  86.     @staticmethod
  87.     def check_clear():
  88.         blk = Block.type_rotate[TRS.cbk.blk][TRS.cbk.angle%4]
  89.         row = list({TRS.cbk.y + blk[i][1] for i in range(4)})
  90.         row.sort()
  91.         row.reverse()
  92.         for b in range(4):
  93.             TRS.map[TRS.cbk.y + blk[b][1]][TRS.cbk.x + blk[b][0]] = TRS.cbk.blk
  94.         del_rows = 0
  95.         for r in row:
  96.             if not (Block.BLANK in TRS.map[r]):
  97.                 TRS.map.pop(r)
  98.                 del_rows += 1
  99.         for d in range(del_rows):
  100.             TRS.map.insert(0,[Block.BLANK for i in range(10)])
  101.             
  102.     @staticmethod
  103.     def print_game():
  104.         TRS.screen.fill((0, 0, 0))
  105.         for row in range(20):
  106.             for col in range(10):
  107.                 pygame.draw.rect(TRS.screen, Block.blk_color[TRS.map[row][col]], ((col*21,row*21), (20, 20)), 0)
  108.         blk = Block.type_rotate[TRS.cbk.blk][TRS.cbk.angle%4]
  109.         for b in range(4):
  110.             pygame.draw.rect(TRS.screen, Block.blk_color[TRS.cbk.blk], (((TRS.cbk.x+blk[b][0])*21,(TRS.cbk.y+blk[b][1])*21), (20, 20)), 0)
  111. class App:
  112.     def __init__(self):
  113.         pygame.init()
  114.         screen = pygame.display.set_mode((300,430))
  115.         Block.init_rotate()
  116.         TRS(screen)
  117.         
  118.     #@staticmethod
  119.     def main(self):
  120.         clock = pygame.time.Clock()   # 创建游戏时钟
  121.         count = 1
  122.         # 进入游戏循环
  123.         while True:
  124.             # 设置刷新帧率
  125.             clock.tick(15)
  126.          
  127.             # 事件检测
  128.             for event in pygame.event.get():
  129.                 if event.type == pygame.QUIT:   # 退出事件
  130.                     sys.exit()
  131.                     
  132.             if TRS.STATUS == 0:
  133.                 TRS.new_blk()
  134.                 if TRS.check_action(TRS.cbk.x,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle):
  135.                     TRS.STATUS = 1
  136.                 else:
  137.                     TRS.STATUS = 3
  138.                     print("GAME OVER")
  139.             elif TRS.STATUS == 1:
  140.                 TRS.action(pygame.key.get_pressed())
  141.                 if count % 10 == 0:
  142.                     TRS.check_drop()
  143.             elif TRS.STATUS == 2:
  144.                 TRS.check_clear()
  145.                 TRS.STATUS = 0

  146.             TRS.print_game()
  147.             pygame.display.update()   #刷新屏幕
  148.             count += 1

  149. App().main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-6-18 00:35:14 | 显示全部楼层
一打开高级功能,图形,附件全弄不上去了,只能回帖:
tetris.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 00:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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