我不知道如何在pygame中触摸特定块时结束游戏

我想做到这样,当我的角色碰到“ v”块时,游戏退出。我试图找到所有为“ v”的块并将其放入列表中。如果玩家与“ v”列表中的任何一个碰撞,游戏将退出。

我认为这应该有效,尽管似乎无效。每当我触摸“ v”块时运行它,都不会发生任何事情。

这是我的代码

import pygame
from pygame.locals import *

pygame.init()
clock = pygame.time.Clock()
WINDOW_SIZE = (600, 400)
pygame.display.set_caption("Game")
screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32)
display = pygame.Surface((300, 200))

player_image = pygame.image.load("Jacques clone-1.png (1).png").convert()
player_image.set_colorkey((255,255,255))

location = [50, 50]

#boolean for movement
moving_right = False
moving_left = False

scroll = [0, 0]

Stay_right = True

game_map1 = """
-----------------------------------------------------
-----------------------------------------------------
-----------------------------------------------------
-----------------------------------------------------
-----------------------------------------------------
xx----------x----------------------------------------
----------vvvv---------------------xxx----------------
---------xooo----------------------------------------
xxxxxxxxxooooxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooooooooo
""".splitlines()

game_map = [list(lst) for lst in game_map1]

tl = {}
tl["v"] = spike_img = pygame.image.load('dirt.png')
tl["o"] = dirt_img = pygame.image.load('grass.png')
tl["x"] = grass_img = pygame.image.load('grass.png')

player_rect = pygame.Rect(50, 50, 25, 25)
momentum = 0
air_timer = 0

#adding tiles list that are hit for movement
def collision_test(rect, tiles):
    hit_list = []
    for tile in tiles:
        if rect.colliderect(tile):
            hit_list.append(tile)
            #print(hit_list)
    return hit_list

def move(rect, movement, tiles):
    collision_types = {'top': False, 'bottom': False, 'right': False, 'left': False}
    rect.x += movement[0]
    hit_list = collision_test(rect, tiles)
    for tile in hit_list:
        if movement[0] > 0:
            rect.right = tile.left
            collision_types['right'] = True
        elif movement[0] < 0:
            rect.left = tile.right
            collision_types['left'] = True
    rect.y += movement[1]
    hit_list = collision_test(rect, tiles)
    for tile in hit_list:
        if movement[1] > 0:
            rect.bottom = tile.top
            collision_types['bottom'] = True
        elif movement[1] < 0:
            rect.top = tile.bottom
            collision_types['top'] = True
    return rect, collision_types

run = True
while run:
    display.fill((146, 244, 255))

    scroll[0] += (player_rect.x - scroll[0] - 130)


    tile_rects = []
    y = 0
    for line_of_symbols in game_map:
        x = 0
        for symbol in line_of_symbols:
            if symbol in tl:
                display.blit(tl[symbol], (x * 16 - scroll[0], y * 16 - scroll[1]))
            if symbol != "-":
                tile_rects.append(pygame.Rect(x * 16, y * 16, 16, 16))
            x += 1
        y += 1

    list_ofspike = []
    y2 = 0
    for lineofsymbols in game_map:
        x2 = 0
        for symbols in lineofsymbols:
            if symbols == "v":
                list_ofspike.append(pygame.Rect(x2 * 16, y2 * 16, 16, 16))
            x2 += 1
        y2 += 1

    for spike in list_ofspike:
        if player_rect.colliderect(spike):
            pygame.quit()

    player_movement = [0, 0]
    if moving_right:
        player_movement[0] += 2
    if moving_left:
        player_movement[0] -= 2
    player_movement[1] += momentum
    momentum += 0.3
    if momentum > 3:
        momentum = 3

    player_rect, collisions = move(player_rect, player_movement, tile_rects)

    if collisions['bottom']:
        air_timer = 0
        momentum = 0
    else:
        air_timer += 1

    if Stay_right:
        display.blit(player_image, (player_rect.x - scroll[0], player_rect.y - scroll[1]))
    else:
        display.blit(pygame.transform.flip(player_image, 1, 0 ),(player_rect.x - scroll[0], player_rect.y - scroll[1]))

    for event in pygame.event.get():
        if event.type == QUIT:
            run = False
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                moving_right = True
                Stay_right = True
            if event.key == K_LEFT:
                moving_left = True
                Stay_right = False
            if event.key == K_SPACE:
                if air_timer < 6:
                    momentum = -5
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                moving_right = False
            if event.key == K_LEFT:
                moving_left = False

    screen.blit(pygame.transform.scale(display, (WINDOW_SIZE)), (0, 0))
    pygame.display.update()
    clock.tick(60)

pygame.quit()
评论
  • cnon
    cnon 回复

    您不会检测到尖峰的碰撞,因为您的碰撞检测效果很好。 移动播放器时,请确保播放器不与任何对象相交,因此播放器也不会与尖峰相交。

    您必须测试播放器是否碰到峰值。将播放器矩形的方向增加1,并使用增加的矩形对尖峰进行碰撞测试:

    while run:
        # [...]
    
         test_rect = pygame.Rect(player_rect.left-1, player_rect.top-1,
                                 player_rect.width+2, player_rect.height+2)
        for spike in list_ofspike:
            if test_rect.colliderect(spike):
                pygame.quit() 
    
        # [...]