heronylee LV
发表于 2025-4-27 18:28:52
deepseek生成一个pygame版本的扫雷游戏,mac下可以玩
界面用cursor美化了下
附上代码- import pygame
- import sys
- import random
- # Initialize Pygame
- pygame.init()
- # Constants
- WIDTH, HEIGHT = 800, 600
- GRID_SIZE = 40
- GRID_WIDTH = WIDTH // GRID_SIZE
- GRID_HEIGHT = (HEIGHT - 50) // GRID_SIZE # 50 pixels for the status bar
- MINES = 40
- # Colors
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- GRAY = (128, 128, 128)
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- LIGHT_GRAY = (220, 220, 220) # 更亮的灰色
- DARK_GRAY = (100, 100, 100) # 更深的灰色
- BLUE = (65, 105, 225) # 添加蓝色
- GOLD = (255, 215, 0) # 添加金色
- BG_COLOR = (240, 240, 240) # 背景色
- # Set up the display
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Minesweeper")
- # Game variables
- board = [[0 for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
- revealed = [[False for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
- flags = [[False for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
- game_over = False
- win = False
- # Load images
- flag_image = pygame.image.load('flag.png')
- flag_image = pygame.transform.scale(flag_image, (GRID_SIZE-10, GRID_SIZE-10)) # 稍微缩小图标
- explosion_image = pygame.image.load('explosion.png')
- explosion_image = pygame.transform.scale(explosion_image, (GRID_SIZE-10, GRID_SIZE-10))
- # Place mines
- def place_mines():
- mines_placed = 0
- while mines_placed < MINES:
- x = random.randint(0, GRID_WIDTH - 1)
- y = random.randint(0, GRID_HEIGHT - 1)
- if board[y][x] != -1:
- board[y][x] = -1
- mines_placed += 1
- # Calculate numbers
- def calculate_numbers():
- for y in range(GRID_HEIGHT):
- for x in range(GRID_WIDTH):
- if board[y][x] == -1:
- continue
- mines = 0
- for i in range(-1, 2):
- for j in range(-1, 2):
- if 0 <= y + i < GRID_HEIGHT and 0 <= x + j < GRID_WIDTH:
- if board[y + i][x + j] == -1:
- mines += 1
- board[y][x] = mines
- # Reveal cell
- def reveal_cell(x, y):
- global game_over
- if revealed[y][x] or flags[y][x]:
- return
- revealed[y][x] = True
- if board[y][x] == -1:
- game_over = True
- return
- if board[y][x] == 0:
- for i in range(-1, 2):
- for j in range(-1, 2):
- if 0 <= y + i < GRID_HEIGHT and 0 <= x + j < GRID_WIDTH:
- reveal_cell(x + j, y + i)
- # Check win condition
- def check_win():
- global win
- for y in range(GRID_HEIGHT):
- for x in range(GRID_WIDTH):
- if board[y][x] != -1 and not revealed[y][x]:
- return
- win = True
- # Draw the board
- def draw_board():
- for y in range(GRID_HEIGHT):
- for x in range(GRID_WIDTH):
- rect = pygame.Rect(x * GRID_SIZE, y * GRID_SIZE + 50, GRID_SIZE, GRID_SIZE)
- if revealed[y][x]:
- if board[y][x] == -1:
- pygame.draw.rect(screen, RED, rect) # 红色背景
- screen.blit(explosion_image, (rect.x + 5, rect.y + 5)) # 居中显示
- else:
- pygame.draw.rect(screen, WHITE, rect)
- if board[y][x] > 0:
- font = pygame.font.Font(None, 36)
- # 不同数字不同颜色
- colors = [BLUE, GREEN, RED, (128, 0, 128), (128, 0, 0),
- (64, 224, 208), BLACK, GRAY]
- text = font.render(str(board[y][x]), True, colors[board[y][x]-1])
- text_rect = text.get_rect(center=rect.center)
- screen.blit(text, text_rect)
- elif flags[y][x]:
- pygame.draw.rect(screen, LIGHT_GRAY, rect)
- screen.blit(flag_image, (rect.x + 5, rect.y + 5)) # 居中显示
- else:
- # 3D效果的方块
- pygame.draw.rect(screen, LIGHT_GRAY, rect)
- pygame.draw.polygon(screen, WHITE,
- [(rect.left, rect.bottom), (rect.left, rect.top),
- (rect.right, rect.top), (rect.right-2, rect.top+2),
- (rect.left+2, rect.top+2), (rect.left+2, rect.bottom-2)])
- pygame.draw.polygon(screen, DARK_GRAY,
- [(rect.left, rect.bottom), (rect.right, rect.bottom),
- (rect.right, rect.top), (rect.right-2, rect.top+2),
- (rect.right-2, rect.bottom-2), (rect.left+2, rect.bottom-2)])
- # Draw status bar
- def draw_status_bar():
- # 渐变背景
- for i in range(50):
- color = (GRAY[0], GRAY[1], min(255, GRAY[2] + i*2))
- pygame.draw.line(screen, color, (0, i), (WIDTH, i))
-
- font = pygame.font.Font(None, 36)
- if game_over:
- text = font.render(&#34;Game Over - Press &#39;R&#39; to restart&#34;, True, RED)
- # 添加阴影效果
- shadow = font.render(&#34;Game Over - Press &#39;R&#39; to restart&#34;, True, BLACK)
- screen.blit(shadow, (WIDTH // 2 - text.get_width() // 2 + 2, 27))
- elif win:
- text = font.render(&#34;You Win! - Press &#39;R&#39; to restart&#34;, True, GOLD)
- shadow = font.render(&#34;You Win! - Press &#39;R&#39; to restart&#34;, True, BLACK)
- screen.blit(shadow, (WIDTH // 2 - text.get_width() // 2 + 2, 27))
- else:
- text = font.render(&#34;Minesweeper - Left click to reveal, Right click to flag&#34;, True, WHITE)
- text_rect = text.get_rect(center=(WIDTH // 2, 25))
- screen.blit(text, text_rect)
- # Main game loop
- def main():
- global game_over, win, revealed, flags, board
- place_mines()
- calculate_numbers()
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == pygame.MOUSEBUTTONDOWN and not game_over and not win:
- x, y = event.pos
- grid_x = x // GRID_SIZE
- grid_y = (y - 50) // GRID_SIZE
- if 0 <= grid_y < GRID_HEIGHT:
- if event.button == 1: # Left click
- reveal_cell(grid_x, grid_y)
- check_win()
- elif event.button == 3: # Right click
- flags[grid_y][grid_x] = not flags[grid_y][grid_x]
- elif event.type == pygame.KEYDOWN and (game_over or win):
- if event.key == pygame.K_r:
- game_over = False
- win = False
- revealed = [[False for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
- flags = [[False for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
- board = [[0 for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
- place_mines()
- calculate_numbers()
- screen.fill(BG_COLOR) # 使用新的背景色
- draw_board()
- draw_status_bar()
- pygame.display.flip()
- pygame.quit()
- sys.exit()
- if __name__ == &#34;__main__&#34;:
- main()
复制代码 小图标网上找的
备注:这个还是v2.5的代码能力
发现一个WEB版扫雷游戏 - 经典小游戏网页版扫雷,这个界面、功能确实好,要打磨成这样,工作量还是很大的。 |
|