问题描述
我想要做的是在我放置的每个瓷砖上附加一个矩形我有瓷砖地图,允许我放置瓷砖,但我希望它也为以下每个瓷砖放置一个矩形我不知道我是怎么做的会这样做,但我试图将我的瓷砖图像放在一个类中
class block:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,width)
self.block_types = [pygame.image.load('top_left_outer.png'),pygame.image.load('top_right_outer.png'),pygame.image.load('single_pillar_top.png')]
self.block_types = [pygame.transform.scale(image,(image.get_width()//4,image.get_height()//4)) for image in self.block_types]
def draw(self):
self.rect.topleft = (self.x,self.y)
pygame.draw.rect(surface,self.color,self.rect)
然后我创建了一个包含矩形的 block1 变量我认为这会为我放置的每个图块创建 1 个块,但这是正确的,没有 VIDEO
white = (255,255,255)
block1 = block(200,200,50,white)
在我的主循环中 这就是我添加瓷砖的方式
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
print('Saving the map...')
with open('save.txt','w') as f:
f.write(repr(map))
# this part gets the key event and block type and place it on the map when I click
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x,mouse_y = pygame.mouse.get_pos()
tile_x,tile_y = mouse_x // block1.block_types[0].get_width(),mouse_y // block1.block_types[0].get_height()
if 0 <= tile_x < len(map[0]) and 0 <= tile_y < len(map):
if block01:
map[tile_y][tile_x] = 0 # Block of block_types[0]
if block2:
map[tile_y][tile_x] = 1 # Block of block_types[0]
if block3:
map[tile_y][tile_x] = 2 # Block of block_types[0]
# this part will append tiles for me
for y in range(len(map)):
for x in range(len(map[y])):
if map[y][x] != -1:
surface.blit(block1.block_types[map[y][x]],(x * block1.block_types[0].get_width(),y * block1.block_types[0].get_height()))
总而言之,我想说的是如何为我放置的每个图块附加一个矩形,因为稍后我将使用与这些矩形的碰撞 我的完整代码
import pygame,os
pygame.init()
surface = pygame.display.set_mode((800,700))
class block:
def __init__(self,self.rect)
white = (255,white)
map = [[-1 for x in range(40)] for y in range(40)]
if os.path.exists('save.txt'):
with open('save.txt') as f:
map = eval(f.read())
else:
map = [
[-1,-1,-1],[2,2,2],[1,1,1],[0,0]
]
block01 = False
block2 = False
block3 = False
fps = 60
clock = pygame.time.Clock()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
print('Saving the map...')
with open('save.txt','w') as f:
f.write(repr(map))
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x,mouse_y // block1.block_types[0].get_height()
if 0 <= tile_x < len(map[0]) and 0 <= tile_y < len(map):
if block01:
map[tile_y][tile_x] = 0 # Block of block_types[0]
if block2:
map[tile_y][tile_x] = 1 # Block of block_types[0]
if block3:
map[tile_y][tile_x] = 2 # Block of block_types[0]
#---------------------------------------------------------------
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
block01 = True
#----------------------------------------------------
surface.fill((0,0))
block1.draw()
for y in range(len(map)):
for x in range(len(map[y])):
if map[y][x] != -1:
surface.blit(block1.block_types[map[y][x]],y * block1.block_types[0].get_height()))
pygame.display.flip()
clock.tick(fps)
pygame.quit()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)