基于pygame的2048小游戏实验代码
时间: 2023-09-03 09:16:36 浏览: 160
以下是基于pygame的2048小游戏实验的代码,供参考:
``` python
import pygame
import random
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 初始化pygame
pygame.init()
# 设置窗口尺寸
size = (400, 500)
screen = pygame.display.set_mode(size)
# 设置窗口标题
pygame.display.set_caption("2048")
# 字体设置
FONT = pygame.font.SysFont('Arial', 50)
# 加载音效
MOVE_SOUND = pygame.mixer.Sound('move_sound.wav')
MERGE_SOUND = pygame.mixer.Sound('merge_sound.wav')
# 加载背景图片及数字图片
BACKGROUND_IMAGE = pygame.image.load('background.png').convert()
NUMBERS_IMAGE = []
for i in range(1, 12):
NUMBERS_IMAGE.append(pygame.image.load(str(i) + '.png').convert_alpha())
# 定义游戏矩阵及状态
matrix = [[0 for i in range(4)] for j in range(4)]
state = 'playing'
# 随机生成一个新的数字
def add_new_number():
while True:
x = random.randint(0, 3)
y = random.randint(0, 3)
if matrix[x][y] == 0:
matrix[x][y] = random.choice([2, 4])
break
# 绘制游戏界面
def draw_game():
screen.blit(BACKGROUND_IMAGE, (0, 0))
for i in range(4):
for j in range(4):
if matrix[i][j] != 0:
num_index = int(pow(2, (matrix[i][j] - 1)))
screen.blit(NUMBERS_IMAGE[num_index - 1], (j * 100 + 10, i * 100 + 110))
pygame.display.update()
# 判断游戏是否胜利
def check_win():
for i in range(4):
for j in range(4):
if matrix[i][j] == 11:
return True
return False
# 判断游戏是否失败
def check_lose():
for i in range(4):
for j in range(4):
if matrix[i][j] == 0:
return False
if j < 3 and matrix[i][j] == matrix[i][j + 1]:
return False
if i < 3 and matrix[i][j] == matrix[i + 1][j]:
return False
return True
# 移动方块
def move(direction):
global state
global matrix
moved = False
if direction == 'left':
for i in range(4):
for j in range(1, 4):
if matrix[i][j] != 0:
k = j - 1
while k >= 0 and matrix[i][k] == 0:
k -= 1
if k >= 0 and matrix[i][k] == matrix[i][j]:
matrix[i][k] += 1
matrix[i][j] = 0
MERGE_SOUND.play()
moved = True
elif k + 1 != j:
matrix[i][k + 1] = matrix[i][j]
matrix[i][j] = 0
MOVE_SOUND.play()
moved = True
elif direction == 'right':
for i in range(4):
for j in range(2, -1, -1):
if matrix[i][j] != 0:
k = j + 1
while k <= 3 and matrix[i][k] == 0:
k += 1
if k <= 3 and matrix[i][k] == matrix[i][j]:
matrix[i][k] += 1
matrix[i][j] = 0
MERGE_SOUND.play()
moved = True
elif k - 1 != j:
matrix[i][k - 1] = matrix[i][j]
matrix[i][j] = 0
MOVE_SOUND.play()
moved = True
elif direction == 'up':
for j in range(4):
for i in range(1, 4):
if matrix[i][j] != 0:
k = i - 1
while k >= 0 and matrix[k][j] == 0:
k -= 1
if k >= 0 and matrix[k][j] == matrix[i][j]:
matrix[k][j] += 1
matrix[i][j] = 0
MERGE_SOUND.play()
moved = True
elif k + 1 != i:
matrix[k + 1][j] = matrix[i][j]
matrix[i][j] = 0
MOVE_SOUND.play()
moved = True
elif direction == 'down':
for j in range(4):
for i in range(2, -1, -1):
if matrix[i][j] != 0:
k = i + 1
while k <= 3 and matrix[k][j] == 0:
k += 1
if k <= 3 and matrix[k][j] == matrix[i][j]:
matrix[k][j] += 1
matrix[i][j] = 0
MERGE_SOUND.play()
moved = True
elif k - 1 != i:
matrix[k - 1][j] = matrix[i][j]
matrix[i][j] = 0
MOVE_SOUND.play()
moved = True
if moved:
add_new_number()
draw_game()
if check_win():
state = 'win'
elif check_lose():
state = 'lose'
# 初始化游戏矩阵
for i in range(2):
add_new_number()
# 游戏循环
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if state == 'playing':
if event.key == pygame.K_LEFT:
move('left')
elif event.key == pygame.K_RIGHT:
move('right')
elif event.key == pygame.K_UP:
move('up')
elif event.key == pygame.K_DOWN:
move('down')
elif state == 'win' or state == 'lose':
if event.key == pygame.K_SPACE:
matrix = [[0 for i in range(4)] for j in range(4)]
for i in range(2):
add_new_number()
state = 'playing'
# 绘制游戏界面
draw_game()
# 绘制游戏状态
if state == 'win':
text = FONT.render('YOU WIN!', True, GREEN)
screen.blit(text, (50, 20))
elif state == 'lose':
text = FONT.render('GAME OVER', True, RED)
screen.blit(text, (10, 20))
# 刷新显示
pygame.display.update()
# 设置帧率
clock.tick(60)
# 退出pygame
pygame.quit()
```
阅读全文