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

Python 我不知道在pygame中触摸到特定块时如何结束游戏,python,python-3.x,pygame,Python,Python 3.x,Pygame,我希望当我的角色碰到“v”块时,游戏退出。我尝试的是找到所有“v”块,并将它们放入列表中。若玩家和“v”列表中的任何一个发生冲突,游戏将退出 我认为这应该行得通,尽管它似乎不起作用。每当我运行它时,当我触摸“v”块时,什么都不会发生 这是我的密码 import pygame from pygame.locals import * pygame.init() clock = pygame.time.Clock() WINDOW_SIZE = (600, 400) pygame.display.s

我希望当我的角色碰到“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()
导入pygame
从pygame.locals导入*
pygame.init()
clock=pygame.time.clock()
窗口大小=(600400)
pygame.display.set_标题(“游戏”)
screen=pygame.display.set_模式(窗口大小,0,32)
display=pygame.Surface((300200))
player_image=pygame.image.load(“Jacques clone-1.png(1.png”).convert()
播放器图像。设置颜色键((255255))
位置=[50,50]
#运动布尔
向右移动=错误
向左移动=错误
滚动=[0,0]
保持正确
游戏_map1=“”
-----------------------------------------------------
-----------------------------------------------------
-----------------------------------------------------
-----------------------------------------------------
-----------------------------------------------------
xx------x----------------------------------------
----------VVV---------------------------xxx----------------
---------xooo----------------------------------------
XXXXXXXX OOOOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
“.splitlines()
游戏地图=[游戏地图1中lst的列表(lst)]
tl={}
tl[“v”]=spike\u img=pygame.image.load('dirt.png'))
tl[“o”]=dirty\u img=pygame.image.load('grass.png'))
tl[“x”]=grass\u img=pygame.image.load('grass.png'))
player_rect=pygame.rect(50,50,25,25)
动量=0
空气计时器=0
#添加为移动而命中的平铺列表
def碰撞测试(矩形、平铺):
点击列表=[]
对于瓷砖中的瓷砖:
如果矩形碰撞矩形(平铺):
点击列表。追加(平铺)
#打印(点击列表)
返回命中列表
def移动(矩形、移动、平铺):
冲突类型={'top':False,'bottom':False,'right':False,'left':False}
矩形x+=移动[0]
点击列表=碰撞测试(矩形、平铺)
对于hit_列表中的磁贴:
如果移动[0]>0:
rect.right=tile.left
碰撞类型['right']=True
elif移动[0]<0:
rect.left=tile.right
碰撞类型['left']=True
矩形y+=移动[1]
点击列表=碰撞测试(矩形、平铺)
对于hit_列表中的磁贴:
如果移动[1]>0:
rect.bottom=tile.top
碰撞类型['bottom']=True
elif移动[1]<0:
rect.top=tile.bottom
碰撞类型['top']=True
返回rect,冲突类型
运行=真
运行时:
显示。填充((146244255))
滚动[0]+=(播放器x-滚动[0]-130)
平铺矩形=[]
y=0
对于游戏地图中的符号线:
x=0
对于_符号中的第_行中的符号:
如果tl中有符号:
display.blit(tl[symbol],(x*16-滚动[0],y*16-滚动[1]))
如果符号!="-":
tile_rects.append(pygame.Rect(x*16,y*16,16,16))
x+=1
y+=1
列表_of Spike=[]
y2=0
对于游戏地图中的符号行:
x2=0
对于符号行中的符号:
如果符号==“v”:
append(pygame.Rect(x2*16,y2*16,16,16))的列表
x2+=1
y2+=1
对于spike列表_中的峰值:
如果玩家正确碰撞(扣球):
pygame.quit()
玩家移动=[0,0]
如果向右移动,请执行以下操作:
玩家移动[0]+=2
如果将_向左移动:
玩家移动[0]-=2
玩家的动作[1]+=动量
动量+=0.3
如果动量>3:
动量=3
玩家移动,碰撞=移动(玩家移动,玩家移动,平铺)
如果碰撞['bottom']:
空气计时器=0
动量=0
其他:
空气计时器+=1
如果保持正确:
blit(player_-image,(player_-rect.x-scroll[0],player_-rect.y-scroll[1]))
其他:
display.blit(pygame.transform.flip(player_image,1,0),(player_rect.x-scroll[0],player_rect.y-scroll[1]))
对于pygame.event.get()中的事件:
如果event.type==退出:
运行=错误
如果event.type==KEYDOWN:
如果event.key==K_RIGHT:
向右移动=真
保持正确
如果event.key==K_LEFT:
向左移动=真
保持正确=错误
如果event.key==K_空间:
如果空气计时器<6:
动量=-5
如果event.type==KEYUP:
如果event.key==K_RIGHT:
向右移动=错误
如果event.key==K_LEFT:
向左移动=错误
screen.blit(pygame.transform.scale(显示,(窗口大小)),(0,0))
pygame.display.update()
时钟滴答(60)
pygame.quit()

您无法检测到带有尖峰的碰撞,因为您的碰撞检测工作得太完美了。
移动玩家时,确保玩家不会与任何对象相交,因此玩家也不会与钉子相交

你必须测试球员是否触到扣球。将玩家矩形在方向上增加1,并使用增加的矩形对钉子进行碰撞测试:

运行时:
# [...]
test_rect=pygame.rect(player_rect.left-1,player_rect.top-1,
玩家直宽+2,玩家直高+2)
对于spike列表_中的峰值:
如果测试正确碰撞正确(尖峰):
pygame.quit()
# [...]