gpt4 book ai didi

python - 多个 Sprite 碰撞检测

转载 作者:行者123 更新时间:2023-12-02 06:21:41 27 4
gpt4 key购买 nike

好吧,所以我一直在试图弄清楚如何设置它,但无法让它正常工作。我正在尝试获得类似于游戏中移动日志的功能Frogger如果你熟悉它。到目前为止,我所做的仅适用于一个 pygame.sprite.Group()

的碰撞检测
import pygame
import os
import random

os.environ['SDL_VIDEO_CENTERED'] = '1'

WIDTH = 480
HEIGHT = 720
FPS = 60

# Moving Object spacing setup first row
INSTANCE_COUNT = 0
BOX_WIDTH = 30
SPACING = 120

# object speed
pos_objspeed = 1
neg_objspeed = -1

BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((30, 30))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 96
self.speedx = 0
self.speedy = 0

class MovingObj_Test(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((90, 30))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = (INSTANCE_COUNT * (SPACING+ BOX_WIDTH))
#self.rect.x = random.randrange(WIDTH)
self.rect.bottom = 384
self.speedx = pos_objspeed

def update(self):
self.rect.x += self.speedx
if self.rect.x >= 480:
self.rect.x = -30
self.rect.bottom = 384
self.speedx = pos_objspeed

class MovingObj_TestTwo(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((90, 30))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = (INSTANCE_COUNT * (SPACING+ BOX_WIDTH))
#self.rect.x = random.randrange(WIDTH)
self.rect.bottom = 336
self.speedx = neg_objspeed

def update(self):
self.rect.x += self.speedx
if self.rect.x >= 480:
self.rect.x = -30
self.rect.bottom = 336
self.speedx = neg_objspeed

all_sprites = pygame.sprite.Group()
movingobj_l = pygame.sprite.Group()
movingobj_r = pygame.sprite.Group()

for i in range (3):
INSTANCE_COUNT = i + 1
obj1 = MovingObj_Test()
all_sprites.add(obj1)
movingobj_l.add(obj1)

for i in range (3):
INSTANCE_COUNT = i + 1
obj2 = MovingObj_TestTwo()
all_sprites.add(obj2)
movingobj_r.add(obj2)

player = Player()
all_sprites.add(player)

running = True
onLogLeft = False
onLogRight = False
groundSpd = 48
while running:

clock.tick(FPS)

hitsleft = pygame.sprite.spritecollide(player, movingobj_l, False, pygame.sprite.collide_rect)
for hit in hitsleft:
player.speedx = pos_objspeed
player.speedy = 0
onLogLeft = True
if len(hitsleft) == 0:
onLogLeft = False
player.speedx = 0
player.speedy = 0
hitsright = pygame.sprite.spritecollide(player, movingobj_r, False, pygame.sprite.collide_rect)
for hit in hitsright:
player.speedx = neg_objspeed
player.speedy = 0
onLogRight = True
if len(hitsright) == 0:
onLogRight = False
player.speedx = 0
player.speedy = 0


for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.speedx = -groundSpd
if event.key == pygame.K_RIGHT:
player.speedx = groundSpd
if event.key == pygame.K_UP:
player.speedy = -groundSpd
if event.key == pygame.K_DOWN:
player.speedy = groundSpd

if event.type == pygame.KEYUP and onLogLeft == False and onLogRight == False:
if event.key == pygame.K_LEFT:
player.speedx = 0
if event.key == pygame.K_RIGHT:
player.speedx = 0
if event.key == pygame.K_UP:
player.speedy = 0
if event.key == pygame.K_DOWN:
player.speedy = 0

if onLogLeft == False and onLogRight == False and player.rect.bottom <= 384:
running = False

player.rect.x += player.speedx
player.rect.y += player.speedy

if event.type == pygame.QUIT:
running = False



#player.rect.x += player.speedx
#player.rect.y += player.speedy

all_sprites.update()
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()



pygame.quit()
quit()

好的,所以在我的代码中

hitsright = pygame.sprite.spritecollide(player, movingobj_r, False, pygame.sprite.collide_rect)

检查碰撞并按照我预期的方式更新玩家速度,但是,

hitsleft = pygame.sprite.spritecollide(player, movingobj_l, False, pygame.sprite.collide_rect)

没有做同样的事情。

如果有人有任何想法,我很困惑为什么会这样。另外我怎样才能修复它以适用于两者?

最佳答案

左右代码的行为不同,因为如果检测到与 hitsleft 发生碰撞,则会设置 player.speedx:

for hit in hitsleft:
player.speedx = pos_objspeed

但是,如果与 hitsleft 发生冲突,则不会与 hitsright 发生冲突,并且 player.speedx 会立即重置:

if len(hitsright) == 0:
onLogRight = False
player.speedx = 0

您必须根据这两个条件设置player.speedx(例如,在if - elif - else 声明):

while running:
# [...]

hitsleft = pygame.sprite.spritecollide(player, movingobj_l, False, pygame.sprite.collide_rect)
onLogLeft = any(hitsleft)

hitsright = pygame.sprite.spritecollide(player, movingobj_r, False, pygame.sprite.collide_rect)
onLogRight = any(hitsright)

player.speedy = 0
if onLogLeft:
player.speedx = pos_objspeed
elif onLogRight:
player.speedx = neg_objspeed
else:
player.speedx = 0

关于python - 多个 Sprite 碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60387054/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com