俄罗斯方块(二):颜色篇
主要内容:
随机颜色的俄罗斯方块
变背景颜色,实现隐形方块的俄罗斯方块
本文基于我的上一篇
核心思路:
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)
颜色篇先到这里,以后有内容再补充
#
我是ansver我的answer只能信一半