当前位置:   article > 正文

python小游戏开心消消乐制作6-2

python小游戏开心消消乐制作6-2


前言

书接上章,本章将把上一章的消除特定元素的功能实现并且封装。


一、创建游戏元素矩阵类

2.点击消除特定游戏元素

属性角度看,为了消除特定的游戏元素,那么我们在绘制的过程中就应该要将游戏元素进行初始化保存。
python小游戏开心消消乐制作3中,我们知道了保存Block元素,可以通过二维矩阵的形式来保存,即

blocks=[[0]*8 for i in range(8)]
  • 1

那么我们可以为类设置一个私有变量blocks用于存储每个游戏元素。并且在绘制游戏元素时存储该游戏元素矩阵。

class GameBlock:
	def __init__(self,screen,start_left,start_top,block_height,block_width,row,col,type):
		self.row = row
		self.col = col
		self.screen = screen
		self.start_left = start_left
		self.start_top = start_top
		self.block_width = block_width
		self.block_height = block_height
		self.type = type
		self.blocks=[[0]*self.col for i in range(self.row)]
	def initMagicBlock(self):
		for i in range(0,self.row,1):
			for j in range(0,self.col,1):
				if self.type == TYPE_IMAGE:
					self.blocks[i][j] = Block(self.screen,self.start_left+self.block_width*j,self.start_top+self.block_height*i,self.block_width,self.block_height,self.type,image=pygame.image.load('./image/0'+str(random.randint(1,5))+"_hightlight.png"))
					self.blocks[i][j].draw()
				elif self.type == TYPE_RECT:
					self.blocks[i][j] = Block(self.screen,self.start_left+self.block_width*j,self.start_top+self.block_height*i,self.block_width,self.block_height,self.type,color=(random.randint(0,255),random.randint(0,255),random.randint(0,255)))
					self.blocks[i][j].draw()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

行为角度看,点击消除游戏元素,首先需要获得所点击的元素在blocks中的坐标,在python小游戏开心消消乐制作4中我们通过获得了鼠标点击的left,top(x,y)坐标,并且我们根据公式j=(left-游戏元素起始left位置)//游戏元素宽度i=(top-游戏元素起始top位置)//游戏元素长度(//代表整除),获得所点击元素在blocks中的坐标blocks[i][j]
获得了具体的游戏元素之后,我们希望点击之后可以使得该游戏元素消失,也就是不再显示该游戏元素,在这里我们将游戏元素直接变为白色矩形元素,代表该游戏元素消失。故而我们可以在GameBlock中定义一个新函数mouseClick用于消除元素。

def mouseClick(self,x,y):
		i = (y-self.start_top)//self.block_height
		j = (x-self.start_left)//self.block_width
		self.blocks[i][j].type = TYPE_RECT
		self.blocks[i][j].color = (255,255,255)
		self.initMagicBlock()#进行重渲染
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

完整代码如下:

-MagicBlock.py
import pygame
import random

TYPE_RECT = 0
TYPE_IMAGE = 1

class Block:
	
	def __init__(self,screen,left,top,width,height,type,image=None,color=None):
		self.screen = screen
		self.left = left
		self.top = top
		self.type = type
		self.image = image
		self.color = color
		self.width = width
		self.height = height
		
	def draw(self):
		
		if self.type == TYPE_RECT:
			position = self.left,self.top,self.width,self.height
			pygame.draw.rect(self.screen,self.color,position)
		elif self.type == TYPE_IMAGE:
			self.screen.blit(self.image,(self.left,self.top))

class GameBlock:
	def __init__(self,screen,start_left,start_top,block_height,block_width,row,col,type):
		self.row = row
		self.col = col
		self.screen = screen
		self.start_left = start_left
		self.start_top = start_top
		self.block_width = block_width
		self.block_height = block_height
		self.type = type
		self.blocks=[[0]*self.col for i in range(self.row)]
	def initMagicBlock(self):
		for i in range(0,self.row,1):
			for j in range(0,self.col,1):
				if self.type == TYPE_IMAGE:
					self.blocks[i][j] = Block(self.screen,self.start_left+self.block_width*j,self.start_top+self.block_height*i,self.block_width,self.block_height,self.type,image=pygame.image.load('./image/0'+str(random.randint(1,5))+"_hightlight.png"))
					self.blocks[i][j].draw()
				elif self.type == TYPE_RECT:
					self.blocks[i][j] = Block(self.screen,self.start_left+self.block_width*j,self.start_top+self.block_height*i,self.block_width,self.block_height,self.type,color=(random.randint(0,255),random.randint(0,255),random.randint(0,255)))
					self.blocks[i][j].draw()
	def mouseClick(self,x,y):
		i = (y-self.start_top)//self.block_height
		j = (x-self.start_left)//self.block_width
		self.blocks[i][j].type = TYPE_RECT
		self.blocks[i][j].color = (255,255,255)
		self.blocks[i][j].draw()#进行重渲染
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

我们在主函数中调用该函数:

-main.py
import pygame
import sys
import time
import random
from MagicBlock import Block,GameBlock
import MagicBlock

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_HEIGHT,SCREEN_WIDTH))
    pygame.display.set_caption("happy remove")
    screen.fill((124,114,242))
        	
    gameblock = GameBlock(screen,start_left=20,start_top=20,block_height=50,block_width=50,row=8,col=8,type=MagicBlock.TYPE_IMAGE)
    gameblock.initMagicBlock()
    #更新窗口
    pygame.display.update()
    while True:
        
        #获取鼠标响应
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit(0)
            elif event.type ==pygame.MOUSEBUTTONDOWN:
                x,y = event.pos
                gameblock.mouseClick(x,y)
        pygame.display.update()
        time.sleep(0.3)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

效果图如下:
在这里插入图片描述
到这里我们就实现了点击元素消除的封装啦~~


总结

本文主要是对python小游戏开心消消乐制作6-1中点击元素消除事件消除的补充,我们将在python小游戏开心消消乐制作6中整合6-16-2的内容。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/679539
推荐阅读
相关标签
  

闽ICP备14008679号