用pygame实现
时间: 2023-11-12 21:17:25 浏览: 40
以下是使用Pygame实现的2048游戏的代码:
```python
import pygame
import random
pygame.init()
# 定义颜色常量
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 定义游戏参数常量
SCREEN_WIDTH = 450
SCREEN_HEIGHT = 550
CELL_SIZE = 100
CELL_PADDING = 10
GRID_SIZE = CELL_SIZE * 4 + CELL_PADDING * 5
FONT_SIZE = 64
# 定义游戏面板类
class Board:
def __init__(self):
self.grid = [[0 for _ in range(4)] for _ in range(4)]
self.score = 0
self.add_random_number()
self.add_random_number()
def add_random_number(self):
empty_cells = [(i, j) for i in range(4) for j in range(4) if self.grid[i][j] == 0]
if not empty_cells:
return False
i, j = random.choice(empty_cells)
self.grid[i][j] = random.choice([2, 4])
return True
def move_left(self):
moved = False
for i in range(4):
row = [x for x in self.grid[i] if x != 0]
row += [0] * (4 - len(row))
for j in range(3):
if row[j] == row[j+1] and row[j] != 0:
row[j] *= 2
self.score += row[j]
row[j+1] = 0
new_row = [x for x in row if x != 0]
new_row += [0] * (4 - len(new_row))
if self.grid[i] != new_row:
self.grid[i] = new_row
moved = True
return moved
def move_right(self):
self.grid = [row[::-1] for row in self.grid]
moved = self.move_left()
self.grid = [row[::-1] for row in self.grid]
return moved
def move_up(self):
self.grid = [list(row) for row in zip(*self.grid)]
moved = self.move_left()
self.grid = [list(row) for row in zip(*self.grid)]
return moved
def move_down(self):
self.grid = [list(row[::-1]) for row in self.grid]
moved = self.move_left()
self.grid = [list(row[::-1]) for row in self.grid]
return moved
def is_game_over(self):
if any(2048 in row for row in self.grid):
return True
if any(0 in row for row in self.grid):
return False
for i in range(4):
for j in range(3):
if self.grid[i][j] == self.grid[i][j+1]:
return False
if self.grid[j][i] == self.grid[j+1][i]:
return False
return True
def draw(self, surface, font):
for i in range(4):
for j in range(4):
cell_rect = pygame.Rect(j * (CELL_SIZE + CELL_PADDING), i * (CELL_SIZE + CELL_PADDING), CELL_SIZE, CELL_SIZE)
pygame.draw.rect(surface, GRAY, cell_rect)
if self.grid[i][j]:
color = WHITE if self.grid[i][j] < 8 else BLACK
text_surface = font.render(str(self.grid[i][j]), True, color)
text_rect = text_surface.get_rect()
text_rect.center = cell_rect.center
surface.blit(text_surface, text_rect)
score_surface = font.render('Score: {}'.format(self.score), True, WHITE)
score_rect = score_surface.get_rect()
score_rect.topleft = (0, GRID_SIZE)
surface.blit(score_surface, score_rect)
# 定义游戏主程序
def main():
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('2048')
font = pygame.font.Font(None, FONT_SIZE)
board = Board()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
moved = board.move_left()
elif event.key == pygame.K_RIGHT:
moved = board.move_right()
elif event.key == pygame.K_UP:
moved = board.move_up()
elif event.key == pygame.K_DOWN:
moved = board.move_down()
screen.fill(BLACK)
board.draw(screen, font)
if board.is_game_over():
game_over_surface = font.render('Game Over!', True, RED)
game_over_rect = game_over_surface.get_rect()
game_over_rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
screen.blit(game_over_surface, game_over_rect)
pygame.display.flip()
if moved:
board.add_random_number()
if __name__ == '__main__':
main()
```
这个代码使用了Pygame库来实现2048游戏的图形化界面,可以在窗口中进行游戏。