俄罗斯方块(二):颜色篇
2018-07-06 01:19:25来源:博客园 阅读 ()
主要内容:
随机颜色的俄罗斯方块
变背景颜色,实现隐形方块的俄罗斯方块
本文基于我的上一篇
俄罗斯方块(一):简版
核心思路:
1,图像由“核心变量”完全控制,图像变化的本质是 变量的改变
2,自上而下式的思考,图像变化的问题将一步步转为 一系列具体的变量修改
3,“核心变量”在思考过程中并非不可变更,为了写函数方便,可以适当让步
一、随机颜色
无疑,我们需要 新变量 记录颜色
下一步 颜色信息的 产生 使用 销毁
一个方块”落地”后,产生的下一个方块形状的同时随机产生一个颜色
active.clear() active.extend(list(random.choice(all_block))) centre.clear() centre.extend([20, 4]) a_color.clear() a_color.extend([random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)])
画面绘制时 就不再用 blue
x, y = centre for i, j in active: i += x j += y pygame.draw.rect(screen, a_color, (j * 25 + 1, 500 - i * 25 + 1, 23, 23))
方块落地后,信息转入background ,background 并不介意0,1之外的 数据
ps:是上一篇的代码中 判断一个位置是否被占 只要求 布尔值为True
不信你看↓↓↓↓
def move_LR(n): """n=-1代表向左,n=1代表向右""" x, y = centre y += n for i, j in active: i += x j += y if j < 0 or j > 9 or background[i][j]: break else: centre.clear() centre.extend([x, y])
判断是否满行时 也仅仅 0 not in xx 而已
所以颜色信息可以直接写入 background
# active --转入-> background x, y = centre for i, j in active: # background[x + i][y + j] = 1 background[x + i][y + j] = tuple(a_color)
ps :a_color 是 list 不能直接赋值(赋值 != 浅拷贝)
画面绘制部分 也要改
for i in range(1, 21): for j in range(10): bolck = background[i][j] if bolck: # pygame.draw.rect(screen, blue, (j * 25 + 1, 500 - i * 25 + 1, 23, 23)) pygame.draw.rect(screen, bolck, (j * 25 + 1, 500 - i * 25 + 1, 23, 23))
销毁就不用担心了 和位置信息的那部分相同
成品
完整的代码
# 随机颜色 import pygame, sys, random, time def new_draw(): screen.fill(white) for i in range(1, 21): for j in range(10): bolck = background[i][j] if bolck: # pygame.draw.rect(screen, blue, (j * 25 + 1, 500 - i * 25 + 1, 23, 23)) pygame.draw.rect(screen, bolck, (j * 25 + 1, 500 - i * 25 + 1, 23, 23)) x, y = centre for i, j in active: i += x j += y pygame.draw.rect(screen, a_color, (j * 25 + 1, 500 - i * 25 + 1, 23, 23)) pygame.display.update() def move_LR(n): """n=-1代表向左,n=1代表向右""" x, y = centre y += n for i, j in active: i += x j += y if j < 0 or j > 9 or background[i][j]: break else: centre.clear() centre.extend([x, y]) def rotate(): x, y = centre l = [(-j, i) for i, j in active] for i, j in l: i += x j += y if j < 0 or j > 9 or background[i][j]: break else: active.clear() active.extend(l) def move_down(): x, y = centre x -= 1 for i, j in active: i += x j += y if background[i][j]: break else: centre.clear() centre.extend([x, y]) return # active --转入-> background x, y = centre for i, j in active: # background[x + i][y + j] = 1 background[x + i][y + j] = tuple(a_color) l = [] for i in range(1, 20): if 0 not in background[i]: l.append(i) l.sort(reverse=True) for i in l: background.pop(i) background.append([0 for j in range(10)]) score[0] += len(l) pygame.display.set_caption("分数:%d" % (score[0])) active.clear() active.extend(list(random.choice(all_block))) centre.clear() centre.extend([20, 4]) a_color.clear() a_color.extend([random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)]) x, y = centre for i, j in active: i += x j += y if background[i][j]: break else: return alive.append(1) pygame.init() screen = pygame.display.set_mode((250, 500)) pygame.display.set_caption("俄罗斯方块") fclock = pygame.time.Clock() all_block = (((0, 0), (0, -1), (0, 1), (0, 2)), ((0, 0), (0, 1), (-1, 0), (-1, 1)), ((0, 0), (0, -1), (-1, 0), (-1, 1)), ((0, 0), (0, 1), (-1, -1), (-1, 0)), ((0, 0), (0, 1), (1, 0), (0, -1)), ((0, 0), (1, 0), (-1, 0), (1, -1)), ((0, 0), (1, 0), (-1, 0), (1, 1))) background = [[0 for i in range(10)] for j in range(24)] background[0] = [1 for i in range(10)] active = list(random.choice(all_block)) centre = [20, 4] score = [0] a_color = [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)] black = 0, 0, 0 white = 255, 255, 255 blue = 0, 0, 255 times = 0 alive = [] press = False while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: move_LR(-1) elif event.key == pygame.K_RIGHT: move_LR(1) elif event.key == pygame.K_UP: rotate() elif event.key == pygame.K_DOWN: press = True elif event.type == pygame.KEYUP: if event.key == pygame.K_DOWN: press = False if press: times += 10 if times >= 50: move_down() times = 0 else: times += 1 if alive: pygame.display.set_caption("over分数:%d" % (score[0])) time.sleep(3) break new_draw() fclock.tick(100)
看了成品图,发现有些颜色很浅,那也在常理之中
那如果随机出来255,255,255 白色或者254,254,254之类 人眼无法分辨的颜色那岂不是.......美滋滋?!.....
二、变背景颜色,实现隐形方块的俄罗斯方块
背景颜色两个,黑和白
活动方块颜色两个,黑和白
两个颜色一黑一白,交替变化
成品图假想图
act = 0 bg = 1 black = 0, 0, 0 white = 255, 255, 255 blue = 0, 0, 255 color = [black, white]
变量color用来保存颜色信息color[0] / color[ act ] 是活动方块的颜色 color[1] / color[ bg ] 是背景颜色
# 双色版 import pygame, sys, random, time def new_draw(): screen.fill(color[bg]) for i in range(1, 21): for j in range(10): bolck = background[i][j] if bolck: pygame.draw.rect(screen, bolck, (j * 25 + 1, 500 - i * 25 + 1, 23, 23)) x, y = centre for i, j in active: i += x j += y pygame.draw.rect(screen, color[act], (j * 25 + 1, 500 - i * 25 + 1, 23, 23)) pygame.display.update() def move_LR(n): """n=-1代表向左,n=1代表向右""" x, y = centre y += n for i, j in active: i += x j += y if j < 0 or j > 9 or background[i][j]: break else: centre.clear() centre.extend([x, y]) def rotate(): x, y = centre l = [(-j, i) for i, j in active] for i, j in l: i += x j += y if j < 0 or j > 9 or background[i][j]: break else: active.clear() active.extend(l) def move_down(): x, y = centre x -= 1 for i, j in active: i += x j += y if background[i][j]: break else: centre.clear() centre.extend([x, y]) return x, y = centre for i, j in active: background[x + i][y + j] = color[act] l = [] for i in range(1, 20): if 0 not in background[i]: l.append(i) l.sort(reverse=True) for i in l: background.pop(i) background.append([0 for j in range(10)]) score[0] += len(l) pygame.display.set_caption("分数:%d" % (score[0])) active.clear() active.extend(list(random.choice(all_block))) centre.clear() centre.extend([20, 4]) # - 互换颜色 color.insert(0, color.pop()) # color[act], color[bg] = color[bg], color[act] x, y = centre for i, j in active: i += x j += y if background[i][j]: break else: return alive.append(1) pygame.init() screen = pygame.display.set_mode((250, 500)) pygame.display.set_caption("俄罗斯方块") fclock = pygame.time.Clock() all_block = (((0, 0), (0, -1), (0, 1), (0, 2)), ((0, 0), (0, 1), (-1, 0), (-1, 1)), ((0, 0), (0, -1), (-1, 0), (-1, 1)), ((0, 0), (0, 1), (-1, -1), (-1, 0)), ((0, 0), (0, 1), (1, 0), (0, -1)), ((0, 0), (1, 0), (-1, 0), (1, -1)), ((0, 0), (1, 0), (-1, 0), (1, 1))) background = [[0 for i in range(10)] for j in range(24)] background[0] = [1 for i in range(10)] active = list(random.choice(all_block)) centre = [20, 4] score = [0] act = 0 bg = 1 black = 0, 0, 0 white = 255, 255, 255 blue = 0, 0, 255 color = [black, white] times = 0 alive = [] press = False while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: move_LR(-1) elif event.key == pygame.K_RIGHT: move_LR(1) elif event.key == pygame.K_UP: rotate() elif event.key == pygame.K_DOWN: press = True elif event.type == pygame.KEYUP: if event.key == pygame.K_DOWN: press = False if press: times += 10 if times >= 50: move_down() times = 0 else: times += 1 if alive: pygame.display.set_caption("over分数:%d" % (score[0])) time.sleep(3) break new_draw() fclock.tick(100)
颜色篇先到这里,以后有内容再补充
#
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 控制台输出带颜色的文字 2019-05-23
- Python:游戏:300行代码实现俄罗斯方块 2019-01-04
- python中matplotlib的颜色及线条控制 2018-10-26
- OpenCV 的颜色空间转换 2018-10-19
- Print输出颜色字体方法 2018-08-21
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash