pygame之2048小游戏
初学python,发现看网上的python理论太过枯燥。
哎嘿~~干脆直接弄个小游戏叭,一边学习,一边写代码。
备注:
1.很多地方我尽量注解,方便大家观看。
2.完整代码在最末的代码片,复制就能用。
一、导入的库
import sys
import random
import pygame
二、全局变量
1.每个语句边上都有注解。
2.方块的颜色使用了经典2048游戏颜色。
pygame.init() # 初始化pygame()
size = 100 # 每个游戏方格的大小
queue = 4 # 初始设置为4X4阵列
game_lis = [] # 存放
background_color = (255, 239, 213) # 背景颜色
Dividingline_color = (255, 222, 173) # 分割线颜色
Dividingline_width = 15 # 分割线宽度
score_height = 120 # 得分区的高度
score_width = 140 # 得分区的宽度
score_color = (205, 193, 180) # 得分字体颜色
font1 = pygame.font.SysFont('SimHei', 50) # 得分区域字体显示黑体24
font_pos_x = Dividingline_width # 得分区域字体位置的X坐标
font_pos_y = int(font1.size('得分')[1]) # 得分区域字体位置的Y坐标
score = 0 # 得分初始值为0
font3 = pygame.font.Font(None, 50) # 数字字体
black = (0, 0, 0) # 数字颜色
screen_height = (((size + Dividingline_width) * queue) + score_height + Dividingline_width * 2) # 屏幕高度
screen_width = (((size + Dividingline_width) * queue) + Dividingline_width) # 屏幕宽度
colors = {
0: (205, 193, 180), # 各种方块对应的颜色
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, 98),
512: (237, 200, 80),
1024: (237, 197, 63),
2048: (225, 187, 0)}
三、绘制游戏背景
这部分代码片包括游戏背景颜色以及网格线。
# 绘制背景
def _draw_background(screen):
screen.fill(background_color)
Dividingline_width_half = int(Dividingline_width / 2)
Difference = score_height + Dividingline_width + int(Dividingline_width / 2)
for i in range(queue + 1): # 绘制横线
pygame.draw.line(screen, Dividingline_color,
(0, i * (size + Dividingline_width) + Difference),
(screen_height, i * (size + Dividingline_width) + Difference),
Dividingline_width)
for j in range(queue + 1): # 绘制横线
pygame.draw.line(screen, Dividingline_color,
(Dividingline_width_half + j * (size + Dividingline_width), Difference),
(Dividingline_width_half + j * (size + Dividingline_width), screen_height),
Dividingline_width)
四、绘制得分区域
1.得分区是个可变化的区域,最省力的情况是只变化分数。
2.然鹅,我在主程序中设计的是循环调用,不停的覆盖之前的结果(整块得分区域)。
有没有大佬知道,如何在不覆盖的情况下显示动态的数字。
# 绘制得分区域
def _draw_score(screen, font, pos_x, pos_y):
global score
print_text(screen, font, pos_x, 10, f'得分')
print_text(screen, font, pos_x, pos_y + 6, f'{score}')
# 得分区域
def print_text(screen, font, x, y, text):
imgText = font.render(text, True, score_color)
screen.blit(imgText, (x, y))
五、初始化游戏列表
这里是用二维阵列来存放游戏数字,初始全设置为0。
# 初始化游戏列表,存放2048数字
def _game_list():
for i in range(queue): # 初始设置全为0
lis = []
for j in range(queue):
lis.append(0)
game_lis.append(lis)
六、显示游戏方块和数字
1.显示游戏方块,方块颜色根据游戏列表里的值改变。
2.显示游戏数字。
# 显示游戏区域:游戏方块和数字
def _show_game(screen):
for i in range(queue):
for j in range(queue):
rect_color = colors[game_lis[i][j]] # 取出字典colors对应的值
rect_position = [(j + 1) * Dividingline_width + j * size, # 色块的位置
Dividingline_width * (i + 2) + size * i + score_height]
pygame.draw.rect(screen, rect_color, (rect_position, (size, size)), 0) # 绘制色块
if game_lis[i][j] != 0: # 只有数列里不为0的时候才会输出
textSurfaceObj = font3.render(str(game_lis[i][j]), True, black) # get_rect()方法返回rect对象
textRectObj = textSurfaceObj.get_rect()
rect_position = [(j + 1) * Dividingline_width + (j + 0.5) * size, # 数字的位置
Dividingline_width * (i + 2) + size * (i + 0.5) + score_height]
textRectObj.center = tuple(rect_position)
screen.blit(textSurfaceObj, textRectObj)
七、产生随机数
在随机位置产生随机数
# 在随机位置产生随机数
def _random()