对象之间的Python Pygame冲突检测



嗨,很抱歉有这么多问题,但我知道python,但正在努力学习pygame。在我制作的游戏中,我试图做到当一个块/图像触摸到精灵时,它会回到屏幕顶部。我看了很多教程,但似乎无法以一种好的方式来理解它。有什么建议吗?这是代码,谢谢!!:

import pygame
import sys
from random import randint
import os
x = 250
y = 30
os.environ["SDL_VIDEO_WINDOW_POS"] = "%d,%d" % (x, y)
width = 1024
height = 768
icon1 = pygame.image.load("Firstpygamegame/santa-claus.png")
pygame.display.set_icon(icon1)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Gift Catcher")
background_image = pygame.image.load("Firstpygamegame/wintervillage.png")
sprite1 = pygame.image.load("Firstpygamegame/santabag2.png")
spriterect = sprite1.get_rect()
speed = 2.5
# 442 or 467
spriterect.x = 442
icon2 = pygame.image.load("Firstpygamegame/present1.png")
icon3 = pygame.image.load("Firstpygamegame/present2.png")
icon4 = pygame.image.load("Firstpygamegame/present3.png")
icon5 = pygame.image.load("Firstpygamegame/present4.png")
cubes = [[
randint(1, 1000),  # X coordinate
randint(-1500, -350)]  # Y coordinate, -Y is above screen  (top of screen is zero)
for x in range(2)]  # 20 cubes
cubes1 = [[
randint(1, 1000),  # X coordinate
randint(-1500, -150)]  # Y coordinate, -Y is above screen  (top of screen is zero)
for x in range(2)]  # 20 cubes
cubes2 = [[
randint(1, 1000),  # X coordinate
randint(-1500, -550)]  # Y coordinate, -Y is above screen  (top of screen is zero)
for x in range(2)]  # 20 cubes
cubes3 = [[
randint(1, 1000),  # X coordinate
randint(-1500, -450)]  # Y coordinate, -Y is above screen  (top of screen is zero)
for x in range(2)]  # 20 cubes
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
spriterect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
spriterect.y = 600
screen.blit(background_image, (0, 0))
screen.blit(sprite1, spriterect)
for cb in cubes:
cb[1] += .25  # cube moves down 2 pixels
screen.blit(icon2, cb)  # draw cube
if cb[1] > 800:  # if cube passed bottom of screen
cb[1] = -100  # move to above screen
cb[0] = randint(1, 1000)
for cb in cubes1:
cb[1] += .25  # cube moves down 2 pixels
screen.blit(icon3, cb)  # draw cube
if cb[1] > 800:  # if cube passed bottom of screen
cb[1] = -100  # move to above screen
cb[0] = randint(1, 1000)  # random X position
for cb in cubes2:
cb[1] += .25  # cube moves down 2 pixels
screen.blit(icon4, cb)  # draw cube
if cb[1] > 800:  # if cube passed bottom of screen
cb[1] = -100  # move to above screen
cb[0] = randint(1, 1000)  # random X position
for cb in cubes3:
cb[1] += .25  # cube moves down 2 pixels
screen.blit(icon5, cb)  # draw cube
if cb[1] > 800:  # if cube passed bottom of screen
cb[1] = -100  # move to above screen
cb[0] = randint(1, 1000)  # random X position
pygame.display.flip()

使用pygame.Rect对象和colliderect()来查找矩形和对象之间的冲突
pygame.Surface.get_rect.get_rect()返回一个与Surface对象大小相同的矩形,该矩形始终从(0,0(开始,因为Surfaceobject没有位置。矩形的位置可以通过关键字参数指定:

while running:
# [...]
for cb in cubes:
cb[1] += .25  # cube moves down 2 pixels
screen.blit(icon2, cb)  # draw cube
icon_rect = icon2.get_rect(topleft = cb)
if cb[1] > 800 or icon_rect.colliderect(spriterect):  
cb[1] = -100  # move to above screen
cb[0] = randint(1, 1000)
# [...]

然而,您可以简化您的代码:

icon_list = [icon2, icon3, icon4, icon5]
cube_lists = [[[
randrange(screen.get_width() - icon.get_width()),
randint(-1500, -350)] 
for x in range(2)]
for icon in icon_list]
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
spriterect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
spriterect.y = 600
screen.blit(background_image, (0, 0))
screen.blit(sprite1, spriterect)
for icon, cubes in zip(icon_list, cube_lists):
for cb in cubes:
cb[1] += .25  # cube moves down 2 pixels
screen.blit(icon, cb)  # draw cube
icon_rect = icon.get_rect(topleft = cb)
if cb[1] > screen.get_height() or icon_rect.colliderect(spriterect):  
cb[:] = randrange(screen.get_width() - icon.get_width()), -800
pygame.display.flip()
pygame.quit()
sys.exit()

请注意,如果runningFalse,则应用程序循环终止。在事件循环中设置running = False和调用pygame.quit()sys.exit()没有意义。让循环运行到最后,并在事件循环之后调用pygame.quit()sys.exit()

最新更新