From bf098b07c5eb0cfeed21bbbe99c140b15fec4e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=BF=8A=E7=9A=93?= <14243992+zhao-junhao1229@user.noreply.gitee.com> Date: Sat, 20 Jul 2024 10:50:43 +0000 Subject: [PATCH 1/3] update main.py. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 赵俊皓 <14243992+zhao-junhao1229@user.noreply.gitee.com> --- main.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 724fc84..67b6739 100644 --- a/main.py +++ b/main.py @@ -82,8 +82,12 @@ class Food: # 不靠墙太近 25 ~ SCREEN_X-25 之间 for pos in range(25,SCREEN_X-25,25): allpos.append(pos) - self.rect.left = random.choice(allpos) - self.rect.top = random.choice(allpos) + while True: + self.rect.left = random.choice(allpos) + self.rect.top = random.choice(allpos) + # 检查食物是否与蛇的任意部分重叠 + if not any(food_rect.colliderect(snake_rect) for snake_rect in snake.body): + break print(self.rect) -- Gitee From 96e067adb0e57250ff75ad36b155dcfb3b0b75d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=BF=8A=E7=9A=93?= <14243992+zhao-junhao1229@user.noreply.gitee.com> Date: Sat, 20 Jul 2024 10:58:32 +0000 Subject: [PATCH 2/3] update main.py. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 赵俊皓 <14243992+zhao-junhao1229@user.noreply.gitee.com> --- main.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 67b6739..e33a566 100644 --- a/main.py +++ b/main.py @@ -84,10 +84,18 @@ class Food: allpos.append(pos) while True: self.rect.left = random.choice(allpos) - self.rect.top = random.choice(allpos) - # 检查食物是否与蛇的任意部分重叠 - if not any(food_rect.colliderect(snake_rect) for snake_rect in snake.body): + self.rect.top = random.choice(allpos) + + # 检查食物位置是否与蛇身体重合 + overlap = False + for segment in snake.body: + if segment.colliderect(self.rect): + overlap = True + break + + if not overlap: break + print(self.rect) -- Gitee From 7b5e40e37787564824042f7117d1fc3478bfc8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=BF=8A=E7=9A=93?= <14243992+zhao-junhao1229@user.noreply.gitee.com> Date: Sat, 20 Jul 2024 11:27:23 +0000 Subject: [PATCH 3/3] update main.py. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 赵俊皓 <14243992+zhao-junhao1229@user.noreply.gitee.com> --- main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index e33a566..386b0d4 100644 --- a/main.py +++ b/main.py @@ -69,14 +69,14 @@ class Snake(object): # 食物类 # 方法: 放置/移除 # 点以25为单位 -class Food: +class Food(object): def __init__(self): self.rect = pygame.Rect(-25,0,25,25) def remove(self): self.rect.x=-25 - def set(self): + def set(self,snake): if self.rect.x == -25: allpos = [] # 不靠墙太近 25 ~ SCREEN_X-25 之间 @@ -159,7 +159,7 @@ def main(): snake.addnode() # 食物投递 - food.set() + food.set(snake) pygame.draw.rect(screen,(136,0,21),food.rect,0) # 显示分数文字 -- Gitee