pygame2048游戏
时间: 2023-08-23 20:06:38 浏览: 85
python pygame实现2048游戏
好的,让我们来制作一个2048游戏吧,使用Pygame库。
首先,我们需要安装Pygame库。可以使用以下命令在终端中安装它:
```
pip install pygame
```
接下来,我们可以开始编写代码。首先,我们需要导入必要的库和定义一些常量:
```python
import pygame
import random
# 定义常量
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 500
TILE_SIZE = 100
GRID_SIZE = 4
GRID_WIDTH = TILE_SIZE * GRID_SIZE
GRID_HEIGHT = TILE_SIZE * GRID_SIZE
FONT_SIZE = 32
SCORE_POS = (10, GRID_HEIGHT + 10)
GAMEOVER_POS = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
# 初始化Pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("2048")
# 加载字体
font = pygame.font.Font(None, FONT_SIZE)
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
# 定义方向
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
```
接下来,我们需要定义一个Tile类,表示每个方块:
```python
class Tile:
def __init__(self, row, col, value):
self.row = row
self.col = col
self.value = value
def draw(self):
# 计算方块位置
x = self.col * TILE_SIZE + TILE_SIZE // 2
y = self.row * TILE_SIZE + TILE_SIZE // 2
# 计算方块颜色
if self.value == 2:
color = (238, 228, 218)
elif self.value == 4:
color = (237, 224, 200)
elif self.value == 8:
color = (242, 177, 121)
elif self.value == 16:
color = (245, 149, 99)
elif self.value == 32:
color = (246, 124, 95)
elif self.value == 64:
color = (246, 94, 59)
elif self.value == 128:
color = (237, 207, 114)
elif self.value == 256:
color = (237, 204, 97)
elif self.value == 512:
color = (237, 200, 80)
elif self.value == 1024:
color = (237, 197, 63)
elif self.value == 2048:
color = (237, 194, 46)
else:
color = (205, 193, 180)
# 绘制方块
pygame.draw.rect(screen, color, (x - TILE_SIZE // 2, y - TILE_SIZE // 2, TILE_SIZE, TILE_SIZE))
# 绘制方块中的数字
text = font.render(str(self.value), True, BLACK)
text_rect = text.get_rect(center=(x, y))
screen.blit(text, text_rect)
```
然后,我们需要定义一个Grid类,表示整个游戏的网格:
```python
class Grid:
def __init__(self):
self.tiles = [[None] * GRID_SIZE for _ in range(GRID_SIZE)]
self.score = 0
self.game_over = False
def add_tile(self):
# 在空白方块中随机添加一个方块
empty_tiles = [(row, col) for row in range(GRID_SIZE) for col in range(GRID_SIZE) if self.tiles[row][col] is None]
if not empty_tiles:
return
row, col = random.choice(empty_tiles)
self.tiles[row][col] = Tile(row, col, 2 if random.random() < 0.9 else 4)
def draw(self):
# 绘制方块和网格
for row in range(GRID_SIZE):
for col in range(GRID_SIZE):
tile = self.tiles[row][col]
if tile is None:
color = GRAY
else:
tile.draw()
for i in range(GRID_SIZE + 1):
pygame.draw.line(screen, BLACK, (i * TILE_SIZE, 0), (i * TILE_SIZE, GRID_HEIGHT))
pygame.draw.line(screen, BLACK, (0, i * TILE_SIZE), (GRID_WIDTH, i * TILE_SIZE))
# 绘制得分和游戏结束提示
text = font.render("Score: " + str(self.score), True, BLACK)
screen.blit(text, SCORE_POS)
if self.game_over:
text = font.render("Game Over!", True, BLACK)
text_rect = text.get_rect(center=GAMEOVER_POS)
screen.blit(text, text_rect)
def move(self, direction):
# 根据方向移动方块
if direction == UP:
for col in range(GRID_SIZE):
i = 0
while i < GRID_SIZE - 1:
j = i + 1
while j < GRID_SIZE and self.tiles[j][col] is None:
j += 1
if j == GRID_SIZE:
break
if self.tiles[i][col] is None:
self.tiles[i][col] = self.tiles[j][col]
self.tiles[j][col] = None
elif self.tiles[i][col].value == self.tiles[j][col].value:
self.tiles[i][col].value *= 2
self.score += self.tiles[i][col].value
self.tiles[j][col] = None
i += 1
elif direction == RIGHT:
for row in range(GRID_SIZE):
i = GRID_SIZE - 1
while i > 0:
j = i - 1
while j >= 0 and self.tiles[row][j] is None:
j -= 1
if j < 0:
break
if self.tiles[row][i] is None:
self.tiles[row][i] = self.tiles[row][j]
self.tiles[row][j] = None
elif self.tiles[row][i].value == self.tiles[row][j].value:
self.tiles[row][i].value *= 2
self.score += self.tiles[row][i].value
self.tiles[row][j] = None
i -= 1
elif direction == DOWN:
for col in range(GRID_SIZE):
i = GRID_SIZE - 1
while i > 0:
j = i - 1
while j >= 0 and self.tiles[j][col] is None:
j -= 1
if j < 0:
break
if self.tiles[i][col] is None:
self.tiles[i][col] = self.tiles[j][col]
self.tiles[j][col] = None
elif self.tiles[i][col].value == self.tiles[j][col].value:
self.tiles[i][col].value *= 2
self.score += self.tiles[i][col].value
self.tiles[j][col] = None
i -= 1
elif direction == LEFT:
for row in range(GRID_SIZE):
i = 0
while i < GRID_SIZE - 1:
j = i + 1
while j < GRID_SIZE and self.tiles[row][j] is None:
j += 1
if j == GRID_SIZE:
break
if self.tiles[row][i] is None:
self.tiles[row][i] = self.tiles[row][j]
self.tiles[row][j] = None
elif self.tiles[row][i].value == self.tiles[row][j].value:
self.tiles[row][i].value *= 2
self.score += self.tiles[row][i].value
self.tiles[row][j] = None
i += 1
# 判断游戏是否结束
empty_tiles = [(row, col) for row in range(GRID_SIZE) for col in range(GRID_SIZE) if self.tiles[row][col] is None]
if not empty_tiles:
for row in range(GRID_SIZE):
for col in range(GRID_SIZE):
if row > 0 and self.tiles[row][col].value == self.tiles[row - 1][col].value:
return
if col > 0 and self.tiles[row][col].value == self.tiles[row][col - 1].value:
return
if row < GRID_SIZE - 1 and self.tiles[row][col].value == self.tiles[row + 1][col].value:
return
if col < GRID_SIZE - 1 and self.tiles[row][col].value == self.tiles[row][col + 1].value:
return
self.game_over = True
```
最后,我们可以使用以下代码来运行游戏:
```python
def main():
# 创建游戏网格
grid = Grid()
# 添加两个方块
grid.add_tile()
grid.add_tile()
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
grid.move(UP)
grid.add_tile()
elif event.key == pygame.K_RIGHT:
grid.move(RIGHT)
grid.add_tile()
elif event.key == pygame.K_DOWN:
grid.move(DOWN)
grid.add_tile()
elif event.key == pygame.K_LEFT:
grid.move(LEFT)
grid.add_tile()
# 绘制游戏窗口
screen.fill(WHITE)
grid.draw()
pygame.display.flip()
if __name__ == "__main__":
main()
```
这样,我们就完成了一个简单的2048游戏。玩得愉快!
阅读全文