赞
踩
书接上章,本章将把上一章的消除特定元素的功能实现并且封装。
从属性
角度看,为了消除特定的游戏元素,那么我们在绘制的过程中就应该要将游戏元素进行初始化保存。
在python小游戏开心消消乐制作3中,我们知道了保存Block
元素,可以通过二维矩阵的形式来保存,即
blocks=[[0]*8 for i in range(8)]
那么我们可以为类设置一个私有变量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()
从行为
角度看,点击消除游戏元素,首先需要获得所点击的元素在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()#进行重渲染
完整代码如下:
-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()#进行重渲染
我们在主函数中调用该函数:
-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)
效果图如下:
到这里我们就实现了点击元素消除的封装啦~~
本文主要是对python小游戏开心消消乐制作6-1中点击元素消除事件消除的补充,我们将在python小游戏开心消消乐制作6
中整合6-1
和6-2
的内容。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。