百木园-与人分享,
就是让自己快乐。

chatgpt写程序-python小游戏-2048-pygame

闲的没事,用chatpgt弄了个小游戏,2048,利用pygame实现,无额外贴图。

只需要告诉他写个python游戏2048,只用pygame实现,不要额外贴图。然后在他暂停后说请继续,最后会有一些bug,把报错告诉他,慢慢改,10分钟就可以完成。

效果如下图:

 

 

 

 

具体代码如下:

  1 import pygame
  2 import random
  3 
  4 # 游戏界面的尺寸
  5 WIDTH = 400
  6 HEIGHT = 400
  7 
  8 # 游戏界面的背景颜色
  9 BACKGROUND_COLOR = (187, 173, 160)
 10 TEXT_COLOR = (119, 110, 101)
 11 GAME_OVER_COLOR = (0, 0, 0)
 12 
 13 # 格子的尺寸和间距
 14 CELL_SIZE = 80
 15 CELL_MARGIN = 10
 16 TILE_SIZE = 100
 17 TILE_MARGIN = 10
 18 GAME_OVER_FONT_SIZE = 72
 19 SIZE = 500
 20 
 21 # 数字方块的颜色和字体
 22 TILE_COLORS = {
 23     0: (204, 192, 179),
 24     2: (238, 228, 218),
 25     4: (237, 224, 200),
 26     8: (242, 177, 121),
 27     16: (245, 149, 99),
 28     32: (246, 124, 95),
 29     64: (246, 94, 59),
 30     128: (237, 207, 114),
 31     256: (237, 204, 97),
 32     512: (237, 200, 80),
 33     1024: (237, 197, 63),
 34     2048: (237, 194, 46),
 35     4096: (174, 207, 48)
 36 }
 37 pygame.font.init()
 38 TILE_FONT = pygame.font.SysFont(\"Arial\", 40, bold=True)
 39 
 40 
 41 def draw_text(surface, text, color, x, y):
 42     \"\"\"
 43     在指定位置绘制文本
 44     \"\"\"
 45     text_surface = TILE_FONT.render(text, True, color)
 46     text_rect = text_surface.get_rect()
 47     text_rect.center = (x, y)
 48     surface.blit(text_surface, text_rect)
 49 
 50 
 51 def draw_tile(surface, x, y, value):
 52     \"\"\"
 53     在指定位置绘制数字方块
 54     \"\"\"
 55     color = TILE_COLORS[value]
 56     pygame.draw.rect(surface, color, (x, y, CELL_SIZE, CELL_SIZE))
 57     if value != 0:
 58         text_color = (255, 255, 255) if value < 8 else (119, 110, 101)
 59         draw_text(surface, str(value), text_color, x + CELL_SIZE / 2, y + CELL_SIZE / 2)
 60 
 61 
 62 def draw_board(surface, board):
 63     \"\"\"
 64     绘制游戏界面上的数字方块
 65     \"\"\"
 66     for row in range(4):
 67         for col in range(4):
 68             x = col * CELL_SIZE + (col + 1) * CELL_MARGIN
 69             y = row * CELL_SIZE + (row + 1) * CELL_MARGIN
 70             draw_tile(surface, x, y, board[row][col])
 71 
 72 
 73 def generate_tile(board):
 74     \"\"\"
 75     在空白格子中随机生成数字方块
 76     \"\"\"
 77     available_cells = []
 78     for row in range(4):
 79         for col in range(4):
 80             if board[row][col] == 0:
 81                 available_cells.append((row, col))
 82     if available_cells:
 83         row, col = random.choice(available_cells)
 84         board[row][col] = 2
 85         return True
 86     else:
 87         return False
 88 
 89 
 90 def move_tiles(board, direction):
 91     \"\"\"
 92     根据方向移动数字方块
 93     \"\"\"
 94     moved = False
 95     if direction == \"left\":
 96         for row in range(4):
 97             for col in range(1, 4):
 98                 if board[row][col] != 0:
 99                     for k in range(col):
100                         if board[row][k] == 0 and no_obstacles(board, row, k, row, col):
101                             board[row][k] = board[row][col]
102                             board[row][col] = 0
103                             moved = True
104                             break
105                         elif board[row][k] == board[row][col] and no_obstacles(board, row, k, row, col):
106                             board[row][k] *= 2
107                             board[row][col] = 0
108                             moved = True
109                             break
110     elif direction == \"right\":
111         for row in range(4):
112             for col in range(2, -1, -1):
113                 if board[row][col] != 0:
114                     for k in range(3, col, -1):
115                         if board[row][k] == 0 and no_obstacles(board, row, col, row, k):
116                             board[row][k] = board[row][col]
117                             board[row][col] = 0
118                             moved = True
119                             break
120                         elif board[row][k] == board[row][col] and no_obstacles(board, row, col, row, k):
121                             board[row][k] *= 2
122                             board[row][col] = 0
123                             moved = True
124                             break
125     elif direction == \"up\":
126         for col in range(4):
127             for row in range(1, 4):
128                 if board[row][col] != 0:
129                     for k in range(row):
130                         if board[k][col] == 0 and no_obstacles(board, k, col, row, col):
131                             board[k][col] = board[row][col]
132                             board[row][col] = 0
133                             moved = True
134                             break
135                         elif board[k][col] == board[row][col] and no_obstacles(board, k, col, row, col):
136                             board[k][col] *= 2
137                             board[row][col] = 0
138                             moved = True
139                             break
140     elif direction == \"down\":
141         for col in range(4):
142             for row in range(2, -1, -1):
143                 if board[row][col] != 0:
144                     for k in range(3, row, -1):
145                         if board[k][col] == 0 and no_obstacles(board, row, col, k, col):
146                             board[k][col] = board[row][col]
147                             board[row][col] = 0
148                             moved = True
149                             break
150                         elif board[k][col] == board[row][col] and no_obstacles(board, row, col, k, col):
151                             board[k][col] *= 2
152                             board[row][col] = 0
153                             moved = True
154                             break
155     return moved
156 
157 
158 def no_obstacles(board, row1, col1, row2, col2):
159     \"\"\"
160     判断两个位置之间是否有障碍物(即是否有数字方块)
161     \"\"\"
162     if row1 == row2:
163         for col in range(col1 + 1, col2):
164             if board[row1][col] != 0:
165                 return False
166     elif col1 == col2:
167         for row in range(row1 + 1, row2):
168             if board[row][col1] != 0:
169                 return False
170     return True
171 
172 
173 def game_over(board):
174     \"\"\"
175     判断游戏是否结束
176     \"\"\"
177     for row in range(4):
178         for col in range(4):
179             if board[row][col] == 0:
180                 return False
181     for row in range(4):
182         for col in range(3):
183             if board[row][col] == board[row][col + 1]:
184                 return False
185     for col in range(4):
186         for row in range(3):
187             if board[row][col] == board[row + 1][col]:
188                 return False
189     return True
190 
191 
192 def add_new_tile(board):
193     # 查找所有空白的方格
194     empty_tiles = [(i, j) for i in range(4) for j in range(4) if board[i][j] == 0]
195 
196     # 如果没有空白的方格,返回 False
197     if len(empty_tiles) == 0:
198         return False
199 
200     # 随机选择一个空白的方格,并随机放入数字 2 或 4
201     row, col = random.choice(empty_tiles)
202     board[row][col] = random.choice([2, 4])
203 
204     return True
205 
206 
207 def get_tile_color(value):
208     colors = {
209         2: (238, 228, 218),
210         4: (237, 224, 200),
211         8: (242, 177, 121),
212         16: (245, 149, 99),
213         32: (246, 124, 95),
214         64: (246, 94, 59),
215         128: (237, 207, 114),
216         256: (237, 204, 97),
217         512: (237, 200, 80),
218         1024: (237, 197, 63),
219         2048: (237, 194, 46),
220     }
221     return colors.get(value, (0, 0, 0))
222 
223 
224 def get_font_size(value):
225     if value < 100:
226         return 64
227     elif value < 1000:
228         return 48
229     else:
230         return 36
231 
232 
233 def main():
234     # 初始化游戏界面
235     pygame.init()
236     screen = pygame.display.set_mode((SIZE, SIZE))
237     pygame.display.set_caption(\"2048 Game\")
238 
239     # 初始化游戏板
240     board = [[0] * 4 for i in range(4)]
241     add_new_tile(board)
242     add_new_tile(board)
243 
244     while True:
245         # 处理事件
246         for event in pygame.event.get():
247             if event.type == pygame.QUIT:
248                 pygame.quit()
249                 sys.exit()
250             elif event.type == pygame.KEYDOWN:
251                 moved = False
252                 if event.key == pygame.K_LEFT:
253                     moved = move_tiles(board, \"left\")
254                 elif event.key == pygame.K_RIGHT:
255                     moved = move_tiles(board, \"right\")
256                 elif event.key == pygame.K_UP:
257                     moved = move_tiles(board, \"up\")
258                 elif event.key == pygame.K_DOWN:
259                     moved = move_tiles(board, \"down\")
260                 if moved:
261                     add_new_tile(board)
262 
263         # 绘制游戏界面
264         screen.fill(BACKGROUND_COLOR)
265         for row in range(4):
266             for col in range(4):
267                 x = col * TILE_SIZE + TILE_MARGIN * (col + 1)
268                 y = row * TILE_SIZE + TILE_MARGIN * (row + 1)
269                 tile_color = get_tile_color(board[row][col])
270                 pygame.draw.rect(screen, tile_color, (x, y, TILE_SIZE, TILE_SIZE))
271                 if board[row][col] != 0:
272                     font_size = get_font_size(board[row][col])
273                     font = pygame.font.Font(None, font_size)
274                     text = font.render(str(board[row][col]), True, TEXT_COLOR)
275                     text_rect = text.get_rect()
276                     text_rect.center = (x + TILE_SIZE / 2, y + TILE_SIZE / 2)
277                     screen.blit(text, text_rect)
278 
279         # 判断游戏是否结束
280         if game_over(board):
281             font = pygame.font.Font(None, GAME_OVER_FONT_SIZE)
282             text = font.render(\"Game Over!\", True, GAME_OVER_COLOR)
283             text_rect = text.get_rect()
284             text_rect.center = (SIZE / 2, SIZE / 2)
285             screen.blit(text, text_rect)
286 
287         pygame.display.update()
288 
289 
290 if __name__ == \"__main__\":
291     main()

 


来源:https://www.cnblogs.com/Brinshy/p/17247146.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » chatgpt写程序-python小游戏-2048-pygame

相关推荐

  • 暂无文章