【发布时间】:2019-12-27 22:42:30
【问题描述】:
我对 Pygame 比较陌生,我正在尝试为一个项目创建 Pacman 游戏。我在 Pacman 和这个块(导入的图像)之间进行碰撞检测时遇到问题。 Pacman 直接穿过了我不想发生的障碍;我想要它,这样吃豆人就不能通过它。我搜索了许多网站和许多论坛,并尝试了许多其他人使用过的不同方法,但我似乎无法理解碰撞检测。以下是我当前的代码。
def Pacman():
pygame.init()
# Creating screen
global screen
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
screen = pygame.display.set_mode((0,0), pygame.RESIZABLE)
# Setting window caption
pygame.display.set_caption('Pacman')
# Loading two images
pacman_image = pygame.image.load("pacmanphoto.png").convert_alpha()
block_image = pygame.image.load("blockphoto.png").convert_alpha()
rect1 = pacman_image.get_rect()
rect2 = block_image.get_rect()
rect1.x = 100
rect1.y = 200
rect2.x = 300
rect2.y = 400
clock = pygame.time.Clock()
x = 10
y = 10
pygame.key.set_repeat(10, 10)
# Movement
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y -= 10
if event.key == pygame.K_DOWN:
y += 10
if event.key == pygame.K_RIGHT:
x += 10
if event.key == pygame.K_LEFT:
x -= 10
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if rect1.colliderect(rect2):
y -=10
##### where collision detection should be ####
# Displaying images on screen
screen.blit(pacman_image, (x,y))
screen.blit(block_image, (50,50))
clock.tick(12)
pygame.display.update()
screen.fill(BLACK)
如果有人能以任何方式帮助我,请告诉我:) 谢谢
【问题讨论】:
标签: python-3.x pygame collision-detection