[英]How to check if something is within a range of multiple items in a list?
import pygame
display_width = 600
display_height = 600
snake_List = [[120.0, 180.0], [120.0, 240.0], [120.0, 300.0], [120.0, 360.0], [180.0, 360.0], [240.0, 360.0], [300.0, 360.0], [300.0, 420.0], [240.0, 420.0], [180.0, 420.0], [120.0, 420.0]]
food_placed = False
snake_block_size = 60
def Food(snake_List, food_placed, foodX, foodY):
while food_placed == False:
foodX = round(random.randrange(0,display_width))
foodY = round(random.randrange(0,display_height))
food_pos = [foodX,foodY]
if food_pos in snake_List:
food_placed = False
else:
food_placed = True
I am making a snake game and have a problem with the food-spawn because it spawns on the snake.我正在制作一个蛇游戏,但食物产卵有问题,因为它会在蛇身上产卵。 The solution I came up with doesn't work because it only will not spawn in the snake if it spawns exactly at the position of the snake being drawn.
我想出的解决方案不起作用,因为如果它恰好在被绘制的蛇的 position 处产生,它就不会在蛇中产生。 So I need it to check if it is within the 60 block range.
所以我需要它来检查它是否在 60 块范围内。
def Food(snake_List, food_placed, foodX, foodY):
foodX = round(random.randrange(0,display_width))
foodY = round(random.randrange(0,display_height))
food_pos = [foodX,foodY]
if food_pos[1] + 60 < snake_List[0::2] and food_pos[2] + 60 < snake_List[1::2]:
food_placed = False
elif food_pos[1] - 60 > snake_List[1::2] and food_pos[1] - 60 > snake_List[0::2]:
food_placed = False
else:
food_placed = True
This was my attempted solution but it says "'<' not supported between instances of 'int' and 'list'"这是我尝试的解决方案,但它说“'int'和'list'的实例之间不支持'<'”
Transform the snake to a list of pygame.Rect
objects:将蛇转换为
pygame.Rect
对象的列表:
snake_block_size = 60
snake_List = [[120.0, 180.0], [120.0, 240.0], [120.0, 300.0], [120.0, 360.0], [180.0, 360.0], [240.0, 360.0], [300.0, 360.0], [300.0, 420.0], [240.0, 420.0], [180.0, 420.0], [120.0, 420.0]]
snake_rect_list = [pygame.Rect(x, y, snake_block_size, snake_block_size) for x, y in snake_List]
Use collidelist
to check whether the foot intersects the snake.使用
collidelist
检查脚是否与蛇相交。 See collidelist
:见
collidelist
:
Test whether the rectangle collides with any in a sequence of rectangles.
测试矩形是否与矩形序列中的任何一个碰撞。 The index of the first collision found is returned.
返回找到的第一个碰撞的索引。 If no collisions are found an index of -1 is returned.
如果没有发现冲突,则返回 -1 的索引。
Fucntion Food
:功能
Food
:
def Food(snake_List):
foodX = round(random.randrange(0, display_width))
foodY = round(random.randrange(0, display_height))
fodd_size = 60
food_rect = pygame.Rect(foodX, foodY, fodd_size, fodd_size)
snake_rect_list = [pygame.Rect(x, y, snake_block_size, snake_block_size)
for x, y in snake_List]
if food_rect.collidelist(snake_rect_list) >= 0:
return None
return foodX, foodY
Note that Python has no concept of in-out parameters.请注意,Python 没有输入输出参数的概念。 Therefore you have to return the result of the function.
因此,您必须返回 function 的结果。 Use the function as follows:
使用 function 如下:
food_pos = Food(snake_List)
while food_pos == None:
food_pos = Food(snake_List)
foodX, foodY = food_pos
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.