初学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():
random_num = random.randint(1, 2) # 随机数
num = pow(2, random_num)
random_pos_x = random.randint(0, queue - 1) # 随机X坐标
random_pos_y = random.randint(0, queue - 1) # 随机Y坐标
if game_lis[random_pos_x][random_pos_y] == 0:
game_lis[random_pos_x][random_pos_y] = num
else:
_random()
左方向键控制。
# 捕获键盘LEFT操作
def _LEFT():
global score
for i in range(queue):
while 0 in game_lis[i]:
game_lis[i].remove(0)
for j in range(len(game_lis[i]) - 1):
if game_lis[i][j] == game_lis[i][j + 1]:
game_lis[i][j] = game_lis[i][j] + game_lis[i][j + 1]
score = score + game_lis[i][j]
game_lis[i][j + 1] = 0
while 0 in game_lis[i]:
game_lis[i].remove(0)
lis = []
for j in range(queue - len(game_lis[i])):
lis.append(0)
game_lis[i] = game_lis[i] + lis
右方向键控制。
# 捕获键盘RIGHT操作
def _RIGHT():
global score
for i in range(queue):
while 0 in game_lis[i]:
game_lis[i].remove(0)
for j in range(len(game_lis[i]) - 1, 0, -1):
if game_lis[i][j] == game_lis[i][j - 1]:
game_lis[i][j] = game_lis[i][j] + game_lis[i][j - 1]
score = score + game_lis[i][j]
game_lis[i][j - 1] = 0
while 0 in game_lis[i]:
game_lis[i].remove(0)
lis = []
for j in range(queue - len(game_lis[i])):
lis.append(0)
game_lis[i] = lis + game_lis[i]
上方向键控制,使用了列表转置。
def _UP():
global score
lis = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
for i in range(queue): # 整个列表顺时针旋转90度
for j in range(queue):
lis[i][j] = game_lis[queue - 1 - j][i]
for i in range(queue):
while 0 in lis[i]:
lis[i].remove(0)
for j in range(len(lis[i]) - 1, 0, -1):
if lis[i][j] == lis[i][j - 1]:
lis[i][j] = lis[i][j] + lis[i][j - 1]
score = score + lis[i][j]
lis[i][j - 1] = 0
while 0 in lis[i]:
lis[i].remove(0)
list1 = []
for j in range(queue - len(lis[i])):
list1.append(0)
lis[i] = list1 + lis[i]
for i in range(queue): # 整个列表逆时针旋转90度
for j in range(queue):
game_lis[i][j] = lis[j][queue - 1 - i]
下方向键控制,使用了列表转置。
def _DOWN():
global score
lis = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
for i in range(queue): # 整个列表顺时针旋转90度
for j in range(queue):
lis[i][j] = game_lis[queue - 1 - j][i]
for i in range(queue):
while 0 in lis[i]:
lis[i].remove(0)
for j in range(len(lis[i]) - 1):
if lis[i][j] == lis[i][j + 1]:
lis[i][j] = lis[i][j] + lis[i][j + 1]
score = score + lis[i][j]
lis[i][j + 1] = 0
while 0 in lis[i]:
lis[i].remove(0)
list1 = []
for j in range(queue - len(lis[i])):
list1.append(0)
lis[i] = lis[i] + list1
for i in range(queue): # 整个列表逆时针旋转90度
for j in range(queue):
game_lis[i][j] = lis[j][queue - 1 - i]
def main():
screen = pygame.display.set_mode((screen_width, screen_height)) # 建立游戏窗口
pygame.display.set_caption('2048') # 设置窗口标题
_draw_background(screen) # 绘制背景
_game_list() # 初始化游戏列表
_show_game(screen) # 显示游戏方块和数字
_draw_score(screen, font1, font_pos_x, font_pos_y) # 绘制得分区
# 开始游戏
_random() # 产生随机数
pygame.display.flip() # 更新游戏
_show_game(screen) # 显示游戏方块和数字
_draw_score(screen, font1, font_pos_x, font_pos_y) # 绘制得分区
while True:
for event in pygame.event.get(): # get()获取事件的返回值
if event.type == pygame.QUIT: # 判断事件是否是退出事件,是则退出
pygame.quit() # 先退出pygame窗口
sys.exit() # 再退出pygame程序
elif event.type == pygame.KEYDOWN: # 捕获按键操作
if event.key == pygame.K_LEFT: # 按下左按键
_LEFT()
print("left")
elif event.key == pygame.K_RIGHT: # 按下右按键
_RIGHT()
print("right")
elif event.key == pygame.K_UP: # 按下上按键
_UP()
print("up")
elif event.key == pygame.K_DOWN: # 按下下按键
_DOWN()
print("down")
else:
print("False")
pygame.display.flip() # 更新游戏
_draw_background(screen) # 绘制背景
_random() # 产生随机数
_show_game(screen) # 显示游戏方块和数字
_draw_score(screen, font1, font_pos_x, font_pos_y) # 绘制得分区
直接复制就能使用。
import sys
import random
import pygame
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)
# 绘制得分区域
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))
# 初始化游戏列表,存放2048数字
def _game_list():
for i in range(queue): # 初始设置全为0
lis = []
for j in range(queue):
lis.append(0)
game_lis.append(lis)
# 显示游戏区域:游戏方块和数字
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():
random_num = random.randint(1, 2) # 随机数
num = pow(2, random_num)
random_pos_x = random.randint(0, queue - 1) # 随机X坐标
random_pos_y = random.randint(0, queue - 1) # 随机Y坐标
if game_lis[random_pos_x][random_pos_y] == 0:
game_lis[random_pos_x][random_pos_y] = num
else:
_random()
# 捕获键盘LEFT操作
def _LEFT():
global score
for i in range(queue):
while 0 in game_lis[i]:
game_lis[i].remove(0)
for j in range(len(game_lis[i]) - 1):
if game_lis[i][j] == game_lis[i][j + 1]:
game_lis[i][j] = game_lis[i][j] + game_lis[i][j + 1]
score = score + game_lis[i][j]
game_lis[i][j + 1] = 0
while 0 in game_lis[i]:
game_lis[i].remove(0)
lis = []
for j in range(queue - len(game_lis[i])):
lis.append(0)
game_lis[i] = game_lis[i] + lis
# 捕获键盘RIGHT操作
def _RIGHT():
global score
for i in range(queue):
while 0 in game_lis[i]:
game_lis[i].remove(0)
for j in range(len(game_lis[i]) - 1, 0, -1):
if game_lis[i][j] == game_lis[i][j - 1]:
game_lis[i][j] = game_lis[i][j] + game_lis[i][j - 1]
score = score + game_lis[i][j]
game_lis[i][j - 1] = 0
while 0 in game_lis[i]:
game_lis[i].remove(0)
lis = []
for j in range(queue - len(game_lis[i])):
lis.append(0)
game_lis[i] = lis + game_lis[i]
# 获取键盘UP操作
def _UP():
global score
lis = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
for i in range(queue): # 整个列表顺时针旋转90度
for j in range(queue):
lis[i][j] = game_lis[queue - 1 - j][i]
for i in range(queue):
while 0 in lis[i]:
lis[i].remove(0)
for j in range(len(lis[i]) - 1, 0, -1):
if lis[i][j] == lis[i][j - 1]:
lis[i][j] = lis[i][j] + lis[i][j - 1]
score = score + lis[i][j]
lis[i][j - 1] = 0
while 0 in lis[i]:
lis[i].remove(0)
list1 = []
for j in range(queue - len(lis[i])):
list1.append(0)
lis[i] = list1 + lis[i]
for i in range(queue): # 整个列表逆时针旋转90度
for j in range(queue):
game_lis[i][j] = lis[j][queue - 1 - i]
# 获取键盘DOWN操作
def _DOWN():
global score
lis = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
for i in range(queue): # 整个列表顺时针旋转90度
for j in range(queue):
lis[i][j] = game_lis[queue - 1 - j][i]
for i in range(queue):
while 0 in lis[i]:
lis[i].remove(0)
for j in range(len(lis[i]) - 1):
if lis[i][j] == lis[i][j + 1]:
lis[i][j] = lis[i][j] + lis[i][j + 1]
score = score + lis[i][j]
lis[i][j + 1] = 0
while 0 in lis[i]:
lis[i].remove(0)
list1 = []
for j in range(queue - len(lis[i])):
list1.append(0)
lis[i] = lis[i] + list1
for i in range(queue): # 整个列表逆时针旋转90度
for j in range(queue):
game_lis[i][j] = lis[j][queue - 1 - i]
# 主函数
def main():
screen = pygame.display.set_mode((screen_width, screen_height)) # 建立游戏窗口
pygame.display.set_caption('2048') # 设置窗口标题
_draw_background(screen) # 绘制背景
_game_list() # 初始化游戏列表
_show_game(screen) # 显示游戏方块和数字
_draw_score(screen, font1, font_pos_x, font_pos_y) # 绘制得分区
# 开始游戏
_random() # 产生随机数
pygame.display.flip() # 更新游戏
_show_game(screen) # 显示游戏方块和数字
_draw_score(screen, font1, font_pos_x, font_pos_y) # 绘制得分区
while True:
for event in pygame.event.get(): # get()获取事件的返回值
if event.type == pygame.QUIT: # 判断事件是否是退出事件,是则退出
pygame.quit() # 先退出pygame窗口
sys.exit() # 再退出pygame程序
elif event.type == pygame.KEYDOWN: # 捕获按键操作
if event.key == pygame.K_LEFT: # 按下左按键
_LEFT()
print("left")
elif event.key == pygame.K_RIGHT: # 按下右按键
_RIGHT()
print("right")
elif event.key == pygame.K_UP: # 按下上按键
_UP()
print("up")
elif event.key == pygame.K_DOWN: # 按下下按键
_DOWN()
print("down")
else:
print("False")
pygame.display.flip() # 更新游戏
_draw_background(screen) # 绘制背景
_random() # 产生随机数
_show_game(screen) # 显示游戏方块和数字
_draw_score(screen, font1, font_pos_x, font_pos_y) # 绘制得分区
if __name__ == '__main__':
main()