【发布时间】:2020-07-31 14:30:46
【问题描述】:
对不起,因为我是非英语人士,所以有任何语法错误
在隔离期间,我正在开发一款小游戏,并想制作一个地图制作工具,以加快关卡设计过程。 我的问题是,即使网格在屏幕上显示为我期望的那样,“getcollide”功能也不能解释表面的“blit”。我花了两天时间,想不出另一种方法来解决这个问题,这是一个示例代码:
import pygame
def run(l, h, fps, scene):
pygame.init()
ecran = pygame.display.set_mode((l, h))
clock = pygame.time.Clock()
scene_active = scene
while scene_active != None:
key = pygame.key.get_pressed()
filtre_touche = []
for event in pygame.event.get():
close = False
if event.type == pygame.QUIT:
close = True
elif event.type == pygame.KEYDOWN:
alt = key[pygame.K_LALT] or touche_active[pygame.K_RALT]
if event.key == pygame.K_ESCAPE:
close = True
elif event.key == pygame.K_F4 and alt:
close = True
if close :
scene.quit()
else :
mouse_pos = pygame.mouse.get_pos()
filtre_touche.append(event)
scene_active.traite_input(filtre_touche, key, mouse_pos)
scene_active.update()
scene_active.render(ecran)
scene_active = scene_active.next
pygame.display.flip()
clock.tick(fps)
surface = pygame.Surface((320,320))
M = []
for i in range(10):
x = []
for j in range(10):
x.append(pygame.draw.rect(surface, (255,255,255),(i*32, j*32, 32,32), 1))
M.append(x)
print(M)
class FirstScreen():
def __init__(self):
self.next = self
def traite_input(self, evenement, touche, mouse_pos):
for event in evenement :
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
pygame.quit()
for line in M :
for row in line:
if row.collidepoint(mouse_pos):
print('collide')
def update (self):
pass
def render(self, ecran):
ecran.fill((200,200,200))
ecran.blit(surface, (350,250), surface.get_rect())
run(800,600,60, FirstScreen())
如果您运行此程序,您将看到表面是我们期望的位置,而图块实际上是 32 x 32。但是将鼠标悬停在网格上没有任何效果。当您将鼠标悬停在网格应该在的位置时调用 print 语句,如果我在 0,0 处对表面进行了 blit。
我错过了什么吗?在搜索了这个网站、pygame doc和各种论坛之后,我似乎是唯一一个遇到这个问题的人,我做错了什么?
【问题讨论】:
标签: python pygame grid collision blit