閑的沒事,用chatpgt弄了個小游戲,2048,利用pygame實作,無額外貼圖,
只需要告訴他寫個python游戲2048,只用pygame實作,不要額外貼圖,然后在他暫停后說請繼續,最后會有一些bug,把報錯告訴他,慢慢改,10分鐘就可以完成,
效果如下圖:
具體代碼如下:
import pygame
import random
# 游戲界面的尺寸
WIDTH = 400
HEIGHT = 400
# 游戲界面的背景顏色
BACKGROUND_COLOR = (187, 173, 160)
TEXT_COLOR = (119, 110, 101)
GAME_OVER_COLOR = (0, 0, 0)
# 格子的尺寸和間距
CELL_SIZE = 80
CELL_MARGIN = 10
TILE_SIZE = 100
TILE_MARGIN = 10
GAME_OVER_FONT_SIZE = 72
SIZE = 500
# 數字方塊的顏色和字體
TILE_COLORS = {
0: (204, 192, 179),
2: (238, 228, 218),
4: (237, 224, 200),
8: (242, 177, 121),
16: (245, 149, 99),
32: (246, 124, 95),
64: (246, 94, 59),
128: (237, 207, 114),
256: (237, 204, 97),
512: (237, 200, 80),
1024: (237, 197, 63),
2048: (237, 194, 46),
4096: (174, 207, 48)
}
pygame.font.init()
TILE_FONT = pygame.font.SysFont("Arial", 40, bold=True)
def draw_text(surface, text, color, x, y):
"""
在指定位置繪制文本
"""
text_surface = TILE_FONT.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
surface.blit(text_surface, text_rect)
def draw_tile(surface, x, y, value):
"""
在指定位置繪制數字方塊
"""
color = TILE_COLORS[value]
pygame.draw.rect(surface, color, (x, y, CELL_SIZE, CELL_SIZE))
if value != 0:
text_color = (255, 255, 255) if value < 8 else (119, 110, 101)
draw_text(surface, str(value), text_color, x + CELL_SIZE / 2, y + CELL_SIZE / 2)
def draw_board(surface, board):
"""
繪制游戲界面上的數字方塊
"""
for row in range(4):
for col in range(4):
x = col * CELL_SIZE + (col + 1) * CELL_MARGIN
y = row * CELL_SIZE + (row + 1) * CELL_MARGIN
draw_tile(surface, x, y, board[row][col])
def generate_tile(board):
"""
在空白格子中隨機生成數字方塊
"""
available_cells = []
for row in range(4):
for col in range(4):
if board[row][col] == 0:
available_cells.append((row, col))
if available_cells:
row, col = random.choice(available_cells)
board[row][col] = 2
return True
else:
return False
def move_tiles(board, direction):
"""
根據方向移動數字方塊
"""
moved = False
if direction == "left":
for row in range(4):
for col in range(1, 4):
if board[row][col] != 0:
for k in range(col):
if board[row][k] == 0 and no_obstacles(board, row, k, row, col):
board[row][k] = board[row][col]
board[row][col] = 0
moved = True
break
elif board[row][k] == board[row][col] and no_obstacles(board, row, k, row, col):
board[row][k] *= 2
board[row][col] = 0
moved = True
break
elif direction == "right":
for row in range(4):
for col in range(2, -1, -1):
if board[row][col] != 0:
for k in range(3, col, -1):
if board[row][k] == 0 and no_obstacles(board, row, col, row, k):
board[row][k] = board[row][col]
board[row][col] = 0
moved = True
break
elif board[row][k] == board[row][col] and no_obstacles(board, row, col, row, k):
board[row][k] *= 2
board[row][col] = 0
moved = True
break
elif direction == "up":
for col in range(4):
for row in range(1, 4):
if board[row][col] != 0:
for k in range(row):
if board[k][col] == 0 and no_obstacles(board, k, col, row, col):
board[k][col] = board[row][col]
board[row][col] = 0
moved = True
break
elif board[k][col] == board[row][col] and no_obstacles(board, k, col, row, col):
board[k][col] *= 2
board[row][col] = 0
moved = True
break
elif direction == "down":
for col in range(4):
for row in range(2, -1, -1):
if board[row][col] != 0:
for k in range(3, row, -1):
if board[k][col] == 0 and no_obstacles(board, row, col, k, col):
board[k][col] = board[row][col]
board[row][col] = 0
moved = True
break
elif board[k][col] == board[row][col] and no_obstacles(board, row, col, k, col):
board[k][col] *= 2
board[row][col] = 0
moved = True
break
return moved
def no_obstacles(board, row1, col1, row2, col2):
"""
判斷兩個位置之間是否有障礙物(即是否有數字方塊)
"""
if row1 == row2:
for col in range(col1 + 1, col2):
if board[row1][col] != 0:
return False
elif col1 == col2:
for row in range(row1 + 1, row2):
if board[row][col1] != 0:
return False
return True
def game_over(board):
"""
判斷游戲是否結束
"""
for row in range(4):
for col in range(4):
if board[row][col] == 0:
return False
for row in range(4):
for col in range(3):
if board[row][col] == board[row][col + 1]:
return False
for col in range(4):
for row in range(3):
if board[row][col] == board[row + 1][col]:
return False
return True
def add_new_tile(board):
# 查找所有空白的方格
empty_tiles = [(i, j) for i in range(4) for j in range(4) if board[i][j] == 0]
# 如果沒有空白的方格,回傳 False
if len(empty_tiles) == 0:
return False
# 隨機選擇一個空白的方格,并隨機放入數字 2 或 4
row, col = random.choice(empty_tiles)
board[row][col] = random.choice([2, 4])
return True
def get_tile_color(value):
colors = {
2: (238, 228, 218),
4: (237, 224, 200),
8: (242, 177, 121),
16: (245, 149, 99),
32: (246, 124, 95),
64: (246, 94, 59),
128: (237, 207, 114),
256: (237, 204, 97),
512: (237, 200, 80),
1024: (237, 197, 63),
2048: (237, 194, 46),
}
return colors.get(value, (0, 0, 0))
def get_font_size(value):
if value < 100:
return 64
elif value < 1000:
return 48
else:
return 36
def main():
# 初始化游戲界面
pygame.init()
screen = pygame.display.set_mode((SIZE, SIZE))
pygame.display.set_caption("2048 Game")
# 初始化游戲板
board = [[0] * 4 for i in range(4)]
add_new_tile(board)
add_new_tile(board)
while True:
# 處理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
moved = False
if event.key == pygame.K_LEFT:
moved = move_tiles(board, "left")
elif event.key == pygame.K_RIGHT:
moved = move_tiles(board, "right")
elif event.key == pygame.K_UP:
moved = move_tiles(board, "up")
elif event.key == pygame.K_DOWN:
moved = move_tiles(board, "down")
if moved:
add_new_tile(board)
# 繪制游戲界面
screen.fill(BACKGROUND_COLOR)
for row in range(4):
for col in range(4):
x = col * TILE_SIZE + TILE_MARGIN * (col + 1)
y = row * TILE_SIZE + TILE_MARGIN * (row + 1)
tile_color = get_tile_color(board[row][col])
pygame.draw.rect(screen, tile_color, (x, y, TILE_SIZE, TILE_SIZE))
if board[row][col] != 0:
font_size = get_font_size(board[row][col])
font = pygame.font.Font(None, font_size)
text = font.render(str(board[row][col]), True, TEXT_COLOR)
text_rect = text.get_rect()
text_rect.center = (x + TILE_SIZE / 2, y + TILE_SIZE / 2)
screen.blit(text, text_rect)
# 判斷游戲是否結束
if game_over(board):
font = pygame.font.Font(None, GAME_OVER_FONT_SIZE)
text = font.render("Game Over!", True, GAME_OVER_COLOR)
text_rect = text.get_rect()
text_rect.center = (SIZE / 2, SIZE / 2)
screen.blit(text, text_rect)
pygame.display.update()
if __name__ == "__main__":
main()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/547867.html
標籤:Python
上一篇:Python工具箱系列(二十八)