|
| 1 | +# 15 2048 |
| 2 | + |
| 3 | +## Requirements |
| 4 | + |
| 5 | +1. Run the code in console using command line. |
| 6 | +2. It'll open a Python window to play the 2048 game. |
| 7 | + |
| 8 | +## What can we practice in this project? |
| 9 | + |
| 10 | +- Turtle |
| 11 | +- 2D List |
| 12 | + |
| 13 | +## A reference code |
| 14 | + |
| 15 | +```python |
| 16 | +from turtle import * |
| 17 | +from random import randint |
| 18 | + |
| 19 | +title("2048") |
| 20 | +gz = 176 # shapesize=8是连长160的正方形,我们可以将格子设置为162,这边就可以画出边框线 |
| 21 | +N = 4 |
| 22 | +bc = gz * N # 边长 |
| 23 | +tracer(False) # 不显示绘制动画 |
| 24 | +bgcolor("gray") |
| 25 | +ht() |
| 26 | +up() |
| 27 | +speed(0) |
| 28 | +shape("square") |
| 29 | +shapesize(8) # shapesize为8是一个连长为160的正方形 |
| 30 | + |
| 31 | +# 二维列表对应游戏表格 |
| 32 | +grid = [ |
| 33 | + [0, 0, 0, 0], |
| 34 | + [0, 0, 0, 0], |
| 35 | + [0, 0, 0, 0], |
| 36 | + [0, 0, 0, 0] |
| 37 | +] |
| 38 | + |
| 39 | +maxx = 2 |
| 40 | +steps = 0 |
| 41 | +tj = Turtle() |
| 42 | +tj.speed(0) |
| 43 | +tj.up() |
| 44 | +tj.ht() |
| 45 | +tj.color("white") |
| 46 | +tj.goto(-bc / 2, bc / 2 + 20) |
| 47 | +tj.write("步数:{},最大值:{}".format(steps, maxx), font=("", 20, "")) |
| 48 | + |
| 49 | +# 颜色字典 |
| 50 | +COLORS = { |
| 51 | + 0: "white", |
| 52 | + 2: "yellow", |
| 53 | + 4: "orange", |
| 54 | + 8: "pink", |
| 55 | + 16: "red", |
| 56 | + 32: "lightblue", |
| 57 | + 64: "lightgreen", |
| 58 | + 128: "green", |
| 59 | + 256: "purple", |
| 60 | + 512: "cyan", |
| 61 | + 1024: "silver", |
| 62 | + 2048: "gold" |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +# 绘制二维列表 |
| 67 | +def draw_grid(): |
| 68 | + global steps |
| 69 | + global maxx |
| 70 | + tj.clear() |
| 71 | + tj.write("步数:{},最大值:{}".format(steps, maxx), font=("", 20, "")) |
| 72 | + steps += 1 |
| 73 | + clear() |
| 74 | + for row in range(N): |
| 75 | + for col in range(N): |
| 76 | + # 从左上角(-bc/2 + gz/2, bc/2 - gz/2)开始从上往下、从左往右绘制 |
| 77 | + goto(-bc / 2 + gz / 2 + col * gz, bc / 2 - gz / 2 - row * gz) |
| 78 | + # 根据grid[row][col]的数字,来设置对应的颜色 |
| 79 | + color(COLORS[grid[row][col]]) |
| 80 | + stamp() |
| 81 | + # 在单元格中写下对应的数字 |
| 82 | + sety(bc / 2 - gz / 2 - row * gz - 30) |
| 83 | + color("black") |
| 84 | + if grid[row][col] > 0: |
| 85 | + write(grid[row][col], font=("", 50, ""), align="center") |
| 86 | + # 找最大值 |
| 87 | + if grid[row][col] > maxx: |
| 88 | + maxx = grid[row][col] |
| 89 | + # 刷新游戏画面 |
| 90 | + update() |
| 91 | + if maxx == 2048: |
| 92 | + goto(0, 0) |
| 93 | + color("red") |
| 94 | + write("游戏胜利", font=("", 100, ""), align="center") |
| 95 | + |
| 96 | + |
| 97 | +def can_add(): |
| 98 | + for i in range(N): |
| 99 | + for j in range(N): |
| 100 | + if grid[i][j] == 0: |
| 101 | + return True |
| 102 | + return False |
| 103 | + |
| 104 | + |
| 105 | +def generate_random(): |
| 106 | + # 如果能加 |
| 107 | + if can_add(): |
| 108 | + added = False |
| 109 | + # 随机选择一个空白位置,将这个位置数字设置为2 |
| 110 | + while not added: |
| 111 | + i = randint(0, N - 1) |
| 112 | + j = randint(0, N - 1) |
| 113 | + if grid[i][j] == 0: |
| 114 | + grid[i][j] = 2 |
| 115 | + added = True |
| 116 | + |
| 117 | + |
| 118 | +generate_random() |
| 119 | +draw_grid() |
| 120 | + |
| 121 | + |
| 122 | +def up(): |
| 123 | + # 每列 |
| 124 | + for col in range(N): |
| 125 | + # 从第2行到最后一行 |
| 126 | + for row in range(1, N): |
| 127 | + # 记下这个格子的数字 |
| 128 | + value = grid[row][col] |
| 129 | + r = row |
| 130 | + # 找到最上面为0的格子r |
| 131 | + while r > 0 and grid[r - 1][col] == 0: |
| 132 | + r = r - 1 |
| 133 | + # 如果r-1存在并且其中的数字和row数字一样,则将r变为r-1 |
| 134 | + if r - 1 >= 0 and grid[r - 1][col] == value: |
| 135 | + r = r - 1 |
| 136 | + # 如果r不等于原来的row,2者合并(此时r位置数字为0或者和row相等) |
| 137 | + if r != row: |
| 138 | + grid[r][col] += value |
| 139 | + grid[row][col] = 0 # 原来row的数字要变为0 |
| 140 | + |
| 141 | + # 随机生成2 |
| 142 | + generate_random() |
| 143 | + # 重新绘制格子 |
| 144 | + draw_grid() |
| 145 | + |
| 146 | + |
| 147 | +def down(): |
| 148 | + for col in range(N): |
| 149 | + for row in range(N - 2, -1, -1): |
| 150 | + value = grid[row][col] |
| 151 | + r = row |
| 152 | + while r < N - 1 and grid[r + 1][col] == 0: |
| 153 | + r = r + 1 |
| 154 | + if r + 1 < N and grid[r + 1][col] == value: |
| 155 | + r = r + 1 |
| 156 | + if r != row: |
| 157 | + grid[r][col] += value |
| 158 | + grid[row][col] = 0 |
| 159 | + generate_random() |
| 160 | + draw_grid() |
| 161 | + |
| 162 | + |
| 163 | +def left(): |
| 164 | + for row in range(N): |
| 165 | + for col in range(1, N): |
| 166 | + value = grid[row][col] |
| 167 | + c = col |
| 168 | + while c > 0 and grid[row][c - 1] == 0: |
| 169 | + c = c - 1 |
| 170 | + if c - 1 >= 0 and grid[row][c - 1] == value: |
| 171 | + c = c - 1 |
| 172 | + if c != col: |
| 173 | + grid[row][c] += value |
| 174 | + grid[row][col] = 0 |
| 175 | + generate_random() |
| 176 | + draw_grid() |
| 177 | + |
| 178 | + |
| 179 | +def right(): |
| 180 | + for row in range(N): |
| 181 | + for col in range(N - 2, -1, -1): |
| 182 | + value = grid[row][col] |
| 183 | + c = col |
| 184 | + while c < N - 1 and grid[row][c + 1] == 0: |
| 185 | + c = c + 1 |
| 186 | + if c + 1 < N and grid[row][c + 1] == value: |
| 187 | + c = c + 1 |
| 188 | + if c != col: |
| 189 | + grid[row][c] += value |
| 190 | + grid[row][col] = 0 |
| 191 | + generate_random() |
| 192 | + draw_grid() |
| 193 | + |
| 194 | + |
| 195 | +onkeypress(up, "Up") # 按向上键("Up"),执行up响应函数 |
| 196 | +onkeypress(down, "Down") # 按向下键("Down"),执行down响应函数 |
| 197 | +onkeypress(left, "Left") # 按向上键("Left"),执行left响应函数 |
| 198 | +onkeypress(right, "Right") # 按向上键("Right"),执行right响应函数 |
| 199 | +listen() # 开始监听 |
| 200 | + |
| 201 | +DONE() |
| 202 | + |
| 203 | +``` |
| 204 | + |
| 205 | +## Run the demo |
| 206 | + |
| 207 | +Please save the Python as game.py and run it in console: |
| 208 | + |
| 209 | +``` |
| 210 | +python game.py |
| 211 | +``` |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | +---- |
| 216 | + |
| 217 | +# 2048 |
| 218 | + |
| 219 | +## 项目需求 |
| 220 | + |
| 221 | +- 直接在控制台使用命令行运行 |
| 222 | +- 运行之后出现2048小游戏 |
| 223 | + |
| 224 | +## 项目练习 |
| 225 | + |
| 226 | +- Turtle |
| 227 | +- Class/Object |
| 228 | + |
| 229 | +## 项目参考代码 |
| 230 | + |
| 231 | +## 测试运行 |
| 232 | + |
| 233 | +将代码保存为game.py,然后在控制台运行: |
| 234 | + |
| 235 | +``` |
| 236 | +python game.py |
| 237 | +``` |
| 238 | + |
| 239 | + |
0 commit comments