当 运行 带有 pygame 的 while 循环时,NoneType 不可订阅

NoneType is not subscriptible when running a while loop with pygame

我正在编写一个函数来更新粒子沿直线的位置,当我 运行 函数本身时,我似乎没有收到任何错误,但是当我 运行 我得到的整个脚本

locationx = (location[0]-10)*np.cos(angle)
TypeError: 'NoneType' object is not subscriptable

这是简单的可重现代码:

import numpy as np 
import pygame 
pygame.init()
location = (100 , 200)
manholes = [(50 , 100) , (60 , 20) , (320 , 200) , (400 , 500), (600 , 78) , (634 , 33) ,(500 , 67)  , (634 , 33) , (700 , 67)]
n = 1
def move(manholes , location,  n ):         
    if event.type == pygame.KEYDOWN: 
        if event.key == pygame.K_LEFT:  
            x = manholes[n][0] - manholes[n-1][0]
            y = manholes[n][1] - manholes[n-1][1] 
            angle = np.arctan(y/x)
            locationx = (location[0]-10)*np.cos(angle)
            locationy = (location[1]-10)*np.sin(angle)   
            location = (locationx , locationy)
            return location    
while True : 
    for event in pygame.event.get() : 
        if event.type == pygame.QUIT :               
            pygame.quit()
            quit()  
    location = move(manholes,location, n)    
    pygame.display.update()  

location 变为 None 因为 move returns 没有 (None) 如果没有 K_LEFT-KEYDOWN事件。 不在函数move中处理事件,只在检测到事件时调用move

def move(manholes, location, n):         
    x = manholes[n][0] - manholes[n-1][0]
    y = manholes[n][1] - manholes[n-1][1] 
    angle = np.arctan(y/x)
    locationx = (location[0]-10)*np.cos(angle)
    locationy = (location[1]-10)*np.sin(angle)   
    location = (locationx , locationy)
    return location    

run = true
while run: 
    for event in pygame.event.get() : 
        if event.type == pygame.QUIT :               
            run = False
        if event.type == pygame.KEYDOWN: 
            if event.key == pygame.K_LEFT:  
                location = move(manholes, location, n)    
    
    pygame.display.update()  

pygame.quit()