def _normalize(key, size): # Includes last index. if isinstance(key, slice): start, stop, _ = key.indices(size) if stop > start: return start, stop - 1 else: if key < 0: key += size if 0 <= key < size: return key, key raise IndexError("invalid index")
时间: 2024-01-23 08:02:04 浏览: 28
这是一个用于规范化索引的函数。它接受两个参数,key 和 size。key 可以是一个整数或切片对象,表示要访问的元素索引或范围。size 表示索引的最大值,即元素个数。
如果 key 是一个切片对象,那么函数会使用 indices() 方法将其转换为起始索引、结束索引和步长。然后,函数会检查切片对象的结束索引是否大于起始索引,如果是,则将结束索引减 1,并返回起始索引和结束索引。
如果 key 是一个整数,那么函数会检查它是否小于 0,如果是,则将其转换为正数索引。然后,函数会检查索引是否在有效范围内,如果是,则返回该索引。
如果 key 不是有效的索引或切片,则函数会抛出 IndexError 异常。
相关问题
class Entity(pygame.sprite.Sprite): def __init__(self,groups): super().__init__(groups) self.frame_index = 0 self.animation_speed = 0.15 self.direction = pygame.math.Vector2() def move(self,speed): if self.direction.magnitude() != 0: self.direction = self.direction.normalize() self.hitbox.x += self.direction.x * speed self.collision('horizontal') self.hitbox.y += self.direction.y * speed self.collision('vertical') self.rect.center = self.hitbox.center def collision(self,direction): if direction == 'horizontal': for sprite in self.obstacle_sprites: if sprite.hitbox.colliderect(self.hitbox): if self.direction.x > 0: # moving right self.hitbox.right = sprite.hitbox.left if self.direction.x < 0: # moving left self.hitbox.left = sprite.hitbox.right if direction == 'vertical': for sprite in self.obstacle_sprites: if sprite.hitbox.colliderect(self.hitbox): if self.direction.y > 0: # moving down self.hitbox.bottom = sprite.hitbox.top if self.direction.y < 0: # moving up self.hitbox.top = sprite.hitbox.bottom def wave_value(self): value = sin(pygame.time.get_ticks()) if value >= 0: return 255 else: return 0对该代码进行注释
```
# 定义实体类,继承自 pygame.sprite.Sprite
class Entity(pygame.sprite.Sprite):
# 初始化方法,传入一个 sprite 组
def __init__(self, groups):
# 调用父类的初始化方法
super().__init__(groups)
# 定义一些属性
self.frame_index = 0
self.animation_speed = 0.15
self.direction = pygame.math.Vector2()
# 移动方法,传入速度值
def move(self, speed):
# 如果方向向量的大小不为零
if self.direction.magnitude() != 0:
# 将方向向量归一化
self.direction = self.direction.normalize()
# 根据方向和速度调整 hitbox 的 x 坐标
self.hitbox.x += self.direction.x * speed
# 检测水平方向的碰撞
self.collision('horizontal')
# 根据方向和速度调整 hitbox 的 y 坐标
self.hitbox.y += self.direction.y * speed
# 检测垂直方向的碰撞
self.collision('vertical')
# 将 rect 的中心点设置为 hitbox 的中心点
self.rect.center = self.hitbox.center
# 碰撞检测方法,传入方向参数
def collision(self, direction):
# 如果是水平方向
if direction == 'horizontal':
# 遍历障碍物组中的每一个 sprite
for sprite in self.obstacle_sprites:
# 如果 hitbox 和障碍物的 hitbox 相交
if sprite.hitbox.colliderect(self.hitbox):
# 如果实体向右移动
if self.direction.x > 0:
# 将 hitbox 的右边界设置为障碍物的左边界
self.hitbox.right = sprite.hitbox.left
# 如果实体向左移动
if self.direction.x < 0:
# 将 hitbox 的左边界设置为障碍物的右边界
self.hitbox.left = sprite.hitbox.right
# 如果是垂直方向
if direction == 'vertical':
# 遍历障碍物组中的每一个 sprite
for sprite in self.obstacle_sprites:
# 如果 hitbox 和障碍物的 hitbox 相交
if sprite.hitbox.colliderect(self.hitbox):
# 如果实体向下移动
if self.direction.y > 0:
# 将 hitbox 的下边界设置为障碍物的上边界
self.hitbox.bottom = sprite.hitbox.top
# 如果实体向上移动
if self.direction.y < 0:
# 将 hitbox 的上边界设置为障碍物的下边界
self.hitbox.top = sprite.hitbox.bottom
# 计算正弦函数的值
def wave_value(self):
# 根据时间计算正弦函数的值
value = sin(pygame.time.get_ticks())
# 如果值大于等于 0,返回 255
if value >= 0:
return 255
# 否则返回 0
else:
return 0
```
def _normalize_idx(self, idx): nidx = idx if nidx < 0: nidx += len(self) if nidx < 0: nidx = 0 return nidx def __getitem__(self, idx): """Implements `x = self[idx]`""" assert(isinstance(idx, int)) nidx = self._normalize_idx(idx) if nidx >= self.length: raise IndexError else: #start at head, skip the NONE sentinel, now at index 0, while loop to walk to nidx, return data # or figure out closer to head or head.prior(tail), make the walk shorter if idx< (self.length/2): #start at head.next, walk forward idx number of nodes current=self.head.next count=0 while count<nidx: current=current.next count+=1 else: #start at head.prior, walk backward len(self.data)-idx-1 number of nodes current=self.head.prior count=self.length-1 while count>nidx: current=current.prior count-=1 return current.val 改写代码风格并保持变量名,代码意思不变
下面是改写后的代码风格:
```
def _normalize_idx(self, idx):
nidx = idx
if nidx < 0:
nidx += len(self)
if nidx < 0:
nidx = 0
return nidx
def __getitem__(self, idx):
"""Implements `x = self[idx]`"""
assert(isinstance(idx, int))
nidx = self._normalize_idx(idx)
if nidx >= self.length:
raise IndexError
else:
# Start at head, skip the NONE sentinel, now at index 0, while loop to walk to nidx, return data
# Or figure out closer to head or head.prior(tail), make the walk shorter
if idx < (self.length/2): # Start at head.next, walk forward idx number of nodes
current = self.head.next
count = 0
while count < nidx:
current = current.next
count += 1
else: # Start at head.prior, walk backward len(self.data)-idx-1 number of nodes
current = self.head.prior
count = self.length-1
while count > nidx:
current = current.prior
count -= 1
return current.val
```
注意,变量名没有改变,只是对代码进行了一些格式化和注释。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)