我正在做我的第一个pygame项目,似乎无法让碰撞检测正常工作, 这就是我目前的情况,我不太确定问题出在哪里
import random
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((288, 512))
clock = pygame.time.Clock()
game_font = pygame.font.Font('freesansbold.ttf', 40)
def draw_bg():
screen.blit(bg_surface, (0, bg_y_pos))
screen.blit(bg_surface, (0, bg_y_pos + 512))
def create_hazzard():
random_hazzard_pos = random.choice(hazzard_length)
left_hazzard = hazzard_surface.get_rect(midright=(random_hazzard_pos, 520))
right_hazzard = hazzard_surface.get_rect(midleft=(random_hazzard_pos + 75, 520))
return right_hazzard, left_hazzard
def move_hazzards(hazzards):
for hazzard in hazzards:
hazzard.centery -= 2
visible_hazzard = [hazzard for hazzard in hazzards if hazzard.top > -50]
return visible_hazzard
def draw_hazzards(hazzards):
for hazzard in hazzards:
if hazzard.left >= 288:
screen.blit(hazzard_surface, hazzard)
else:
flip_hazzard = pygame.transform.flip(hazzard_surface, True, False)
screen.blit(flip_hazzard, hazzard)
def check_collision(hazzards):
for hazzard in hazzards:
if player_rect.colliderect(hazzard):
death_sound.play()
print('collision')
return False
else:
return True
bg_y_pos = 0
player_x_pos = 144
player_y_pos = 256
player_x_change = 0
character_movement = 0
game_active = True
score = 0
player_surface = pygame.image.load('player.png').convert_alpha()
player_surface = pygame.transform.scale2x(player_surface)
player_rect = player_surface.get_rect(center=(player_x_pos, player_y_pos))
bg_surface = pygame.image.load('temp.png').convert()
hazzard_surface = pygame.image.load('temp_hazard.png')
death_sound = pygame.mixer.Sound('temp_hit.mp3')
hazzard_list = []
SPAWNHAZARD = pygame.USEREVENT
pygame.time.set_timer(SPAWNHAZARD, 1200)
hazzard_length = [200, 50, 100]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x_change -= 2
if event.key == pygame.K_RIGHT:
player_x_change += 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or pygame.K_RIGHT:
player_x_change = 0
if event.type == SPAWNHAZARD:
hazzard_list.extend(create_hazzard())
player_x_pos += player_x_change
# scolling background
bg_y_pos -= 2
if bg_y_pos <= -512:
bg_y_pos = 0
if game_active:
draw_bg()
hazzard_list = move_hazzards(hazzard_list)
draw_hazzards(hazzard_list)
player_rect.centerx += player_x_change
screen.blit(player_surface, player_rect)
check_collision(hazzard_list)
pygame.display.update()
clock.tick(120)
只有右哈扎德检测到碰撞,左哈扎德检测不到。如果我先返回left_hazzard,然后返回right_hazzard,那么left_hazzard可以检测碰撞,但不能检测right_hazzard
这里的问题是
check_collision
函数的逻辑,您可能已经弄明白了。具体来说,问题在于函数在完全到达循环末尾之前返回结果我猜您正在寻找的逻辑是,如果“left_hazard”或“right_hazard”有问题,则返回false,但将其扩展为任意长的列表。因为它实际上是一个很长的“或”语句。当检测到碰撞时,您仍然可以返回“False”,但是在循环完成之前不要返回true
所以代码看起来更像这样:
注意:我假设函数
player_rect.colliderect
和create_hazzard
的输出没有问题,因为我不确定前者是如何实现的。因此,尽管上面的问题看起来确实是一个有效的bug,但如果它不能解决您的问题,我会更仔细地查看player_rect.colliderect
函数,并检查是否向其传递了正确的输入相关问题 更多 >
编程相关推荐