欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python小游戏代码(利用python画图的步骤)

程序员文章站 2024-03-27 17:14:04
前言:本期我们将制作一个拼图小游戏。让我们愉快地开始吧~~~先来看看效果是这样子的:开发工具**python版本:**3.6.4私信小编01即可获取大量python学习资源相关模块:pygame模块;...

前言:

本期我们将制作一个拼图小游戏。让我们愉快地开始吧~~~

先来看看效果是这样子的:

python小游戏代码(利用python画图的步骤)

开发工具

**python版本:**3.6.4

私信小编01即可获取大量python学习资源

相关模块:

pygame模块;

以及一些python自带的模块

环境搭建

安装python并添加到环境变量,pip安装需要的相关模块即可。

原理介绍

游戏简介:

将图像分为m×n个矩形块,并将图像右下角的矩形块替换为空白块后,将这些矩形块随机摆放成原图像的形状。游戏目标为通过移动非空白块将随机摆放获得的图像恢复成原图像的模样,且规定移动操作仅存在于非空白块移动到空白块。

例如下图所示:

python小游戏代码(利用python画图的步骤)

逐步实现:

step1:游戏初始界面

既然是游戏,总得有个初始界面吧?

ok,我们先写一个游戏初始界面:

'''显示游戏开始界面'''
def showstartinterface(screen, width, height):
	screen.fill(cfg.backgroundcolor)
	tfont = pygame.font.font(cfg.fontpath, width//4)
	cfont = pygame.font.font(cfg.fontpath, width//20)
	title = tfont.render('拼图游戏', true, cfg.red)
	content1 = cfont.render('按h或m或l键开始游戏', true, cfg.blue)
	content2 = cfont.render('h为5*5模式, m为4*4模式, l为3*3模式', true, cfg.blue)
	trect = title.get_rect()
	trect.midtop = (width/2, height/10)
	crect1 = content1.get_rect()
	crect1.midtop = (width/2, height/2.2)
	crect2 = content2.get_rect()
	crect2.midtop = (width/2, height/1.8)
	screen.blit(title, trect)
	screen.blit(content1, crect1)
	screen.blit(content2, crect2)
	while true:
		for event in pygame.event.get():
			if (event.type == pygame.quit) or (event.type == pygame.keydown and event.key == pygame.k_escape):
				pygame.quit()
				sys.exit()
			elif event.type == pygame.keydown:
				if event.key == ord('l'): return 3
				elif event.key == ord('m'): return 4
				elif event.key == ord('h'): return 5
		pygame.display.update()
复制代码

根据玩家自身水平,可以选择不同难度的拼图游戏。

step2:定义移动操作

定义移动操作的目的是为了移动拼图(好像是废话t_t),具体实现起来十分简单:

'''将空白cell左边的cell右移到空白cell位置'''
def mover(board, blank_cell_idx, num_cols):
	if blank_cell_idx % num_cols == 0: return blank_cell_idx
	board[blank_cell_idx-1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-1]
	return blank_cell_idx - 1


'''将空白cell右边的cell左移到空白cell位置'''
def movel(board, blank_cell_idx, num_cols):
	if (blank_cell_idx+1) % num_cols == 0: return blank_cell_idx
	board[blank_cell_idx+1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+1]
	return blank_cell_idx + 1


'''将空白cell上边的cell下移到空白cell位置'''
def moved(board, blank_cell_idx, num_cols):
	if blank_cell_idx < num_cols: return blank_cell_idx
	board[blank_cell_idx-num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-num_cols]
	return blank_cell_idx - num_cols


'''将空白cell下边的cell上移到空白cell位置'''
def moveu(board, blank_cell_idx, num_rows, num_cols):
	if blank_cell_idx >= (num_rows-1) * num_cols: return blank_cell_idx
	board[blank_cell_idx+num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+num_cols]
	return blank_cell_idx + num_cols
复制代码

step3:游戏主界面

ok,有了前面的铺垫,我们可以开始实现我们的游戏主界面了。

首先,我们需要打乱拼图, 但是随机打乱很可能导致拼图无解,因此我们通过随机移动拼图来实现打乱拼图的效果 ,这也是我们先定义拼图的移动操作的主要原因:

'''获得打乱的拼图'''
def createboard(num_rows, num_cols, num_cells):
	board = []
	for i in range(num_cells): board.append(i)
	# 去掉右下角那块
	blank_cell_idx = num_cells - 1
	board[blank_cell_idx] = -1
	for i in range(cfg.numrandom):
		# 0: left, 1: right, 2: up, 3: down
		direction = random.randint(0, 3)
		if direction == 0: blank_cell_idx = movel(board, blank_cell_idx, num_cols)
		elif direction == 1: blank_cell_idx = mover(board, blank_cell_idx, num_cols)
		elif direction == 2: blank_cell_idx = moveu(board, blank_cell_idx, num_rows, num_cols)
		elif direction == 3: blank_cell_idx = moved(board, blank_cell_idx, num_cols)
	return board, blank_cell_idx
复制代码

游戏主界面初始化:

python小游戏代码(利用python画图的步骤)

最后实现主界面的显示刷新以及事件响应等功能:

while true:
		game_board, blank_cell_idx = createboard(num_rows, num_cols, num_cells)
		if not isgameover(game_board, size):
			break
	# 游戏主循环
	is_running = true
	while is_running:
		# --事件捕获
		for event in pygame.event.get():
			# ----退出游戏
			if (event.type == pygame.quit) or (event.type == pygame.keydown and event.key == pygame.k_escape):
				pygame.quit()
				sys.exit()
			# ----键盘操作
			elif event.type == pygame.keydown:
				if event.key == pygame.k_left or event.key == ord('a'):
					blank_cell_idx = movel(game_board, blank_cell_idx, num_cols)
				elif event.key == pygame.k_right or event.key == ord('d'):
					blank_cell_idx = mover(game_board, blank_cell_idx, num_cols)
				elif event.key == pygame.k_up or event.key == ord('w'):
					blank_cell_idx = moveu(game_board, blank_cell_idx, num_rows, num_cols)
				elif event.key == pygame.k_down or event.key == ord('s'):
					blank_cell_idx = moved(game_board, blank_cell_idx, num_cols)
			# ----鼠标操作
			elif event.type == pygame.mousebuttondown and event.button == 1:
				x, y = pygame.mouse.get_pos()
				x_pos = x // cell_width
				y_pos = y // cell_height
				idx = x_pos + y_pos * num_cols
				if idx == blank_cell_idx-1:
					blank_cell_idx = mover(game_board, blank_cell_idx, num_cols)
				elif idx == blank_cell_idx+1:
					blank_cell_idx = movel(game_board, blank_cell_idx, num_cols)
				elif idx == blank_cell_idx+num_cols:
					blank_cell_idx = moveu(game_board, blank_cell_idx, num_rows, num_cols)
				elif idx == blank_cell_idx-num_cols:
					blank_cell_idx = moved(game_board, blank_cell_idx, num_cols)
		# --判断游戏是否结束
		if isgameover(game_board, size):
			game_board[blank_cell_idx] = num_cells - 1
			is_running = false
		# --更新屏幕
		screen.fill(cfg.backgroundcolor)
		for i in range(num_cells):
			if game_board[i] == -1:
				continue
			x_pos = i // num_cols
			y_pos = i % num_cols
			rect = pygame.rect(y_pos*cell_width, x_pos*cell_height, cell_width, cell_height)
			img_area = pygame.rect((game_board[i]%num_cols)*cell_width, (game_board[i]//num_cols)*cell_height, cell_width, cell_height)
			screen.blit(game_img_used, rect, img_area)
		for i in range(num_cols+1):
			pygame.draw.line(screen, cfg.black, (i*cell_width, 0), (i*cell_width, game_img_used_rect.height))
		for i in range(num_rows+1):
			pygame.draw.line(screen, cfg.black, (0, i*cell_height), (game_img_used_rect.width, i*cell_height))
		pygame.display.update()
		clock.tick(cfg.fps)
复制代码

step4:游戏结束界面

当玩家完成拼图后,需要显示游戏结束界面,和游戏初始界面类似,实现起来都比较简单:

'''显示游戏结束界面'''
def showendinterface(screen, width, height):
	screen.fill(cfg.backgroundcolor)
	font = pygame.font.font(cfg.fontpath, width//15)
	title = font.render('恭喜! 你成功完成了拼图!', true, (233, 150, 122))
	rect = title.get_rect()
	rect.midtop = (width/2, height/2.5)
	screen.blit(title, rect)
	pygame.display.update()
	while true:
		for event in pygame.event.get():
			if (event.type == pygame.quit) or (event.type == pygame.keydown and event.key == pygame.k_escape):
				pygame.quit()
				sys.exit()
		pygame.display.update()
复制代码

文章到这里就结束了,感谢你的观看