chatgpt写程序-python小游戏-2048-pygame
闲的没事,用chatpgt弄了个小游戏,2048,利用pygame实现,无额外贴图。只需要告诉他写个python游戏2048,只用pygame实现,不要额外贴图。然后在他暂停后说请继续,最后会有一些bug,把报错告诉他,慢慢改,10分钟就可以完成。
效果如下图:
https://appdocs.wpscdn.cn/api/v3/office/copy/MXhmYWZYVml5S1ZsMkNscjBQUGZ5KzI3VmlnQWNkVmFjYmNUbk43TmFvSGVxTkFBdTlRSXkwZ2s2ZFNiOFBOWGZBdEx0M2NjdlZyYkNJUFdsZGNsL1pRSmFaUk9tNGI2bnVhN3ZKMlpiVjJPTTl0a01hMWdldFRiMFdVVzlPbElGNHoraWQ3U2tSemVJN2xMVVU0VHhEdEhueFJCUDdPZDNqWmZMOXV1QnpaTUlpRGRaRDF5Mk0rVG9OZGFvZ2Z0Mzk4OHFhMjZYZWVhaEhjN0VwWmRwN0M0Y3dvN3ZrcmFFNlBtRm1vdVROOUo2QXNmbTd6OC95RTZyOHd3K0x2cXEwSHVHVGJLQUl2Y1JQMjRqOWVDUUpWUlVCeDVhRHBRVitmL05jb1A1V1RGb09Lbm9CWHdlalk1T1dpRg==/attach/object/4PMQIBAAII?
https://appdocs.wpscdn.cn/api/v3/office/copy/MXhmYWZYVml5S1ZsMkNscjBQUGZ5KzI3VmlnQWNkVmFjYmNUbk43TmFvSGVxTkFBdTlRSXkwZ2s2ZFNiOFBOWGZBdEx0M2NjdlZyYkNJUFdsZGNsL1pRSmFaUk9tNGI2bnVhN3ZKMlpiVjJPTTl0a01hMWdldFRiMFdVVzlPbElGNHoraWQ3U2tSemVJN2xMVVU0VHhEdEhueFJCUDdPZDNqWmZMOXV1QnpaTUlpRGRaRDF5Mk0rVG9OZGFvZ2Z0Mzk4OHFhMjZYZWVhaEhjN0VwWmRwN0M0Y3dvN3ZrcmFFNlBtRm1vdVROOUo2QXNmbTd6OC95RTZyOHd3K0x2cXEwSHVHVGJLQUl2Y1JQMjRqOWVDUUpWUlVCeDVhRHBRVitmL05jb1A1V1RGb09Lbm9CWHdlalk1T1dpRg==/attach/object/7LNAIBAA24?
https://appdocs.wpscdn.cn/api/v3/office/copy/MXhmYWZYVml5S1ZsMkNscjBQUGZ5KzI3VmlnQWNkVmFjYmNUbk43TmFvSGVxTkFBdTlRSXkwZ2s2ZFNiOFBOWGZBdEx0M2NjdlZyYkNJUFdsZGNsL1pRSmFaUk9tNGI2bnVhN3ZKMlpiVjJPTTl0a01hMWdldFRiMFdVVzlPbElGNHoraWQ3U2tSemVJN2xMVVU0VHhEdEhueFJCUDdPZDNqWmZMOXV1QnpaTUlpRGRaRDF5Mk0rVG9OZGFvZ2Z0Mzk4OHFhMjZYZWVhaEhjN0VwWmRwN0M0Y3dvN3ZrcmFFNlBtRm1vdVROOUo2QXNmbTd6OC95RTZyOHd3K0x2cXEwSHVHVGJLQUl2Y1JQMjRqOWVDUUpWUlVCeDVhRHBRVitmL05jb1A1V1RGb09Lbm9CWHdlalk1T1dpRg==/attach/object/KHNAIBAAQA?
具体代码如下:
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
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)
def generate_tile(board):
"""
在空白格子中随机生成数字方块
"""
available_cells = []
for row in range(4):
for col in range(4):
if board == 0:
available_cells.append((row, col))
if available_cells:
row, col = random.choice(available_cells)
board = 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 != 0:
for k in range(col):
if board == 0 and no_obstacles(board, row, k, row, col):
board = board
board = 0
moved = True
break
elif board == board and no_obstacles(board, row, k, row, col):
board *= 2
board = 0
moved = True
break
elif direction == "right":
for row in range(4):
for col in range(2, -1, -1):
if board != 0:
for k in range(3, col, -1):
if board == 0 and no_obstacles(board, row, col, row, k):
board = board
board = 0
moved = True
break
elif board == board and no_obstacles(board, row, col, row, k):
board *= 2
board = 0
moved = True
break
elif direction == "up":
for col in range(4):
for row in range(1, 4):
if board != 0:
for k in range(row):
if board == 0 and no_obstacles(board, k, col, row, col):
board = board
board = 0
moved = True
break
elif board == board and no_obstacles(board, k, col, row, col):
board *= 2
board = 0
moved = True
break
elif direction == "down":
for col in range(4):
for row in range(2, -1, -1):
if board != 0:
for k in range(3, row, -1):
if board == 0 and no_obstacles(board, row, col, k, col):
board = board
board = 0
moved = True
break
elif board == board and no_obstacles(board, row, col, k, col):
board *= 2
board = 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 != 0:
return False
elif col1 == col2:
for row in range(row1 + 1, row2):
if board != 0:
return False
return True
def game_over(board):
"""
判断游戏是否结束
"""
for row in range(4):
for col in range(4):
if board == 0:
return False
for row in range(4):
for col in range(3):
if board == board:
return False
for col in range(4):
for row in range(3):
if board == board:
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 == 0]
# 如果没有空白的方格,返回 False
if len(empty_tiles) == 0:
return False
# 随机选择一个空白的方格,并随机放入数字 2 或 4
row, col = random.choice(empty_tiles)
board = random.choice()
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 = [ * 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)
pygame.draw.rect(screen, tile_color, (x, y, TILE_SIZE, TILE_SIZE))
if board != 0:
font_size = get_font_size(board)
font = pygame.font.Font(None, font_size)
text = font.render(str(board), 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.cnblogs.com/Brinshy/p/17247146.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]